Skip to content

Commit

Permalink
Merge branch '2.12.x' into 3.0.x
Browse files Browse the repository at this point in the history
* 2.12.x:
  Fix types on CacheLogger implementations (doctrine#9401)
  Rework some tests that use hardcoded DBAL mocks (doctrine#9404)
  • Loading branch information
derrabus committed Jan 20, 2022
2 parents d4f5db4 + f7822c7 commit 7c17c11
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 58 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,25 +13,19 @@
*/
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 supportsSequences(): bool
{
return $this->supportsSequences;
return false;
}

/**
Expand Down Expand Up @@ -93,16 +87,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
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
46 changes: 42 additions & 4 deletions tests/Doctrine/Tests/ORM/UnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -802,9 +836,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);
Expand Down

0 comments on commit 7c17c11

Please sign in to comment.