Skip to content

Commit

Permalink
Rework some tests that use hardcoded DBAL mocks (#9404)
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov authored Jan 19, 2022
1 parent 026bba2 commit 8c08792
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 70 deletions.
20 changes: 2 additions & 18 deletions tests/Doctrine/Tests/Mocks/DatabasePlatformMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,14 @@
*/
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.');
}

public function supportsIdentityColumns(): bool
{
return $this->supportsIdentityColumns;
return true;
}

public function prefersSequences(): bool
Expand All @@ -36,7 +30,7 @@ public function prefersSequences(): bool

public function supportsSequences(): bool
{
return $this->supportsSequences;
return false;
}

/**
Expand Down Expand Up @@ -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.');
Expand Down
14 changes: 11 additions & 3 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH7869Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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])
Expand Down
51 changes: 31 additions & 20 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 20 additions & 3 deletions tests/Doctrine/Tests/ORM/PersistentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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();
}
Expand Down
23 changes: 15 additions & 8 deletions tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,8 +30,8 @@
*/
class ProxyFactoryTest extends OrmTestCase
{
/** @var ConnectionMock */
private $connectionMock;
/** @var Connection */
private $connection;

/** @var UnitOfWorkMock */
private $uowMock;
Expand All @@ -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);
}
Expand Down
19 changes: 13 additions & 6 deletions tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/ORM/Tools/Pagination/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();

Expand Down
Loading

0 comments on commit 8c08792

Please sign in to comment.