diff --git a/tests/Doctrine/Tests/Mocks/DatabasePlatformMock.php b/tests/Doctrine/Tests/Mocks/DatabasePlatformMock.php index 546fa96a4f..13f05b7e5f 100644 --- a/tests/Doctrine/Tests/Mocks/DatabasePlatformMock.php +++ b/tests/Doctrine/Tests/Mocks/DatabasePlatformMock.php @@ -13,12 +13,6 @@ */ class DatabasePlatformMock extends AbstractPlatform { - /** @var bool */ - private $supportsIdentityColumns = true; - - /** @var bool */ - private $supportsSequences = false; - public function prefersIdentityColumns(): bool { throw new BadMethodCallException('Call to deprecated method.'); @@ -26,7 +20,7 @@ public function prefersIdentityColumns(): bool public function supportsIdentityColumns(): bool { - return $this->supportsIdentityColumns; + return true; } public function prefersSequences(): bool @@ -36,7 +30,7 @@ public function prefersSequences(): bool public function supportsSequences(): bool { - return $this->supportsSequences; + return false; } /** @@ -98,16 +92,6 @@ public function getClobTypeDeclarationSQL(array $field) /* MOCK API */ - public function setSupportsIdentityColumns(bool $bool): void - { - $this->supportsIdentityColumns = $bool; - } - - public function setSupportsSequences(bool $bool): void - { - $this->supportsSequences = $bool; - } - public function getName(): string { throw new BadMethodCallException('Call to deprecated method.'); diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7869Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7869Test.php index 5ff4820a26..0874ef1b4b 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7869Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7869Test.php @@ -4,6 +4,8 @@ namespace Doctrine\Tests\ORM\Functional\Ticket; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\ORM\Decorator\EntityManagerDecorator; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\Column; @@ -12,8 +14,6 @@ use Doctrine\ORM\Mapping\Id; use Doctrine\ORM\Mapping\OneToOne; use Doctrine\ORM\UnitOfWork; -use Doctrine\Tests\Mocks\ConnectionMock; -use Doctrine\Tests\Mocks\DriverMock; use Doctrine\Tests\Mocks\EntityManagerMock; use Doctrine\Tests\OrmTestCase; @@ -24,7 +24,15 @@ class GH7869Test extends OrmTestCase { public function testDQLDeferredEagerLoad(): void { - $decoratedEm = EntityManagerMock::create(new ConnectionMock([], new DriverMock())); + $platform = $this->createMock(AbstractPlatform::class); + $platform->method('supportsIdentityColumns') + ->willReturn(true); + + $connection = $this->createMock(Connection::class); + $connection->method('getDatabasePlatform') + ->willReturn($platform); + + $decoratedEm = EntityManagerMock::create($connection); $em = $this->getMockBuilder(EntityManagerDecorator::class) ->setConstructorArgs([$decoratedEm]) diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php index a1517f78f9..65eb46278a 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php @@ -6,6 +6,8 @@ use Doctrine\Common\EventManager; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Driver; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\ORM\Configuration; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\OnClassMetadataNotFoundEventArgs; @@ -25,9 +27,6 @@ use Doctrine\ORM\Mapping\MappingException; use Doctrine\Persistence\Mapping\Driver\MappingDriver; use Doctrine\Persistence\Mapping\RuntimeReflectionService; -use Doctrine\Tests\Mocks\ConnectionMock; -use Doctrine\Tests\Mocks\DatabasePlatformMock; -use Doctrine\Tests\Mocks\DriverMock; use Doctrine\Tests\Mocks\EntityManagerMock; use Doctrine\Tests\Mocks\MetadataDriverMock; use Doctrine\Tests\Models\CMS\CmsArticle; @@ -53,13 +52,18 @@ class ClassMetadataFactoryTest extends OrmTestCase { public function testGetMetadataForSingleClass(): void { - $mockDriver = new MetadataDriverMock(); - $entityManager = $this->createEntityManager($mockDriver); + $platform = $this->createMock(AbstractPlatform::class); + $platform->method('supportsSequences') + ->willReturn(true); + + $driver = $this->createMock(Driver::class); + $driver->method('getDatabasePlatform') + ->willReturn($platform); - $conn = $entityManager->getConnection(); - $mockPlatform = $conn->getDatabasePlatform(); - $mockPlatform->setSupportsSequences(true); - $mockPlatform->setSupportsIdentityColumns(false); + $conn = new Connection([], $driver); + + $mockDriver = new MetadataDriverMock(); + $entityManager = $this->createEntityManager($mockDriver, $conn); $cm1 = $this->createValidClassMetadata(); @@ -90,13 +94,14 @@ public function testItThrowsWhenUsingAutoWithIncompatiblePlatform(): void { $cm1 = $this->createValidClassMetadata(); $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO); - $entityManager = $this->createEntityManager(new MetadataDriverMock()); - $connection = $entityManager->getConnection(); - assert($connection instanceof ConnectionMock); - $platform = $connection->getDatabasePlatform(); - assert($platform instanceof DatabasePlatformMock); - $platform->setSupportsIdentityColumns(false); - $cmf = new ClassMetadataFactoryTestSubject(); + + $driver = $this->createMock(Driver::class); + $driver->method('getDatabasePlatform') + ->willReturn($this->createMock(AbstractPlatform::class)); + + $connection = new Connection([], $driver); + $entityManager = $this->createEntityManager(new MetadataDriverMock(), $connection); + $cmf = new ClassMetadataFactoryTestSubject(); $cmf->setEntityManager($entityManager); $cmf->setMetadataForClass($cm1->name, $cm1); $this->expectException(CannotGenerateIds::class); @@ -276,13 +281,20 @@ public function testGetAllMetadataWorksWithBadConnection(): void protected function createEntityManager(MappingDriver $metadataDriver, $conn = null): EntityManagerMock { - $driverMock = new DriverMock(); - $config = new Configuration(); + $config = new Configuration(); $config->setProxyDir(__DIR__ . '/../../Proxies'); $config->setProxyNamespace('Doctrine\Tests\Proxies'); $eventManager = new EventManager(); if (! $conn) { - $conn = new ConnectionMock([], $driverMock, $config, $eventManager); + $platform = $this->createMock(AbstractPlatform::class); + $platform->method('supportsIdentityColumns') + ->willReturn(true); + + $driver = $this->createMock(Driver::class); + $driver->method('getDatabasePlatform') + ->willReturn($platform); + + $conn = new Connection([], $driver, $config, $eventManager); } $config->setMetadataDriverImpl($metadataDriver); @@ -404,7 +416,6 @@ public function testQuoteMetadata(): void */ public function testFallbackLoadingCausesEventTriggeringThatCanModifyFetchedMetadata(): void { - $test = $this; $metadata = $this->createMock(ClassMetadata::class); assert($metadata instanceof ClassMetadata); $cmf = new ClassMetadataFactory(); diff --git a/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php b/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php index a38c8a5fbf..01cbc2116b 100644 --- a/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/PersistentCollectionTest.php @@ -5,10 +5,11 @@ namespace Doctrine\Tests\ORM; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Result; use Doctrine\ORM\PersistentCollection; use Doctrine\ORM\UnitOfWork; -use Doctrine\Tests\Mocks\ConnectionMock; -use Doctrine\Tests\Mocks\DriverMock; use Doctrine\Tests\Mocks\EntityManagerMock; use Doctrine\Tests\Models\ECommerce\ECommerceCart; use Doctrine\Tests\Models\ECommerce\ECommerceProduct; @@ -18,6 +19,7 @@ use function array_keys; use function assert; +use function method_exists; /** * Tests the lazy-loading capabilities of the PersistentCollection and the initialization of collections. @@ -34,7 +36,22 @@ protected function setUp(): void { parent::setUp(); - $this->_emMock = EntityManagerMock::create(new ConnectionMock([], new DriverMock())); + $platform = $this->createMock(AbstractPlatform::class); + $platform->method('supportsIdentityColumns') + ->willReturn(true); + + if (method_exists($platform, 'getSQLResultCasing')) { + $platform->method('getSQLResultCasing') + ->willReturnArgument(0); + } + + $connection = $this->createMock(Connection::class); + $connection->method('getDatabasePlatform') + ->willReturn($platform); + $connection->method('executeQuery') + ->willReturn($this->createMock(Result::class)); + + $this->_emMock = EntityManagerMock::create($connection); $this->setUpPersistentCollection(); } diff --git a/tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php b/tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php index 309eb927fc..f86d698fa0 100644 --- a/tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php @@ -6,13 +6,13 @@ use Doctrine\Common\Proxy\AbstractProxyFactory; use Doctrine\Common\Proxy\Proxy; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\ORM\EntityNotFoundException; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Persisters\Entity\BasicEntityPersister; use Doctrine\ORM\Proxy\ProxyFactory; use Doctrine\Persistence\Mapping\RuntimeReflectionService; -use Doctrine\Tests\Mocks\ConnectionMock; -use Doctrine\Tests\Mocks\DriverMock; use Doctrine\Tests\Mocks\EntityManagerMock; use Doctrine\Tests\Mocks\UnitOfWorkMock; use Doctrine\Tests\Models\Company\CompanyEmployee; @@ -30,8 +30,8 @@ */ class ProxyFactoryTest extends OrmTestCase { - /** @var ConnectionMock */ - private $connectionMock; + /** @var Connection */ + private $connection; /** @var UnitOfWorkMock */ private $uowMock; @@ -44,10 +44,17 @@ class ProxyFactoryTest extends OrmTestCase protected function setUp(): void { - parent::setUp(); - $this->connectionMock = new ConnectionMock([], new DriverMock()); - $this->emMock = EntityManagerMock::create($this->connectionMock); - $this->uowMock = new UnitOfWorkMock($this->emMock); + $platform = $this->createMock(AbstractPlatform::class); + $platform->method('supportsIdentityColumns') + ->willReturn(true); + + $connection = $this->createMock(Connection::class); + $connection->method('getDatabasePlatform') + ->willReturn($platform); + + $this->connection = $connection; + $this->emMock = EntityManagerMock::create($this->connection); + $this->uowMock = new UnitOfWorkMock($this->emMock); $this->emMock->setUnitOfWork($this->uowMock); $this->proxyFactory = new ProxyFactory($this->emMock, sys_get_temp_dir(), 'Proxies', AbstractProxyFactory::AUTOGENERATE_ALWAYS); } diff --git a/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php b/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php index 41a44b651a..414d3d6cf3 100644 --- a/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php @@ -5,14 +5,14 @@ namespace Doctrine\Tests\ORM\Tools; use Doctrine\Common\EventManager; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\ORM\Configuration; use Doctrine\ORM\Mapping\Driver\YamlDriver; use Doctrine\ORM\Tools\ConvertDoctrine1Schema; use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory; use Doctrine\ORM\Tools\Export\ClassMetadataExporter; use Doctrine\Persistence\Mapping\Driver\MappingDriver; -use Doctrine\Tests\Mocks\ConnectionMock; -use Doctrine\Tests\Mocks\DriverMock; use Doctrine\Tests\Mocks\EntityManagerMock; use Doctrine\Tests\OrmTestCase; @@ -31,15 +31,22 @@ class ConvertDoctrine1SchemaTest extends OrmTestCase { protected function createEntityManager(MappingDriver $metadataDriver): EntityManagerMock { - $driverMock = new DriverMock(); - $config = new Configuration(); + $platform = $this->createMock(AbstractPlatform::class); + $platform->method('supportsIdentityColumns') + ->willReturn(true); + + $connection = $this->createMock(Connection::class); + $connection->method('getDatabasePlatform') + ->willReturn($platform); + + $config = new Configuration(); $config->setProxyDir(__DIR__ . '/../../Proxies'); $config->setProxyNamespace('Doctrine\Tests\Proxies'); $eventManager = new EventManager(); - $conn = new ConnectionMock([], $driverMock, $config, $eventManager); + $config->setMetadataDriverImpl($metadataDriver); - return EntityManagerMock::create($conn, $config, $eventManager); + return EntityManagerMock::create($connection, $config, $eventManager); } public function testTest(): void diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php b/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php index 07e5ded4bc..efebde2970 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php @@ -5,6 +5,8 @@ namespace Doctrine\Tests\ORM\Tools\Export; use Doctrine\Common\EventManager; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\ORM\Configuration; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Events; @@ -19,8 +21,6 @@ use Doctrine\ORM\Tools\Export\ClassMetadataExporter; use Doctrine\Persistence\Mapping\Driver\MappingDriver; use Doctrine\Persistence\Mapping\Driver\PHPDriver; -use Doctrine\Tests\Mocks\ConnectionMock; -use Doctrine\Tests\Mocks\DriverMock; use Doctrine\Tests\Mocks\EntityManagerMock; use Doctrine\Tests\OrmTestCase; use Symfony\Component\Yaml\Parser; @@ -52,15 +52,21 @@ abstract protected function getType(): string; protected function createEntityManager($metadataDriver): EntityManagerMock { - $driverMock = new DriverMock(); - $config = new Configuration(); + $platform = $this->createMock(AbstractPlatform::class); + $platform->method('supportsIdentityColumns') + ->willReturn(true); + + $connection = $this->createMock(Connection::class); + $connection->method('getDatabasePlatform') + ->willReturn($platform); + + $config = new Configuration(); $config->setProxyDir(__DIR__ . '/../../Proxies'); $config->setProxyNamespace('Doctrine\Tests\Proxies'); $eventManager = new EventManager(); - $conn = new ConnectionMock([], $driverMock, $config, $eventManager); $config->setMetadataDriverImpl($metadataDriver); - return EntityManagerMock::create($conn, $config, $eventManager); + return EntityManagerMock::create($connection, $config, $eventManager); } protected function createMetadataDriver(string $type, string $path): MappingDriver diff --git a/tests/Doctrine/Tests/ORM/Tools/Pagination/PaginatorTest.php b/tests/Doctrine/Tests/ORM/Tools/Pagination/PaginatorTest.php index db82f25c94..22381ac98d 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Pagination/PaginatorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Pagination/PaginatorTest.php @@ -5,13 +5,13 @@ namespace Doctrine\Tests\ORM\Tools\Pagination; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Driver; use Doctrine\ORM\Decorator\EntityManagerDecorator; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Internal\Hydration\AbstractHydrator; use Doctrine\ORM\Query; use Doctrine\ORM\Tools\Pagination\Paginator; use Doctrine\Tests\Mocks\ConnectionMock; -use Doctrine\Tests\Mocks\DriverMock; use Doctrine\Tests\OrmTestCase; use PHPUnit\Framework\MockObject\MockObject; @@ -27,7 +27,7 @@ class PaginatorTest extends OrmTestCase protected function setUp(): void { $this->connection = $this->getMockBuilder(ConnectionMock::class) - ->setConstructorArgs([[], new DriverMock()]) + ->setConstructorArgs([[], $this->createMock(Driver::class)]) ->setMethods(['executeQuery']) ->getMock(); diff --git a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php index 0254a467fc..26baa20823 100644 --- a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php +++ b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php @@ -7,6 +7,9 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\Common\EventManager; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Driver; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; use Doctrine\ORM\EntityNotFoundException; use Doctrine\ORM\Events; @@ -25,7 +28,6 @@ use Doctrine\Persistence\NotifyPropertyChanged; use Doctrine\Persistence\PropertyChangedListener; use Doctrine\Tests\Mocks\ConnectionMock; -use Doctrine\Tests\Mocks\DriverMock; use Doctrine\Tests\Mocks\EntityManagerMock; use Doctrine\Tests\Mocks\EntityPersisterMock; use Doctrine\Tests\Mocks\UnitOfWorkMock; @@ -43,6 +45,7 @@ use function count; use function gc_collect_cycles; use function get_class; +use function method_exists; use function random_int; use function uniqid; @@ -80,9 +83,40 @@ class UnitOfWorkTest extends OrmTestCase protected function setUp(): void { parent::setUp(); - $this->_connectionMock = new ConnectionMock([], new DriverMock()); + + $platform = $this->createMock(AbstractPlatform::class); + $platform->method('supportsIdentityColumns') + ->willReturn(true); + + if (method_exists($platform, 'getSQLResultCasing')) { + $platform->method('getSQLResultCasing') + ->willReturnCallback(static function (string $column): string { + return $column; + }); + } + + $driverStatement = $this->createMock(Driver\Statement::class); + + if (method_exists($driverStatement, 'rowCount')) { + $driverStatement->method('rowCount') + ->willReturn(0); + } + + $driverConnection = $this->createMock(Driver\Connection::class); + $driverConnection->method('prepare') + ->willReturn($driverStatement); + + $driver = $this->createMock(Driver::class); + $driver->method('getDatabasePlatform') + ->willReturn($platform); + $driver->method('connect') + ->willReturn($driverConnection); + + $connection = new Connection([], $driver); + + $this->_connectionMock = $connection; $this->eventManager = $this->getMockBuilder(EventManager::class)->getMock(); - $this->_emMock = EntityManagerMock::create($this->_connectionMock, null, $this->eventManager); + $this->_emMock = EntityManagerMock::create($connection, null, $this->eventManager); // SUT $this->_unitOfWork = new UnitOfWorkMock($this->_emMock); $this->_emMock->setUnitOfWork($this->_unitOfWork); @@ -809,9 +843,13 @@ public function testPreviousDetectedIllegalNewNonCascadedEntitiesAreCleanedUpOnS */ public function testCommitThrowOptimisticLockExceptionWhenConnectionCommitReturnFalse(): void { + $driver = $this->createMock(Driver::class); + $driver->method('connect') + ->willReturn($this->createMock(Driver\Connection::class)); + // Set another connection mock that fail on commit $this->_connectionMock = $this->getMockBuilder(ConnectionMock::class) - ->setConstructorArgs([[], new DriverMock()]) + ->setConstructorArgs([[], $driver]) ->setMethods(['commit']) ->getMock(); $this->_emMock = EntityManagerMock::create($this->_connectionMock, null, $this->eventManager);