From ed095226061b9bd787622bd56d603f1f642bb66c Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 11 Sep 2021 21:22:18 +0200 Subject: [PATCH] Fix tests (DBAL 3, Symfony 6) (#1399) --- Command/Proxy/DoctrineCommandHelper.php | 6 +++- Tests/ConnectionFactoryTest.php | 34 ++++++++++++++++--- .../Compiler/CacheCompatibilityPassTest.php | 3 +- Tests/ProfilerTest.php | 4 ++- Tests/TestCase.php | 14 +++++++- 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/Command/Proxy/DoctrineCommandHelper.php b/Command/Proxy/DoctrineCommandHelper.php index d395a280c..c1e205cbc 100644 --- a/Command/Proxy/DoctrineCommandHelper.php +++ b/Command/Proxy/DoctrineCommandHelper.php @@ -8,6 +8,7 @@ use Symfony\Bundle\FrameworkBundle\Console\Application; use function assert; +use function class_exists; /** * Provides some helper and convenience methods to configure doctrine commands in the context of bundles @@ -25,7 +26,10 @@ public static function setApplicationEntityManager(Application $application, $em $em = $application->getKernel()->getContainer()->get('doctrine')->getManager($emName); assert($em instanceof EntityManagerInterface); $helperSet = $application->getHelperSet(); - $helperSet->set(new ConnectionHelper($em->getConnection()), 'db'); + if (class_exists(ConnectionHelper::class)) { + $helperSet->set(new ConnectionHelper($em->getConnection()), 'db'); + } + $helperSet->set(new EntityManagerHelper($em), 'em'); } diff --git a/Tests/ConnectionFactoryTest.php b/Tests/ConnectionFactoryTest.php index b9fdfa77c..979ca32dd 100644 --- a/Tests/ConnectionFactoryTest.php +++ b/Tests/ConnectionFactoryTest.php @@ -8,9 +8,11 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Driver; +use Doctrine\DBAL\Driver\API\ExceptionConverter; +use Doctrine\DBAL\Driver\Exception as InternalDriverException; use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Platforms\AbstractPlatform; -use Doctrine\DBAL\Platforms\MySqlPlatform; +use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager; use Exception; use Throwable; @@ -19,6 +21,9 @@ use function class_exists; use function strpos; +// Compatibility with DBAL < 3 +class_exists(\Doctrine\DBAL\Platforms\MySqlPlatform::class); + class ConnectionFactoryTest extends TestCase { public function testContainer(): void @@ -29,9 +34,10 @@ public function testContainer(): void $config = null; $eventManager = null; $mappingTypes = ['' => '']; - $exception = class_exists(Driver\AbstractDriverException::class) ? + /** @psalm-suppress InvalidArgument */ + $exception = class_exists(Driver\AbstractDriverException::class) ? new DriverException('', $this->createMock(Driver\AbstractDriverException::class)) : - new DriverException('', $this->createMock(Driver\AbstractException::class)); + new DriverException($this->createMock(InternalDriverException::class), null); // put the mock into the fake driver FakeDriver::$exception = $exception; @@ -139,6 +145,10 @@ class FakeDriver implements Driver * So we have to fake the exception a driver would normally throw. * * @link https://github.com/doctrine/DoctrineBundle/issues/673 + * + * @psalm-suppress InvalidReturnStatement + * @psalm-suppress InvalidReturnType + * @psalm-suppress UndefinedClass */ public function getDatabasePlatform(): AbstractPlatform { @@ -146,7 +156,7 @@ public function getDatabasePlatform(): AbstractPlatform throw self::$exception; } - return static::$platform ?? new MySqlPlatform(); + return static::$platform ?? new MySQLPlatform(); } // ----- below this line follow only dummy methods to satisfy the interface requirements ---- @@ -169,6 +179,22 @@ public function getSchemaManager(Connection $conn, ?AbstractPlatform $platform = throw new Exception('not implemented'); } + /** + * @psalm-suppress InvalidReturnStatement + * @psalm-suppress InvalidReturnType + * @psalm-suppress MissingDependency + * @psalm-suppress UndefinedClass + */ + public function getExceptionConverter(): ExceptionConverter + { + return new class implements ExceptionConverter { + public function convert(InternalDriverException $exception, ?Query $query): DriverException + { + return new DriverException($exception, $query); + } + }; + } + public function getName(): string { return 'FakeDriver'; diff --git a/Tests/DependencyInjection/Compiler/CacheCompatibilityPassTest.php b/Tests/DependencyInjection/Compiler/CacheCompatibilityPassTest.php index 73b5d559f..f0aa01101 100644 --- a/Tests/DependencyInjection/Compiler/CacheCompatibilityPassTest.php +++ b/Tests/DependencyInjection/Compiler/CacheCompatibilityPassTest.php @@ -17,7 +17,6 @@ class CacheCompatibilityPassTest extends TestCase public function testCacheConfigUsingServiceDefinedByApplication(): void { - $this->expectNotToPerformAssertions(); (new class () extends TestKernel { public function registerContainerConfiguration(LoaderInterface $loader): void { @@ -54,6 +53,8 @@ public function registerContainerConfiguration(LoaderInterface $loader): void }); } })->boot(); + + $this->addToAssertionCount(1); } /** @group legacy */ diff --git a/Tests/ProfilerTest.php b/Tests/ProfilerTest.php index 5520afc49..53eff5e4e 100644 --- a/Tests/ProfilerTest.php +++ b/Tests/ProfilerTest.php @@ -41,7 +41,9 @@ public function setUp(): void { $this->logger = new DebugStack(); $registry = $this->getMockBuilder(ManagerRegistry::class)->getMock(); - $registry->expects($this->once())->method('getManagers')->willReturn([]); + $registry->method('getConnectionNames')->willReturn([]); + $registry->method('getManagerNames')->willReturn([]); + $registry->method('getManagers')->willReturn([]); $this->collector = new DoctrineDataCollector($registry); $this->collector->addLogger('foo', $this->logger); diff --git a/Tests/TestCase.php b/Tests/TestCase.php index 0ad1241f5..2154adde5 100644 --- a/Tests/TestCase.php +++ b/Tests/TestCase.php @@ -6,18 +6,28 @@ use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension; use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestType; use Doctrine\Common\Annotations\AnnotationReader; +use Doctrine\DBAL\Platforms\MySQLPlatform; use PHPUnit\Framework\TestCase as BaseTestCase; +use ReflectionClass; use Symfony\Component\Cache\Adapter\ArrayAdapter; use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; +use function class_exists; use function sys_get_temp_dir; use function uniqid; +// Compatibility with DBAL < 3 +class_exists(\Doctrine\DBAL\Platforms\MySqlPlatform::class); + class TestCase extends BaseTestCase { + /** + * @psalm-suppress InvalidClass + * @psalm-suppress UndefinedClass + */ public function createXmlBundleTestContainer(): ContainerBuilder { $container = new ContainerBuilder(new ParameterBag([ @@ -69,7 +79,9 @@ public function createXmlBundleTestContainer(): ContainerBuilder ], ], $container); - $container->setDefinition('my.platform', new Definition('Doctrine\DBAL\Platforms\MySqlPlatform'))->setPublic(true); + // Fix casing: The class is named MySqlPlatform on DBAL 2. + $platformClassName = (new ReflectionClass(MySQLPlatform::class))->getName(); + $container->setDefinition('my.platform', new Definition($platformClassName))->setPublic(true); // Register dummy cache services so we don't have to load the FrameworkExtension $container->setDefinition('cache.system', (new Definition(ArrayAdapter::class))->setPublic(true));