Skip to content

Remove calls to getMockForAbstractClass() #12003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.20.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
<!-- extending a class from another package -->
<exclude-pattern>tests/Tests/Mocks/DatabasePlatformMock.php</exclude-pattern>
<exclude-pattern>tests/Tests/Mocks/SchemaManagerMock.php</exclude-pattern>
<exclude-pattern>tests/Tests/ORM/AbstractQueryTest.php</exclude-pattern>
<exclude-pattern>tests/Tests/ORM/Functional/Ticket/DDC3634Test.php</exclude-pattern>
</rule>

Expand Down
47 changes: 25 additions & 22 deletions tests/Tests/ORM/AbstractQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public function testItMakesHydrationCacheProfilesAwareOfTheResultCacheDriver():

$configuration = new Configuration();
$configuration->setHydrationCache($cache);
$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->method('getConfiguration')->willReturn($configuration);
$query = $this->getMockForAbstractClass(AbstractQuery::class, [$entityManager]);
$query = $this->createAbstractQuery($configuration);
$cacheProfile = new QueryCacheProfile();

$query->setHydrationCacheProfile($cacheProfile);
Expand All @@ -45,9 +43,7 @@ public function testItMakesHydrationCacheProfilesAwareOfTheResultCache(): void

$configuration = new Configuration();
$configuration->setHydrationCache($cache);
$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->method('getConfiguration')->willReturn($configuration);
$query = $this->getMockForAbstractClass(AbstractQuery::class, [$entityManager]);
$query = $this->createAbstractQuery($configuration);
$cacheProfile = new QueryCacheProfile();

$query->setHydrationCacheProfile($cacheProfile);
Expand All @@ -61,9 +57,7 @@ public function testItMakesResultCacheProfilesAwareOfTheResultCache(): void

$configuration = new Configuration();
$configuration->setResultCache($cache);
$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->method('getConfiguration')->willReturn($configuration);
$query = $this->getMockForAbstractClass(AbstractQuery::class, [$entityManager]);
$query = $this->createAbstractQuery($configuration);
$cacheProfile = new QueryCacheProfile();

$query->setResultCacheProfile($cacheProfile);
Expand All @@ -74,9 +68,7 @@ public function testItMakesResultCacheProfilesAwareOfTheResultCache(): void
/** @dataProvider provideSettersWithDeprecatedDefault */
public function testCallingSettersWithoutArgumentsIsDeprecated(string $setter): void
{
$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->method('getConfiguration')->willReturn(new Configuration());
$query = $this->getMockForAbstractClass(AbstractQuery::class, [$entityManager]);
$query = $this->createAbstractQuery(new Configuration());

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/9791');
$query->$setter();
Expand All @@ -95,10 +87,7 @@ public static function provideSettersWithDeprecatedDefault(): array
public function testSettingTheResultCacheIsPossibleWithoutCallingDeprecatedMethods(): void
{
$cache = $this->createMock(CacheItemPoolInterface::class);

$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->method('getConfiguration')->willReturn(new Configuration());
$query = $this->getMockForAbstractClass(AbstractQuery::class, [$entityManager]);
$query = $this->createAbstractQuery(new Configuration());

$query->setResultCache($cache);
self::assertSame($cache, CacheAdapter::wrap($query->getResultCacheDriver()));
Expand All @@ -107,13 +96,27 @@ public function testSettingTheResultCacheIsPossibleWithoutCallingDeprecatedMetho

public function testSettingTheFetchModeToRandomIntegersIsDeprecated(): void
{
$query = $this->getMockForAbstractClass(
AbstractQuery::class,
[],
'',
false // no need to call the constructor
);
$query = $this->createAbstractQuery(new Configuration());

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/9777');
$query->setFetchMode(stdClass::class, 'foo', 42);
}

private function createAbstractQuery(Configuration $configuration): AbstractQuery
{
$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->method('getConfiguration')->willReturn($configuration);

return new class ($entityManager) extends AbstractQuery {
public function getSQL(): string
{
return '';
}

protected function _doExecute(): int
{
return 0;
}
};
}
}
13 changes: 7 additions & 6 deletions tests/Tests/ORM/Functional/QueryCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\DBAL\Connection;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
use Doctrine\ORM\Query\ParserResult;
Expand Down Expand Up @@ -121,12 +122,12 @@ public function testQueryCacheHitDoesNotSaveParserResult(): void

$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');

$sqlExecMock = $this->getMockBuilder(AbstractSqlExecutor::class)
->getMockForAbstractClass();

$sqlExecMock->expects(self::once())
->method('execute')
->willReturn(10);
$sqlExecMock = new class extends AbstractSqlExecutor {
public function execute(Connection $conn, array $params, array $types): int
{
return 10;
}
};

$parserResultMock = new ParserResult();
$parserResultMock->setSqlExecutor($sqlExecMock);
Expand Down
30 changes: 20 additions & 10 deletions tests/Tests/ORM/Hydration/AbstractHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\DBAL\Result;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\ORM\Exception\NotSupported;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\Query\ResultSetMapping;
Expand All @@ -30,7 +31,7 @@ class AbstractHydratorTest extends OrmFunctionalTestCase
/** @var ResultSetMapping&MockObject */
private $mockResultMapping;

/** @var AbstractHydrator&MockObject */
/** @var DummyHydrator */
private $hydrator;

protected function setUp(): void
Expand All @@ -56,10 +57,7 @@ protected function setUp(): void
->method('fetchAssociative')
->willReturn(false);

$this->hydrator = $this
->getMockBuilder(AbstractHydrator::class)
->setConstructorArgs([$mockEntityManagerInterface])
->getMockForAbstractClass();
$this->hydrator = new DummyHydrator($mockEntityManagerInterface);
}

/**
Expand Down Expand Up @@ -146,11 +144,7 @@ public function testHydrateAllClearsAllAttachedListenersEvenOnError(): void
$this->assertTrue($eventListenerHasBeenRegistered);
});

$this
->hydrator
->expects(self::once())
->method('hydrateAllData')
->willThrowException(new ORMException());
$this->hydrator->throwException = true;

$this->expectException(ORMException::class);
$this->hydrator->hydrateAll($this->mockResult, $this->mockResultMapping);
Expand Down Expand Up @@ -185,3 +179,19 @@ public function testToIterableIfYieldAndBreakBeforeFinishAlwaysCleansUp(): void
self::assertCount(0, $evm->getListeners(Events::onClear));
}
}

class DummyHydrator extends AbstractHydrator
{
/** @var bool */
public $throwException = false;

/** {@inheritDoc} */
protected function hydrateAllData()
{
if ($this->throwException) {
throw NotSupported::create();
}

return [];
}
}
5 changes: 1 addition & 4 deletions tests/Tests/ORM/Mapping/DefaultQuoteStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Doctrine\Tests\Models\NonPublicSchemaJoins\User as NonPublicSchemaUser;
use Doctrine\Tests\OrmTestCase;

use function assert;

/**
* Doctrine\Tests\ORM\Mapping\DefaultQuoteStrategyTest
*/
Expand All @@ -25,8 +23,7 @@ public function testGetJoinTableName(): void
$em = $this->getTestEntityManager();
$metadata = $em->getClassMetadata(NonPublicSchemaUser::class);
$strategy = new DefaultQuoteStrategy();
$platform = $this->getMockForAbstractClass(AbstractPlatform::class);
assert($platform instanceof AbstractPlatform);
$platform = self::createStub(AbstractPlatform::class);

self::assertSame(
'readers.author_reader',
Expand Down
2 changes: 1 addition & 1 deletion tests/Tests/ORM/Query/ParserResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testSetGetSqlExecutor(): void
{
self::assertNull($this->parserResult->getSqlExecutor());

$executor = $this->getMockForAbstractClass(AbstractSqlExecutor::class);
$executor = self::createStub(AbstractSqlExecutor::class);
$this->parserResult->setSqlExecutor($executor);
self::assertSame($executor, $this->parserResult->getSqlExecutor());
}
Expand Down
Loading