From 311a153c232b9c1fadcbca9d4823f43428d34a3f Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Sat, 6 Apr 2019 11:49:51 +0200 Subject: [PATCH] Add tests for type registration including deprecations --- Tests/ConnectionFactoryTest.php | 125 +++++++++++++++++- .../DependencyInjection/TestCommentedType.php | 24 ++++ Tests/TestCase.php | 5 +- phpunit.xml.dist | 4 + 4 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 Tests/DependencyInjection/TestCommentedType.php diff --git a/Tests/ConnectionFactoryTest.php b/Tests/ConnectionFactoryTest.php index ea0873911..dc7018018 100644 --- a/Tests/ConnectionFactoryTest.php +++ b/Tests/ConnectionFactoryTest.php @@ -3,9 +3,13 @@ namespace Doctrine\Bundle\DoctrineBundle\Tests; use Doctrine\Bundle\DoctrineBundle\ConnectionFactory; +use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestCommentedType; +use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestType; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; use Doctrine\DBAL\Exception\DriverException; +use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\ORM\Version; use Exception; @@ -29,11 +33,11 @@ public function testContainer() { $typesConfig = []; $factory = new ConnectionFactory($typesConfig); - $params = ['driverClass' => '\\Doctrine\\Bundle\\DoctrineBundle\\Tests\\FakeDriver']; + $params = ['driverClass' => FakeDriver::class]; $config = null; $eventManager = null; $mappingTypes = [0]; - $exception = new DriverException('', $this->getMockBuilder(Driver\AbstractDriverException::class)->disableOriginalConstructor()->getMock()); + $exception = new DriverException('', $this->createMock(Driver\AbstractDriverException::class)); // put the mock into the fake driver FakeDriver::$exception = $exception; @@ -43,8 +47,116 @@ public function testContainer() } catch (Exception $e) { $this->assertTrue(strpos($e->getMessage(), 'can circumvent this by setting') > 0); throw $e; + } finally { + FakeDriver::$exception = null; } } + + /** + * @dataProvider getValidTypeConfigurations + */ + public function testRegisterTypes(array $type, int $expectedCalls) : void + { + $factory = new ConnectionFactory(['test' => $type]); + $params = ['driverClass' => FakeDriver::class]; + $config = null; + $eventManager = null; + $mappingTypes = []; + + $platform = $this->createMock(AbstractPlatform::class); + $platform + ->expects($this->exactly($expectedCalls)) + ->method('markDoctrineTypeCommented') + ->with($this->isInstanceOf($type['class'])); + + FakeDriver::$platform = $platform; + + try { + $factory->createConnection($params, $config, $eventManager, $mappingTypes); + } finally { + FakeDriver::$platform = null; + } + } + + public static function getValidTypeConfigurations() : array + { + return [ + 'uncommentedTypeMarkedNotCommented' => [ + 'type' => [ + 'class' => TestType::class, + 'commented' => false, + ], + 'expectedCalls' => 0, + ], + 'commentedTypeNotMarked' => [ + 'type' => [ + 'class' => TestCommentedType::class, + 'commented' => null, + ], + 'expectedCalls' => 0, + ], + ]; + } + + /** + * @group legacy + * @expectedDeprecation The type "test" was implicitly marked as commented due to the configuration. This is deprecated and will be removed in DoctrineBundle 2.0. Either set the "commented" attribute in the configuration to "false" or mark the type as commented in "Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestType::requiresSQLCommentHint()." + */ + public function testRegisterUncommentedTypeNotMarked() : void + { + $this->testRegisterTypes( + [ + 'class' => TestType::class, + 'commented' => null, + ], + 1 + ); + } + + /** + * @group legacy + * @expectedDeprecation The type "test" was marked as commented in its configuration but not in the type itself. This is deprecated and will be removed in DoctrineBundle 2.0. Please update the return value of "Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestType::requiresSQLCommentHint()." + */ + public function testRegisterUncommentedTypeMarkedCommented() : void + { + $this->testRegisterTypes( + [ + 'class' => TestType::class, + 'commented' => true, + ], + 1 + ); + } + + /** + * @group legacy + * @expectedDeprecation The type "test" was explicitly marked as commented in its configuration. This is no longer necessary and will be removed in DoctrineBundle 2.0. Please remove the "commented" attribute from the type configuration. + */ + public function testRegisterCommentedTypeMarkedCommented() : void + { + $this->testRegisterTypes( + [ + 'class' => TestCommentedType::class, + 'commented' => true, + ], + 0 + ); + } + + /** + * @group legacy + * @expectedDeprecation Disabling type commenting for the commented type "test" is deprecated and will be removed in DoctrineBundle 2.0. Please remove the "commented" attribute from the configuration and instead disable type commenting in "Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestCommentedType::requiresSQLCommentHint()" if you no longer want the type to be commented. + */ + public function testRegisterCommentedTypeMarkedNotCommented() : void + { + $this->testRegisterTypes( + [ + 'class' => TestCommentedType::class, + 'commented' => false, + ], + 0 + ); + } } /** @@ -62,6 +174,9 @@ class FakeDriver implements Driver */ public static $exception; + /** @var AbstractPlatform|null */ + public static $platform; + /** * This method gets called to determine the database version which in our case leeds to the problem. * So we have to fake the exception a driver would normally throw. @@ -70,7 +185,11 @@ class FakeDriver implements Driver */ public function getDatabasePlatform() { - throw self::$exception; + if (self::$exception !== null) { + throw self::$exception; + } + + return static::$platform ?? new MySqlPlatform(); } // ----- below this line follow only dummy methods to satisfy the interface requirements ---- diff --git a/Tests/DependencyInjection/TestCommentedType.php b/Tests/DependencyInjection/TestCommentedType.php new file mode 100644 index 000000000..a453c964f --- /dev/null +++ b/Tests/DependencyInjection/TestCommentedType.php @@ -0,0 +1,24 @@ + 'default', 'types' => [ - 'test' => TestType::class, + 'test' => [ + 'class' => TestType::class, + 'commented' => false, + ], ], ], 'orm' => [ 'default_entity_manager' => 'default', diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 963871524..f33cab4ab 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -17,4 +17,8 @@ + + + +