Skip to content

Commit 2150ce8

Browse files
authored
Make PHPUnit more strict and verbose, and fix issues (#2873)
* Trigger deprecation on not using native lazy objects only when accessing the value * Fix access to deprecated reflProperties in ClassMetadataTest And fix assertNotEmpty for array * Restore the default map of types to cancel side-effects * Remove usage of deprecated Aggregation\Builder::execute() and test it aside * Deprecation messages are expected for the CHANGETRACKING_NOTIFY feature * Replace deprecated type 'boolean' with 'bool' * Ignore deprecation on PrimingIteratorTest that uses NOTIFY tracking policy * Replace partial clear by explicit detach, and add a test on UoW::clear($class) * Don't use clear by class name when not necessary * Avoid call to getAllMetadata() since ProfileNotify uses deprecated NOTIFY tracking policy * Ignore deprecation on NOTIFY change tracker * Replace deprected @indexes annotation, already covered by DeprecatedIndexesPropertyAnnotation * Make PHPUnit more strict and verbose * Do not fail on deprecation Currently jmikola/geojson: 1.0.0 triggers PHP deprecation on Return type of GeoJson\GeoJson::jsonSerialize()
1 parent b49d449 commit 2150ce8

23 files changed

+121
-66
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ jobs:
166166
topology: ${{ matrix.topology }}
167167

168168
- name: "Run PHPUnit"
169-
run: "vendor/bin/phpunit --exclude-group=atlas"
169+
run: "vendor/bin/phpunit --exclude-group=atlas ${{ matrix.dependencies == 'lowest' && '--do-not-fail-on-deprecation --do-not-fail-on-warning --do-not-fail-on-notice' || '' }}"
170170
env:
171171
DOCTRINE_MONGODB_SERVER: ${{ steps.setup-mongodb.outputs.cluster-uri }}
172172
USE_LAZY_GHOST_OBJECT: ${{ matrix.proxy == 'lazy-ghost' && '1' || '0' }}

phpunit.xml.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
55
bootstrap="tests/bootstrap.php"
66
cacheDirectory=".phpunit.cache"
7+
displayDetailsOnAllIssues="true"
8+
failOnPhpunitDeprecation="true"
9+
failOnPhpunitWarning="true"
10+
failOnDeprecation="true"
11+
failOnNotice="true"
12+
failOnWarning="true"
13+
failOnIncomplete="true"
714
failOnRisky="true"
15+
failOnEmptyTestSuite="true"
816
>
917
<testsuites>
1018
<testsuite name="Doctrine ODM MongoDB Test Suite">

src/Configuration.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,10 +703,6 @@ public function setUseLazyGhostObject(bool $flag): void
703703
trigger_deprecation('doctrine/mongodb-odm', '2.10', 'Using "friendsofphp/proxy-manager-lts" is deprecated. Use "symfony/var-exporter" LazyGhostObjects instead.');
704704
}
705705

706-
if ($flag === true && PHP_VERSION_ID >= 80400) {
707-
trigger_deprecation('doctrine/mongodb-odm', '2.14', 'Using "symfony/var-exporter" lazy ghost objects is deprecated and will be impossible in Doctrine MongoDB ODM 3.0.');
708-
}
709-
710706
$this->lazyGhostObject = $flag;
711707
}
712708

tests/Documentation/CustomMapping/CustomMappingTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@
88
use DateTimeZone;
99
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;
1010
use Doctrine\ODM\MongoDB\Types\Type;
11+
use PHPUnit\Framework\Attributes\After;
12+
use ReflectionProperty;
1113

1214
class CustomMappingTest extends BaseTestCase
1315
{
16+
#[After]
17+
public function restoreTypeMap(): void
18+
{
19+
$r = new ReflectionProperty(Type::class, 'typesMap');
20+
$r->setValue(null, $r->getDefaultValue());
21+
}
22+
1423
public function testTest(): void
1524
{
1625
Type::addType('date_with_timezone', DateTimeWithTimezoneType::class);

tests/Tests/Aggregation/BuilderTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DateTimeImmutable;
88
use Doctrine\ODM\MongoDB\Aggregation\Aggregation;
99
use Doctrine\ODM\MongoDB\Aggregation\Stage;
10+
use Doctrine\ODM\MongoDB\Iterator\CachingIterator;
1011
use Doctrine\ODM\MongoDB\Iterator\Iterator;
1112
use Doctrine\ODM\MongoDB\Iterator\UnrewindableIterator;
1213
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;
@@ -18,6 +19,8 @@
1819
use Documents\Tag;
1920
use MongoDB\BSON\ObjectId;
2021
use MongoDB\BSON\UTCDateTime;
22+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
23+
use PHPUnit\Framework\Attributes\TestWith;
2124

2225
use function array_keys;
2326

@@ -188,6 +191,7 @@ public function testAggregationBuilder(): void
188191
->field('numPosts')
189192
->sum(1)
190193
->sort('numPosts', 'desc')
194+
->getAggregation()
191195
->execute();
192196

193197
self::assertInstanceOf(Iterator::class, $resultCursor);
@@ -437,7 +441,7 @@ public function testBuilderWithOutStageReturnsNoData(): void
437441
$builder
438442
->out('sampleCollection');
439443

440-
$result = $builder->execute()->toArray();
444+
$result = $builder->getAggregation()->getIterator();
441445
self::assertEmpty($result);
442446
}
443447

@@ -464,14 +468,28 @@ public function testEmptyMatchStageIsSkipped(): void
464468
], $builder->getPipeline());
465469
}
466470

471+
#[IgnoreDeprecations]
472+
#[TestWith([false, UnrewindableIterator::class])]
473+
#[TestWith([true, CachingIterator::class])]
474+
public function testExecute(bool $rewindable, string $iteratorClass): void
475+
{
476+
$builder = $this->dm
477+
->createAggregationBuilder(BlogPost::class)
478+
->match()
479+
->rewindable($rewindable);
480+
481+
$iterator = $builder->getAggregation()->execute();
482+
self::assertInstanceOf($iteratorClass, $iterator);
483+
}
484+
467485
public function testNonRewindableBuilder(): void
468486
{
469487
$builder = $this->dm
470488
->createAggregationBuilder(BlogPost::class)
471489
->match()
472490
->rewindable(false);
473491

474-
$iterator = $builder->execute();
492+
$iterator = $builder->getAggregation()->execute();
475493
self::assertInstanceOf(UnrewindableIterator::class, $iterator);
476494
}
477495

tests/Tests/Aggregation/Stage/GraphLookupTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
use function array_merge;
2222
use function count;
23+
use function iterator_to_array;
2324

2425
class GraphLookupTest extends BaseTestCase
2526
{
@@ -172,7 +173,7 @@ public function testWithEmployees(Closure $addGraphLookupStage, array $expectedF
172173

173174
self::assertEquals($expectedPipeline, $builder->getPipeline());
174175

175-
$result = $builder->execute()->toArray();
176+
$result = $builder->getAggregation();
176177

177178
self::assertCount(6, $result);
178179
foreach ($result as $reportingHierarchy) {
@@ -245,7 +246,7 @@ public function testWithTraveller(Closure $addGraphLookupStage, array $expectedF
245246

246247
self::assertEquals($expectedPipeline, $builder->getPipeline());
247248

248-
$result = $builder->execute()->toArray();
249+
$result = iterator_to_array($builder->getAggregation());
249250

250251
self::assertCount(3, $result);
251252
}

tests/Tests/Aggregation/Stage/LookupTest.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Documents\User;
1212
use InvalidArgumentException;
1313

14+
use function iterator_to_array;
15+
1416
class LookupTest extends BaseTestCase
1517
{
1618
public function setUp(): void
@@ -40,7 +42,7 @@ public function testStage(): void
4042

4143
self::assertEquals($expectedPipeline, $builder->getPipeline());
4244

43-
$result = $builder->execute()->toArray();
45+
$result = iterator_to_array($builder->getAggregation());
4446

4547
self::assertCount(1, $result);
4648
self::assertCount(1, $result[0]['user']);
@@ -237,7 +239,7 @@ public function testStageWithClassName(): void
237239

238240
self::assertEquals($expectedPipeline, $builder->getPipeline());
239241

240-
$result = $builder->execute()->toArray();
242+
$result = iterator_to_array($builder->getAggregation());
241243

242244
self::assertCount(1, $result);
243245
self::assertCount(1, $result[0]['user']);
@@ -266,7 +268,7 @@ public function testStageWithCollectionName(): void
266268

267269
self::assertEquals($expectedPipeline, $builder->getPipeline());
268270

269-
$result = $builder->execute()->toArray();
271+
$result = iterator_to_array($builder->getAggregation());
270272

271273
self::assertCount(1, $result);
272274
self::assertEmpty($result[0]['user']);
@@ -294,7 +296,7 @@ public function testStageReferenceMany(): void
294296

295297
self::assertEquals($expectedPipeline, $builder->getPipeline());
296298

297-
$result = $builder->execute()->toArray();
299+
$result = iterator_to_array($builder->getAggregation());
298300

299301
self::assertCount(2, $result);
300302
self::assertCount(1, $result[0]['users']);
@@ -325,7 +327,7 @@ public function testStageReferenceManyStoreAsRef(): void
325327

326328
self::assertEquals($expectedPipeline, $builder->getPipeline());
327329

328-
$result = $builder->execute()->toArray();
330+
$result = iterator_to_array($builder->getAggregation());
329331

330332
self::assertCount(2, $result);
331333
self::assertCount(1, $result[0]['users']);
@@ -360,7 +362,7 @@ public function testStageReferenceOneInverse(): void
360362

361363
self::assertEquals($expectedPipeline, $builder->getPipeline());
362364

363-
$result = $builder->execute()->toArray();
365+
$result = iterator_to_array($builder->getAggregation());
364366

365367
self::assertCount(1, $result);
366368
self::assertCount(1, $result[0]['simpleReferenceOneInverse']);
@@ -392,7 +394,7 @@ public function testStageReferenceManyInverse(): void
392394

393395
self::assertEquals($expectedPipeline, $builder->getPipeline());
394396

395-
$result = $builder->execute()->toArray();
397+
$result = iterator_to_array($builder->getAggregation());
396398

397399
self::assertCount(1, $result);
398400
self::assertCount(1, $result[0]['simpleReferenceManyInverse']);
@@ -424,7 +426,7 @@ public function testStageReferenceOneInverseStoreAsRef(): void
424426

425427
self::assertEquals($expectedPipeline, $builder->getPipeline());
426428

427-
$result = $builder->execute()->toArray();
429+
$result = iterator_to_array($builder->getAggregation());
428430

429431
self::assertCount(1, $result);
430432
self::assertCount(1, $result[0]['embeddedReferenceOneInverse']);
@@ -456,7 +458,7 @@ public function testStageReferenceManyInverseStoreAsRef(): void
456458

457459
self::assertEquals($expectedPipeline, $builder->getPipeline());
458460

459-
$result = $builder->execute()->toArray();
461+
$result = iterator_to_array($builder->getAggregation());
460462

461463
self::assertCount(1, $result);
462464
self::assertCount(1, $result[0]['embeddedReferenceManyInverse']);
@@ -508,7 +510,7 @@ public function testStageAndDefaultAlias(): void
508510

509511
self::assertEquals($expectedPipeline, $builder->getPipeline());
510512

511-
$result = $builder->execute()->toArray();
513+
$result = iterator_to_array($builder->getAggregation());
512514
self::assertCount(1, $result[0]['simpleReferenceOneInverse']);
513515
}
514516

@@ -532,7 +534,7 @@ public function testStageAndDefaultAliasOverride(): void
532534

533535
self::assertEquals($expectedPipeline, $builder->getPipeline());
534536

535-
$result = $builder->execute()->toArray();
537+
$result = iterator_to_array($builder->getAggregation());
536538
self::assertCount(1, $result[0]['override']);
537539
}
538540
}

tests/Tests/Functional/CustomCollectionsTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\ODM\MongoDB\Tests\ClassMetadataTestUtil;
1717
use Documents\File;
1818
use Documents\ProfileNotify;
19+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
1920
use stdClass;
2021

2122
use function assert;
@@ -148,6 +149,8 @@ public function testModifyingCollectionByCustomMethod(): void
148149
self::assertEquals($e1, $d->coll[1]);
149150
}
150151

152+
/** @see ClassMetadata::CHANGETRACKING_NOTIFY */
153+
#[IgnoreDeprecations]
151154
public function testModifyingCollectionInChangeTrackingNotifyDocument(): void
152155
{
153156
$repository = $this->dm->getRepository(File::class);

tests/Tests/Functional/CustomTypeTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Doctrine\ODM\MongoDB\Types\Type;
1212
use Exception;
1313
use PHPUnit\Framework\Attributes\After;
14-
use PHPUnit\Framework\Attributes\Before;
1514
use ReflectionProperty;
1615

1716
use function array_map;
@@ -21,13 +20,9 @@
2120

2221
class CustomTypeTest extends BaseTestCase
2322
{
24-
/** @var string[] */
25-
private array $originalTypeMap = [];
26-
27-
#[Before]
28-
public function backupTypeMap(): void
23+
public function setUp(): void
2924
{
30-
$this->originalTypeMap = (new ReflectionProperty(Type::class, 'typesMap'))->getValue();
25+
parent::setUp();
3126

3227
Type::addType('date_collection', DateCollectionType::class);
3328
Type::addType(Language::class, LanguageType::class);
@@ -36,8 +31,8 @@ public function backupTypeMap(): void
3631
#[After]
3732
public function restoreTypeMap(): void
3833
{
39-
(new ReflectionProperty(Type::class, 'typesMap'))->setValue($this->originalTypeMap);
40-
unset($this->originalTypeMap);
34+
$r = new ReflectionProperty(Type::class, 'typesMap');
35+
$r->setValue(null, $r->getDefaultValue());
4136
}
4237

4338
public function testCustomTypeValueConversions(): void

tests/Tests/Functional/IdTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ public static function getTestIdTypesAndStrategiesData(): array
254254

255255
return [
256256
// boolean
257-
['boolean', 'none', true, true, 'boolean'],
258-
['boolean', 'none', 1, true, 'boolean'],
259-
['boolean', 'none', false, false, 'boolean'],
257+
['bool', 'none', true, true, 'boolean'],
258+
['bool', 'none', 1, true, 'boolean'],
259+
['bool', 'none', false, false, 'boolean'],
260260

261261
// integer
262262
['int', 'none', 0, 0, 'integer'],

0 commit comments

Comments
 (0)