Skip to content

Commit

Permalink
Fix assert not lazy object
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Nov 23, 2024
1 parent a81c08c commit e69cd31
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 64 deletions.
12 changes: 2 additions & 10 deletions tests/Doctrine/ODM/MongoDB/Tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Doctrine\ODM\MongoDB\Tests;

use Composer\InstalledVersions;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver;
Expand Down Expand Up @@ -118,16 +117,9 @@ public static function assertArraySubset(array $subset, array $array, bool $chec
}
}

public static function assertIsLazyObject(object $document): void
public static function isLazyObject(object $document): bool
{
if (InstalledVersions::isInstalled('friendsofphp/proxy-manager')) {
self::logicalOr(
self::isInstanceOf(InternalProxy::class),
self::isInstanceOf(LazyLoadingInterface::class),
)->evaluate($document);
} else {
self::assertInstanceOf(InternalProxy::class, $document);
}
return $document instanceof InternalProxy || $document instanceof LazyLoadingInterface;
}

protected static function createMetadataDriverImpl(): MappingDriver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testGetIdentifierValue(): void

$userTest = $test->getUser();
self::assertEquals($user->getId(), $userTest->getId());
self::assertIsLazyObject($userTest);
self::assertTrue(self::isLazyObject($userTest));
self::assertTrue($this->uow->isUninitializedObject($userTest));

$this->dm->clear();
Expand All @@ -41,7 +41,7 @@ public function testGetIdentifierValue(): void
$foundUser = $test->getUser();
self::assertEquals($user->getId(), $class->getIdentifierValue($user));
self::assertEquals($user->getId(), $class->getFieldValue($foundUser, 'id'));
self::assertIsLazyObject($foundUser);
self::assertTrue(self::isLazyObject($foundUser));
self::assertTrue($this->uow->isUninitializedObject($foundUser));

self::assertEquals('jwage', $foundUser->getUsername());
Expand Down
47 changes: 23 additions & 24 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencePrimerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use DateTime;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Proxy\InternalProxy;
use Doctrine\ODM\MongoDB\Query\Query;
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;
use Doctrine\ODM\MongoDB\Tests\ClassMetadataTestUtil;
Expand Down Expand Up @@ -95,7 +94,7 @@ public function testPrimeReferencesWithDBRefObjects(): void
->field('groups')->prime(true);

foreach ($qb->getQuery() as $user) {
self::assertIsLazyObject($user->getAccount());
self::assertTrue(self::isLazyObject($user->getAccount()));
self::assertFalse($this->uow->isUninitializedObject($user->getAccount()));

self::assertCount(2, $user->getGroups());
Expand All @@ -104,7 +103,7 @@ public function testPrimeReferencesWithDBRefObjects(): void
* initialized, they will not be hydrated as proxy objects.
*/
foreach ($user->getGroups() as $group) {
self::assertNotInstanceOf(InternalProxy::class, $group);
self::assertFalse(self::isLazyObject($group));
self::assertInstanceOf(Group::class, $group);
}
}
Expand Down Expand Up @@ -133,13 +132,13 @@ public function testPrimeReferencesWithSimpleReferences(): void
->field('users')->prime(true);

foreach ($qb->getQuery() as $simpleUser) {
self::assertIsLazyObject($simpleUser->getUser());
self::assertTrue(self::isLazyObject($simpleUser->getUser()));
self::assertFalse($this->uow->isUninitializedObject($simpleUser->getUser()));

self::assertCount(2, $simpleUser->getUsers());

foreach ($simpleUser->getUsers() as $user) {
self::assertNotInstanceOf(InternalProxy::class, $user);
self::assertFalse(self::isLazyObject($user));
self::assertInstanceOf(User::class, $user);
}
}
Expand Down Expand Up @@ -188,20 +187,20 @@ public function testPrimeReferencesNestedInNamedEmbeddedReference(): void
->field('embeddedDocs.referencedDocs')->prime(true);

foreach ($qb->getQuery() as $root) {
self::assertNotInstanceOf(InternalProxy::class, $root->embeddedDoc);
self::assertFalse(self::isLazyObject($root->embeddedDoc));
self::assertInstanceOf(EmbeddedWhichReferences::class, $root->embeddedDoc);

self::assertCount(2, $root->embeddedDocs);
foreach ($root->embeddedDocs as $embeddedDoc) {
self::assertNotInstanceOf(InternalProxy::class, $embeddedDoc);
self::assertFalse(self::isLazyObject($embeddedDoc));
self::assertInstanceOf(EmbeddedWhichReferences::class, $embeddedDoc);

self::assertIsLazyObject($embeddedDoc->referencedDoc);
self::assertTrue(self::isLazyObject($embeddedDoc->referencedDoc));
self::assertFalse($this->uow->isUninitializedObject($embeddedDoc->referencedDoc));

self::assertCount(2, $embeddedDoc->referencedDocs);
foreach ($embeddedDoc->referencedDocs as $referencedDoc) {
self::assertNotInstanceOf(InternalProxy::class, $referencedDoc);
self::assertFalse(self::isLazyObject($referencedDoc));
self::assertInstanceOf(Reference::class, $referencedDoc);
}
}
Expand Down Expand Up @@ -252,37 +251,37 @@ public function testPrimeReferencesWithDifferentStoreAsReferences(): void
assert($referenceUser instanceof ReferenceUser);
$user = $referenceUser->getUser();
self::assertInstanceOf(User::class, $user);
self::assertIsLazyObject($user);
self::assertTrue(self::isLazyObject($user));
self::assertFalse($this->uow->isUninitializedObject($user));

self::assertCount(1, $referenceUser->getUsers());

foreach ($referenceUser->getUsers() as $user) {
self::assertNotInstanceOf(InternalProxy::class, $user);
self::assertFalse(self::isLazyObject($user));
self::assertInstanceOf(User::class, $user);
}

$parentUser = $referenceUser->getParentUser();
self::assertIsLazyObject($parentUser);
self::assertTrue(self::isLazyObject($parentUser));
self::assertInstanceOf(User::class, $parentUser);
self::assertFalse($this->uow->isUninitializedObject($parentUser));

self::assertCount(1, $referenceUser->getParentUsers());

foreach ($referenceUser->getParentUsers() as $user) {
self::assertNotInstanceOf(InternalProxy::class, $user);
self::assertFalse(self::isLazyObject($user));
self::assertInstanceOf(User::class, $user);
}

$otherUser = $referenceUser->getOtherUser();
self::assertInstanceOf(User::class, $otherUser);
self::assertIsLazyObject($otherUser);
self::assertTrue(self::isLazyObject($otherUser));
self::assertFalse($this->uow->isUninitializedObject($otherUser));

self::assertCount(1, $referenceUser->getOtherUsers());

foreach ($referenceUser->getOtherUsers() as $user) {
self::assertNotInstanceOf(InternalProxy::class, $user);
self::assertFalse(self::isLazyObject($user));
self::assertInstanceOf(User::class, $user);
}
}
Expand All @@ -309,10 +308,10 @@ public function testPrimeReferencesWithDiscriminatedReferenceMany(): void
foreach ($qb->getQuery() as $user) {
$favorites = $user->getFavorites()->toArray();

self::assertNotInstanceOf(InternalProxy::class, $favorites[0]);
self::assertFalse(self::isLazyObject($favorites[0]));
self::assertInstanceOf(Group::class, $favorites[0]);

self::assertNotInstanceOf(InternalProxy::class, $favorites[1]);
self::assertFalse(self::isLazyObject($favorites[1]));
self::assertInstanceOf(Project::class, $favorites[1]);
}
}
Expand All @@ -331,7 +330,7 @@ public function testPrimeReferencesWithDiscriminatedReferenceOne(): void
->field('server')->prime(true);

foreach ($qb->getQuery() as $agent) {
self::assertIsLazyObject($agent->server);
self::assertTrue(self::isLazyObject($agent->server));
self::assertFalse($this->uow->isUninitializedObject($agent->server));
}
}
Expand Down Expand Up @@ -360,7 +359,7 @@ public function testPrimeReferencesIgnoresInitializedProxyObjects(): void
self::assertCount(2, $user->getGroups());

foreach ($user->getGroups() as $group) {
self::assertNotInstanceOf(InternalProxy::class, $group);
self::assertFalse(self::isLazyObject($group));
self::assertInstanceOf(Group::class, $group);
}
}
Expand Down Expand Up @@ -440,7 +439,7 @@ public function testPrimeReferencesInFindAndModifyResult(): void
self::assertCount(1, $user->getGroups());

foreach ($user->getGroups() as $group) {
self::assertNotInstanceOf(InternalProxy::class, $group);
self::assertFalse(self::isLazyObject($group));
self::assertInstanceOf(Group::class, $group);
}
}
Expand Down Expand Up @@ -472,7 +471,7 @@ public function testPrimeEmbeddedReferenceOneLevelDeep(): void

$phonenumber = $phonenumbers->current();

self::assertNotInstanceOf(InternalProxy::class, $phonenumber);
self::assertFalse(self::isLazyObject($phonenumber));
self::assertInstanceOf(Phonenumber::class, $phonenumber);
}

Expand Down Expand Up @@ -523,7 +522,7 @@ public function testPrimeEmbeddedReferenceTwoLevelsDeep(): void

$currency = $money->getCurrency();

self::assertIsLazyObject($currency);
self::assertTrue(self::isLazyObject($currency));
self::assertInstanceOf(Currency::class, $currency);
self::assertFalse($this->uow->isUninitializedObject($currency));
}
Expand Down Expand Up @@ -551,7 +550,7 @@ public function testPrimeReferencesInReferenceMany(): void
self::assertInstanceOf(BlogPost::class, $post);

$comment = $post->comments->first();
self::assertIsLazyObject($comment->author);
self::assertTrue(self::isLazyObject($comment->author));
self::assertFalse($this->uow->isUninitializedObject($comment->author));
}

Expand All @@ -578,7 +577,7 @@ public function testPrimeReferencesInReferenceManyWithRepositoryMethodEager(): v
self::assertInstanceOf(BlogPost::class, $post);

$comment = $post->repoCommentsWithPrimer->first();
self::assertIsLazyObject($comment->author);
self::assertTrue(self::isLazyObject($comment->author));
self::assertFalse($this->uow->isUninitializedObject($comment->author));
}
}
12 changes: 6 additions & 6 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function testLazyLoadReference(): void
assert($profile instanceof Profile);

self::assertInstanceOf(Profile::class, $profile);
self::assertIsLazyObject($profile);
self::assertTrue(self::isLazyObject($profile));

$profile->getFirstName();

Expand All @@ -102,7 +102,7 @@ public function testLazyLoadedWithNotifyPropertyChanged(): void

$user = $this->dm->find($user::class, $user->getId());
$profile = $user->getProfileNotify();
self::assertIsLazyObject($profile);
self::assertTrue(self::isLazyObject($profile));
self::assertTrue($this->uow->isUninitializedObject($profile));

$user->getProfileNotify()->setLastName('Malarz');
Expand Down Expand Up @@ -394,7 +394,7 @@ public function testDocumentNotFoundExceptionWithArrayId(): void
);

$test = $this->dm->find($test::class, $test->id);
self::assertIsLazyObject($test->referenceOne);
self::assertTrue(self::isLazyObject($test->referenceOne));
$this->expectException(DocumentNotFoundException::class);
$this->expectExceptionMessage(
'The "Doctrine\ODM\MongoDB\Tests\Functional\DocumentWithArrayId" document with identifier ' .
Expand Down Expand Up @@ -427,7 +427,7 @@ public function testDocumentNotFoundExceptionWithObjectId(): void

$user = $this->dm->find($user::class, $user->getId());
$profile = $user->getProfile();
self::assertIsLazyObject($profile);
self::assertTrue(self::isLazyObject($profile));
$this->expectException(DocumentNotFoundException::class);
$this->expectExceptionMessage(
'The "Documents\Profile" document with identifier "abcdefabcdefabcdefabcdef" could not be found.',
Expand Down Expand Up @@ -458,7 +458,7 @@ public function testDocumentNotFoundExceptionWithMongoBinDataId(): void
);

$test = $this->dm->find($test::class, $test->id);
self::assertIsLazyObject($test->referenceOne);
self::assertTrue(self::isLazyObject($test->referenceOne));
$this->expectException(DocumentNotFoundException::class);
$this->expectExceptionMessage(
'The "Doctrine\ODM\MongoDB\Tests\Functional\DocumentWithMongoBinDataId" document with identifier ' .
Expand Down Expand Up @@ -500,7 +500,7 @@ public function testDocumentNotFoundEvent(): void

$this->dm->getEventManager()->addEventListener(Events::documentNotFound, new DocumentNotFoundListener($closure));

self::assertIsLazyObject($profile);
self::assertTrue(self::isLazyObject($profile));
$this->uow->initializeObject($profile);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testProxy(): void
$user = $test->getUser();
self::assertNotNull($user);
self::assertInstanceOf(User::class, $user);
self::assertIsLazyObject($user);
self::assertTrue(self::isLazyObject($user));
self::assertTrue($this->uow->isUninitializedObject($user));
self::assertEquals('jwage', $user->getUsername());
self::assertFalse($this->uow->isUninitializedObject($user));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testPrimeWithGetSingleResult(): void
$document = $query->getSingleResult();

self::assertInstanceOf(GH520Document::class, $document);
self::assertIsLazyObject($document->ref);
self::assertTrue(self::isLazyObject($document->ref));
self::assertFalse($this->uow->isUninitializedObject($document->ref));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public function testReferenceManyOwningSidePreparesFilterCriteria(): void
*/
self::assertCount(2, $user1following);

self::assertIsLazyObject($user1following[0]);
self::assertTrue(self::isLazyObject($user1following[0]));
self::assertFalse($this->uow->isUninitializedObject($user1following[0]));
self::assertEquals($user2->getId(), $user1following[0]->getId());

self::assertIsLazyObject($user1following[1]);
self::assertTrue(self::isLazyObject($user1following[1]));
self::assertTrue($this->uow->isUninitializedObject($user1following[1]));
self::assertEquals($user3->getId(), $user1following[1]->getId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public function testReferenceManyOwningSidePreparesFilterCriteriaForDifferentCla
*/
self::assertCount(2, $user1likes);

self::assertIsLazyObject($user1likes[0]);
self::assertTrue(self::isLazyObject($user1likes[0]));
self::assertFalse($this->uow->isUninitializedObject($user1likes[0]));
self::assertEquals($thing1->getId(), $user1likes[0]->getId());

self::assertIsLazyObject($user1likes[1]);
self::assertTrue(self::isLazyObject($user1likes[1]));
self::assertTrue($this->uow->isUninitializedObject($user1likes[1]));
self::assertEquals($thing2->getId(), $user1likes[1]->getId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testA(Closure $idGenerator): void
self::assertEquals($idGenerator('parent'), $parent->id);
self::assertEquals('parent', $parent->name);

self::assertIsLazyObject($parent->refOne);
self::assertTrue(self::isLazyObject($parent->refOne));
self::assertInstanceOf(GH852Document::class, $parent->refOne);
self::assertTrue($this->uow->isUninitializedObject($parent->refOne));
self::assertEquals($idGenerator('childA'), $parent->refOne->id);
Expand All @@ -60,13 +60,13 @@ public function testA(Closure $idGenerator): void
/* These proxies will be initialized when we first access the collection
* by DocumentPersister::loadReferenceManyCollectionOwningSide().
*/
self::assertIsLazyObject($parent->refMany[0]);
self::assertTrue(self::isLazyObject($parent->refMany[0]));
self::assertInstanceOf(GH852Document::class, $parent->refMany[0]);
self::assertFalse($this->uow->isUninitializedObject($parent->refMany[0]));
self::assertEquals($idGenerator('childB'), $parent->refMany[0]->id);
self::assertEquals('childB', $parent->refMany[0]->name);

self::assertIsLazyObject($parent->refMany[1]);
self::assertTrue(self::isLazyObject($parent->refMany[1]));
self::assertInstanceOf(GH852Document::class, $parent->refMany[1]);
self::assertFalse($this->uow->isUninitializedObject($parent->refMany[1]));
self::assertEquals($idGenerator('childC'), $parent->refMany[1]->id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testRemoveCascadesThroughProxyDocuments(): void

$foo = $this->dm->find(GH936Document::class, $foo->id);

self::assertIsLazyObject($foo->ref);
self::assertTrue(self::isLazyObject($foo->ref));

$this->dm->remove($foo);
$this->dm->flush();
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ public function testViewReferences(): void
$viewReference = $this->dm->find(ViewReference::class, $alcaeus->getId());
self::assertInstanceOf(ViewReference::class, $viewReference);

self::assertIsLazyObject($viewReference->getReferenceOneView());
self::assertTrue(self::isLazyObject($viewReference->getReferenceOneView()));
self::assertSame($malarzm->getId(), $viewReference->getReferenceOneView()->getId());

// No proxies for inverse referenceOne
self::assertInstanceOf(UserName::class, $viewReference->getReferenceOneViewMappedBy());
self::assertSame($alcaeus->getId(), $viewReference->getReferenceOneViewMappedBy()->getId());

self::assertCount(1, $viewReference->getReferenceManyView());
self::assertIsLazyObject($viewReference->getReferenceManyView()[0]);
self::assertTrue(self::isLazyObject($viewReference->getReferenceManyView()[0]));
self::assertSame($malarzm->getId(), $viewReference->getReferenceManyView()[0]->getId());

// No proxies for inverse referenceMany
Expand Down
Loading

0 comments on commit e69cd31

Please sign in to comment.