Skip to content

Commit

Permalink
Add support for DoctrineModule v6.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz authored and TomHAnderson committed Jun 8, 2023
1 parent 300b6ec commit 1bddfed
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ jobs:
- name: "Configure test application"
run: "cp ci/config/application.config.php config/application.config.php"

- name: "Create cache directory"
run: "mkdir -p data/DoctrineModule/cache"

- name: "Run PHPUnit"
run: "vendor/bin/phpunit --coverage-clover=coverage.xml"

Expand Down
2 changes: 2 additions & 0 deletions ci/config/application.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
return [
'modules' => [
'Laminas\Cache',
'Laminas\Cache\Storage\Adapter\Filesystem',
'Laminas\Cache\Storage\Adapter\Memory',
'Laminas\Form',
'Laminas\Hydrator',
'Laminas\Paginator',
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"ext-json": "*",
"doctrine/dbal": "^2.13.7 || ^3.3.2",
"doctrine/doctrine-laminas-hydrator": "^3.0.0",
"doctrine/doctrine-module": "^5.3.0",
"doctrine/doctrine-module": "^5.3.0 || ^6.0.2",
"doctrine/event-manager": "^1.1.1",
"doctrine/orm": "^2.11.1",
"doctrine/persistence": "^2.3.0 || ^3.0.0",
Expand All @@ -71,6 +71,8 @@
"doctrine/coding-standard": "^9.0.0",
"doctrine/data-fixtures": "^1.5.2",
"doctrine/migrations": "^3.4.1",
"laminas/laminas-cache-storage-adapter-filesystem": "^2.0",
"laminas/laminas-cache-storage-adapter-memory": "^2.0",
"laminas/laminas-developer-tools": "^2.3.0",
"laminas/laminas-i18n": "^2.13.0",
"laminas/laminas-log": "^2.15.0",
Expand Down
19 changes: 15 additions & 4 deletions tests/Service/ConfigurationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
use Doctrine\ORM\Mapping\NamingStrategy;
use Doctrine\ORM\Mapping\QuoteStrategy;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use DoctrineModule\Cache\LaminasStorageCache;
use DoctrineORMModule\Options\Configuration;
use DoctrineORMModule\Service\ConfigurationFactory;
use DoctrineORMModuleTest\Assets\RepositoryClass;
use Laminas\Cache\Storage\Adapter\Memory;
use Laminas\ServiceManager\Exception\InvalidArgumentException;
use Laminas\ServiceManager\ServiceManager;
use PHPUnit\Framework\TestCase;
Expand All @@ -27,6 +29,7 @@

use function assert;
use function class_exists;
use function get_class;

class ConfigurationFactoryTest extends TestCase
{
Expand All @@ -37,13 +40,21 @@ public function setUp(): void
{
$this->serviceManager = new ServiceManager();
$this->factory = new ConfigurationFactory('test_default');
$this->serviceManager->setService('doctrine.cache.array', new ArrayCache());
$this->serviceManager->setService('doctrine.cache.array', $this->getArrayCacheInstance());
$this->serviceManager->setService(
'doctrine.driver.orm_default',
$this->createMock(MappingDriver::class)
);
}

protected function getArrayCacheInstance(): object
{
// Set up appropriate cache based on DoctrineModule version detection:
return class_exists(ArrayCache::class)
? new ArrayCache() // DoctrineModule 5
: new LaminasStorageCache(new Memory()); // DoctrineModule 6
}

public function testWillInstantiateConfigWithoutNamingStrategySetting(): void
{
$config = [
Expand Down Expand Up @@ -164,7 +175,7 @@ public function testWillInstantiateConfigWithHydrationCacheSetting(): void
$this->serviceManager->setService('config', $config);
$factory = new ConfigurationFactory('test_default');
$ormConfig = $factory($this->serviceManager, Configuration::class);
$this->assertInstanceOf(ArrayCache::class, $ormConfig->getHydrationCacheImpl());
$this->assertInstanceOf(get_class($this->getArrayCacheInstance()), $ormConfig->getHydrationCacheImpl());
}

public function testCanSetDefaultRepositoryClass(): void
Expand All @@ -184,7 +195,7 @@ public function testCanSetDefaultRepositoryClass(): void

$factory = new ConfigurationFactory('test_default');
$ormConfig = $factory($this->serviceManager, Configuration::class);
$this->assertInstanceOf(ArrayCache::class, $ormConfig->getHydrationCacheImpl());
$this->assertInstanceOf(get_class($this->getArrayCacheInstance()), $ormConfig->getHydrationCacheImpl());
}

public function testAcceptsMetadataFactory(): void
Expand Down Expand Up @@ -376,7 +387,7 @@ public function testCanInstantiateWithSecondLevelCacheConfig(): void
$reflProperty->setAccessible(true);
$cacheDecorator = $reflProperty->getValue($cacheFactory);
$this->assertInstanceOf(CacheAdapter::class, $cacheDecorator);
$this->assertInstanceOf(ArrayCache::class, $cacheDecorator->getCache());
$this->assertInstanceOf(get_class($this->getArrayCacheInstance()), $cacheDecorator->getCache());
}

public function testConfigureMiddlewares(): void
Expand Down
10 changes: 9 additions & 1 deletion tests/Service/DBALConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Configuration;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use DoctrineModule\Cache\LaminasStorageCache;
use DoctrineORMModule\Service\ConfigurationFactory;
use DoctrineORMModule\Service\DBALConnectionFactory;
use DoctrineORMModuleTest\Assets\Types\MoneyType;
use Laminas\Cache\Storage\Adapter\Memory;
use Laminas\ServiceManager\ServiceManager;
use PHPUnit\Framework\TestCase;

use function class_exists;

/**
* @covers \DoctrineORMModule\Service\DBALConnectionFactory
*/
Expand All @@ -30,7 +34,11 @@ public function setUp(): void
{
$this->serviceManager = new ServiceManager();
$this->factory = new DBALConnectionFactory('orm_default');
$this->serviceManager->setService('doctrine.cache.array', new ArrayCache());
// Set up appropriate cache based on DoctrineModule version detection:
$arrayCache = class_exists(ArrayCache::class)
? new ArrayCache() // DoctrineModule 5
: new LaminasStorageCache(new Memory()); // DoctrineModule 6
$this->serviceManager->setService('doctrine.cache.array', $arrayCache);
$this->serviceManager->setService('doctrine.eventmanager.orm_default', new EventManager());
}

Expand Down
1 change: 1 addition & 0 deletions tests/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
return [
'modules' => [
'Laminas\Cache',
'Laminas\Cache\Storage\Adapter\Memory',
'Laminas\Form',
'Laminas\Hydrator',
'Laminas\Paginator',
Expand Down

0 comments on commit 1bddfed

Please sign in to comment.