Skip to content

Commit

Permalink
Fix tests (DBAL 3, Symfony 6) (#1399)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Sep 11, 2021
1 parent c394f20 commit ed09522
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Command/Proxy/DoctrineCommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
}

Expand Down
34 changes: 30 additions & 4 deletions Tests/ConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -139,14 +145,18 @@ 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
{
if (self::$exception !== null) {
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 ----
Expand All @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class CacheCompatibilityPassTest extends TestCase

public function testCacheConfigUsingServiceDefinedByApplication(): void
{
$this->expectNotToPerformAssertions();
(new class () extends TestKernel {
public function registerContainerConfiguration(LoaderInterface $loader): void
{
Expand Down Expand Up @@ -54,6 +53,8 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
});
}
})->boot();

$this->addToAssertionCount(1);
}

/** @group legacy */
Expand Down
4 changes: 3 additions & 1 deletion Tests/ProfilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 13 additions & 1 deletion Tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit ed09522

Please sign in to comment.