Skip to content

Commit d2cbab2

Browse files
committed
Remove calls to getMockForAbstractClass()
1 parent 7155010 commit d2cbab2

File tree

6 files changed

+55
-43
lines changed

6 files changed

+55
-43
lines changed

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@
234234
<!-- extending a class from another package -->
235235
<exclude-pattern>tests/Tests/Mocks/DatabasePlatformMock.php</exclude-pattern>
236236
<exclude-pattern>tests/Tests/Mocks/SchemaManagerMock.php</exclude-pattern>
237+
<exclude-pattern>tests/Tests/ORM/AbstractQueryTest.php</exclude-pattern>
237238
<exclude-pattern>tests/Tests/ORM/Functional/Ticket/DDC3634Test.php</exclude-pattern>
238239
</rule>
239240

tests/Tests/ORM/AbstractQueryTest.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ public function testItMakesHydrationCacheProfilesAwareOfTheResultCacheDriver():
2727

2828
$configuration = new Configuration();
2929
$configuration->setHydrationCache($cache);
30-
$entityManager = $this->createMock(EntityManagerInterface::class);
31-
$entityManager->method('getConfiguration')->willReturn($configuration);
32-
$query = $this->getMockForAbstractClass(AbstractQuery::class, [$entityManager]);
30+
$query = $this->createAbstractQuery($configuration);
3331
$cacheProfile = new QueryCacheProfile();
3432

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

4644
$configuration = new Configuration();
4745
$configuration->setHydrationCache($cache);
48-
$entityManager = $this->createMock(EntityManagerInterface::class);
49-
$entityManager->method('getConfiguration')->willReturn($configuration);
50-
$query = $this->getMockForAbstractClass(AbstractQuery::class, [$entityManager]);
46+
$query = $this->createAbstractQuery($configuration);
5147
$cacheProfile = new QueryCacheProfile();
5248

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

6258
$configuration = new Configuration();
6359
$configuration->setResultCache($cache);
64-
$entityManager = $this->createMock(EntityManagerInterface::class);
65-
$entityManager->method('getConfiguration')->willReturn($configuration);
66-
$query = $this->getMockForAbstractClass(AbstractQuery::class, [$entityManager]);
60+
$query = $this->createAbstractQuery($configuration);
6761
$cacheProfile = new QueryCacheProfile();
6862

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

8173
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/9791');
8274
$query->$setter();
@@ -95,10 +87,7 @@ public static function provideSettersWithDeprecatedDefault(): array
9587
public function testSettingTheResultCacheIsPossibleWithoutCallingDeprecatedMethods(): void
9688
{
9789
$cache = $this->createMock(CacheItemPoolInterface::class);
98-
99-
$entityManager = $this->createMock(EntityManagerInterface::class);
100-
$entityManager->method('getConfiguration')->willReturn(new Configuration());
101-
$query = $this->getMockForAbstractClass(AbstractQuery::class, [$entityManager]);
90+
$query = $this->createAbstractQuery(new Configuration());
10291

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

10897
public function testSettingTheFetchModeToRandomIntegersIsDeprecated(): void
10998
{
110-
$query = $this->getMockForAbstractClass(
111-
AbstractQuery::class,
112-
[],
113-
'',
114-
false // no need to call the constructor
115-
);
99+
$query = $this->createAbstractQuery(new Configuration());
100+
116101
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/9777');
117102
$query->setFetchMode(stdClass::class, 'foo', 42);
118103
}
104+
105+
private function createAbstractQuery(Configuration $configuration): AbstractQuery
106+
{
107+
$entityManager = $this->createMock(EntityManagerInterface::class);
108+
$entityManager->method('getConfiguration')->willReturn($configuration);
109+
110+
return new class ($entityManager) extends AbstractQuery {
111+
public function getSQL(): string
112+
{
113+
return '';
114+
}
115+
116+
protected function _doExecute(): int
117+
{
118+
return 0;
119+
}
120+
};
121+
}
119122
}

tests/Tests/ORM/Functional/QueryCacheTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\Tests\ORM\Functional;
66

7+
use Doctrine\DBAL\Connection;
78
use Doctrine\ORM\Query;
89
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
910
use Doctrine\ORM\Query\ParserResult;
@@ -121,12 +122,12 @@ public function testQueryCacheHitDoesNotSaveParserResult(): void
121122

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

124-
$sqlExecMock = $this->getMockBuilder(AbstractSqlExecutor::class)
125-
->getMockForAbstractClass();
126-
127-
$sqlExecMock->expects(self::once())
128-
->method('execute')
129-
->willReturn(10);
125+
$sqlExecMock = new class extends AbstractSqlExecutor {
126+
public function execute(Connection $conn, array $params, array $types): int
127+
{
128+
return 10;
129+
}
130+
};
130131

131132
$parserResultMock = new ParserResult();
132133
$parserResultMock->setSqlExecutor($sqlExecMock);

tests/Tests/ORM/Hydration/AbstractHydratorTest.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doctrine\DBAL\Result;
1010
use Doctrine\ORM\EntityManagerInterface;
1111
use Doctrine\ORM\Events;
12+
use Doctrine\ORM\Exception\NotSupported;
1213
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
1314
use Doctrine\ORM\ORMException;
1415
use Doctrine\ORM\Query\ResultSetMapping;
@@ -30,7 +31,7 @@ class AbstractHydratorTest extends OrmFunctionalTestCase
3031
/** @var ResultSetMapping&MockObject */
3132
private $mockResultMapping;
3233

33-
/** @var AbstractHydrator&MockObject */
34+
/** @var DummyHydrator */
3435
private $hydrator;
3536

3637
protected function setUp(): void
@@ -56,10 +57,7 @@ protected function setUp(): void
5657
->method('fetchAssociative')
5758
->willReturn(false);
5859

59-
$this->hydrator = $this
60-
->getMockBuilder(AbstractHydrator::class)
61-
->setConstructorArgs([$mockEntityManagerInterface])
62-
->getMockForAbstractClass();
60+
$this->hydrator = new DummyHydrator($mockEntityManagerInterface);
6361
}
6462

6563
/**
@@ -146,11 +144,7 @@ public function testHydrateAllClearsAllAttachedListenersEvenOnError(): void
146144
$this->assertTrue($eventListenerHasBeenRegistered);
147145
});
148146

149-
$this
150-
->hydrator
151-
->expects(self::once())
152-
->method('hydrateAllData')
153-
->willThrowException(new ORMException());
147+
$this->hydrator->throwException = true;
154148

155149
$this->expectException(ORMException::class);
156150
$this->hydrator->hydrateAll($this->mockResult, $this->mockResultMapping);
@@ -185,3 +179,19 @@ public function testToIterableIfYieldAndBreakBeforeFinishAlwaysCleansUp(): void
185179
self::assertCount(0, $evm->getListeners(Events::onClear));
186180
}
187181
}
182+
183+
class DummyHydrator extends AbstractHydrator
184+
{
185+
/** @var bool */
186+
public $throwException = false;
187+
188+
/** {@inheritDoc} */
189+
protected function hydrateAllData()
190+
{
191+
if ($this->throwException) {
192+
throw NotSupported::create();
193+
}
194+
195+
return [];
196+
}
197+
}

tests/Tests/ORM/Mapping/DefaultQuoteStrategyTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
use Doctrine\Tests\Models\NonPublicSchemaJoins\User as NonPublicSchemaUser;
1010
use Doctrine\Tests\OrmTestCase;
1111

12-
use function assert;
13-
1412
/**
1513
* Doctrine\Tests\ORM\Mapping\DefaultQuoteStrategyTest
1614
*/
@@ -25,8 +23,7 @@ public function testGetJoinTableName(): void
2523
$em = $this->getTestEntityManager();
2624
$metadata = $em->getClassMetadata(NonPublicSchemaUser::class);
2725
$strategy = new DefaultQuoteStrategy();
28-
$platform = $this->getMockForAbstractClass(AbstractPlatform::class);
29-
assert($platform instanceof AbstractPlatform);
26+
$platform = self::createStub(AbstractPlatform::class);
3027

3128
self::assertSame(
3229
'readers.author_reader',

tests/Tests/ORM/Query/ParserResultTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testSetGetSqlExecutor(): void
2828
{
2929
self::assertNull($this->parserResult->getSqlExecutor());
3030

31-
$executor = $this->getMockForAbstractClass(AbstractSqlExecutor::class);
31+
$executor = self::createStub(AbstractSqlExecutor::class);
3232
$this->parserResult->setSqlExecutor($executor);
3333
self::assertSame($executor, $this->parserResult->getSqlExecutor());
3434
}

0 commit comments

Comments
 (0)