Skip to content

Commit

Permalink
Merge pull request #16421 from rudiservo/i16420
Browse files Browse the repository at this point in the history
Fix #16420, fixed metadata get column from cache adapters, added tests
  • Loading branch information
niden authored Sep 5, 2023
2 parents d5a12bf + ddef5db commit c7820df
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed
- Infinite save loop in Model::save() [#16395](https://github.com/phalcon/cphalcon/issues/16395)
- Undefined column with columnMap and model caching [#16420] (https://github.com/phalcon/cphalcon/issues/16420)


## [5.3.0](https://github.com/phalcon/cphalcon/releases/tag/v5.3.0) (2023-08-15)
Expand Down
4 changes: 2 additions & 2 deletions phalcon/Mvc/Model/MetaData.zep
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ abstract class MetaData implements InjectionAwareInterface, MetaDataInterface
}

if true === isset(this->columnMap[key]) {
return false;
return true;
}

/**
Expand All @@ -845,7 +845,7 @@ abstract class MetaData implements InjectionAwareInterface, MetaDataInterface
if data !== null {
let this->columnMap[key] = data;

return false;
return true;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/_data/fixtures/Traits/DiTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
use Phalcon\Http\Request;
use Phalcon\Http\Response;
use Phalcon\Mvc\Model\Manager as ModelsManager;
use Phalcon\Mvc\Model\Metadata\Apcu as MetadataApcu;
use Phalcon\Mvc\Model\Metadata\Memory as MetadataMemory;
use Phalcon\Mvc\Model\Metadata\Libmemcached as MetadataMemcached;
use Phalcon\Mvc\Model\Metadata\Redis as MetadataRedis;
use Phalcon\Mvc\Model\Metadata\Stream as MetadataStream;
use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Simple;
use Phalcon\Session\Adapter\Libmemcached as SessionLibmemcached;
Expand All @@ -57,6 +59,7 @@
use function getOptionsRedis;
use function getOptionsSessionStream;
use function getOptionsSqlite;
use function outputDir;

/**
* Trait DiTrait
Expand Down Expand Up @@ -174,6 +177,11 @@ protected function newService(string $service, $options = null)
return (new Filter\FilterFactory())->newInstance();
case 'metadataMemory':
return new MetadataMemory();
case 'metadataApcu':
return new MetadataApcu(
new AdapterFactory(new SerializerFactory()),
[]
);
case 'metadataLibmemcached':
return new MetadataMemcached(
new AdapterFactory(new SerializerFactory()),
Expand All @@ -184,6 +192,11 @@ protected function newService(string $service, $options = null)
new AdapterFactory(new SerializerFactory()),
getOptionsRedis()
);
case 'metadataStream':
return new MetadataStream(
new AdapterFactory(new SerializerFactory()),
['options' => ['storageDir' => outputDir()] ],
);
case 'modelsCacheLibmemcached':
return new StorageLibmemcached(
new SerializerFactory(),
Expand Down
56 changes: 54 additions & 2 deletions tests/database/Mvc/Model/MetaData/GetAttributesCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Phalcon\Tests\Database\Mvc\Model\MetaData;

use Codeception\Example;
use DatabaseTester;
use Phalcon\Mvc\Model\MetaData;
use Phalcon\Tests\Fixtures\Traits\DiTrait;
Expand All @@ -34,17 +35,34 @@ public function _before(DatabaseTester $I)
/**
* Tests Phalcon\Mvc\Model\MetaData :: getAttributes()
*
* @dataProvider getExamples
*
* @param DatabaseTester $I
* @param Example $example
*
* @author Phalcon Team <team@phalcon.io>
* @since 2020-02-01
*
* @group mysql
* @group pgsql
* @group sqlite
*/
public function mvcModelMetadataGetAttributes(DatabaseTester $I)
{
public function mvcModelMetadataGetAttributes(
DatabaseTester $I,
Example $example
) {
$I->wantToTest('Mvc\Model\MetaData - getAttributes()');

$service = $example['service'];

$adapter = $this->newService($service);
$connection = $I->getConnection();
$adapter->setDi($this->container);

$adapter->reset();

$this->container->setShared('modelsMetadata', $adapter);

/** @var MetaData $metadata */
$metadata = $this->container->get('modelsMetadata');

Expand All @@ -59,5 +77,39 @@ public function mvcModelMetadataGetAttributes(DatabaseTester $I)
];
$actual = $metadata->getAttributes($model);
$I->assertEquals($expected, $actual);

$adapter = $this->newService($service);
$this->container->setShared('modelsMetadata', $adapter);
$adapter->setDi($this->container);

$I->assertNotEquals($adapter, $metadata);

$actual = $adapter->getAttributes($model);
$I->assertEquals($expected, $actual);
}

/**
* @return array[]
*/
private function getExamples(): array
{
return [
[
'service' => 'metadataMemory',
'className' => 'Memory',
],
[
'service' => 'metadataApcu',
'className' => 'Apcu',
],
[
'service' => 'metadataRedis',
'className' => 'Redis',
],
[
'service' => 'metadataLibmemcached',
'className' => 'Libmemcached',
],
];
}
}
62 changes: 59 additions & 3 deletions tests/database/Mvc/Model/MetaData/GetColumnMapCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Phalcon\Tests\Database\Mvc\Model\MetaData;

use Codeception\Example;
use DatabaseTester;
use Phalcon\Mvc\Model\MetaData;
use Phalcon\Storage\Exception;
Expand Down Expand Up @@ -47,7 +48,10 @@ public function _before(DatabaseTester $I): void
/**
* Tests Phalcon\Mvc\Model\MetaData :: getColumnMap()
*
* @param DatabaseTester $I
* @dataProvider getExamples
*
* @param DatabaseTester $I
* @param Example $example
*
* @author Phalcon Team <team@phalcon.io>
* @since 2020-02-01
Expand All @@ -56,10 +60,22 @@ public function _before(DatabaseTester $I): void
* @group pgsql
* @group sqlite
*/
public function mvcModelMetadataGetColumnMap(DatabaseTester $I)
{
public function mvcModelMetadataGetColumnMap(
DatabaseTester $I,
Example $example
) {
$I->wantToTest('Mvc\Model\MetaData - getColumnMap()');

$service = $example['service'];

$adapter = $this->newService($service);
$adapter->setDi($this->container);
$connection = $I->getConnection();

$adapter->reset();

$this->container->setShared('modelsMetadata', $adapter);

/** @var MetaData $metadata */
$metadata = $this->container->get('modelsMetadata');
$model = new Invoices();
Expand All @@ -77,5 +93,45 @@ public function mvcModelMetadataGetColumnMap(DatabaseTester $I)
];

$I->assertEquals($expected, $metadata->getColumnMap($model));

$I->assertTrue($adapter->isEmpty());

/**
* Double check it can get from cache systems and not memory
*/
$adapter = $this->newService($service);
$this->container->setShared('modelsMetadata', $adapter);
$adapter->setDi($this->container);

$I->assertNotEquals($adapter, $metadata);

$I->assertTrue($adapter->isEmpty());

$I->assertEquals($expected, $adapter->getColumnMap($model));
}

/**
* @return array[]
*/
private function getExamples(): array
{
return [
[
'service' => 'metadataMemory',
'className' => 'Memory',
],
[
'service' => 'metadataApcu',
'className' => 'Apcu',
],
[
'service' => 'metadataRedis',
'className' => 'Redis',
],
[
'service' => 'metadataLibmemcached',
'className' => 'Libmemcached',
],
];
}
}
64 changes: 62 additions & 2 deletions tests/database/Mvc/Model/MetaData/GetDataTypesCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Phalcon\Tests\Database\Mvc\Model\MetaData;

use Codeception\Example;
use DatabaseTester;
use Phalcon\Mvc\Model\MetaData;
use Phalcon\Tests\Fixtures\Traits\DiTrait;
Expand All @@ -34,24 +35,58 @@ public function _before(DatabaseTester $I)
/**
* Tests Phalcon\Mvc\Model\MetaData :: getDataTypes()
*
* @dataProvider getExamples
*
* @param DatabaseTester $I
* @param Example $example
*
* @author Phalcon Team <team@phalcon.io>
* @since 2020-02-01
*
* @group mysql
* @group pgsql
* @group sqlite
*/
public function mvcModelMetadataGetDataTypes(DatabaseTester $I)
{
public function mvcModelMetadataGetDataTypes(
DatabaseTester $I,
Example $example
) {
$I->wantToTest('Mvc\Model\MetaData - getDataTypes()');

$service = $example['service'];

$adapter = $this->newService($service);
$adapter->setDi($this->container);
$connection = $I->getConnection();

$adapter->reset();
$I->assertTrue($adapter->isEmpty());

$this->container->setShared('modelsMetadata', $adapter);

/** @var MetaData $metadata */
$metadata = $this->container->get('modelsMetadata');

$model = new Invoices();
$expected = $this->getTypes($I);
$actual = $metadata->getDataTypes($model);
$I->assertEquals($expected, $actual);

$I->assertFalse($adapter->isEmpty());

/**
* Double check it can get from cache systems and not memory
*/
$adapter = $this->newService($service);
$this->container->setShared('modelsMetadata', $adapter);
$adapter->setDi($this->container);

$I->assertNotEquals($adapter, $metadata);

$I->assertTrue($adapter->isEmpty());

$actual = $adapter->getDataTypes($model);
$I->assertEquals($expected, $actual);
}

private function getTypes(DatabaseTester $I)
Expand Down Expand Up @@ -87,4 +122,29 @@ private function getTypes(DatabaseTester $I)
];
}
}

/**
* @return array[]
*/
private function getExamples(): array
{
return [
[
'service' => 'metadataMemory',
'className' => 'Memory',
],
[
'service' => 'metadataApcu',
'className' => 'Apcu',
],
[
'service' => 'metadataRedis',
'className' => 'Redis',
],
[
'service' => 'metadataLibmemcached',
'className' => 'Libmemcached',
],
];
}
}
Loading

0 comments on commit c7820df

Please sign in to comment.