diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/LookupTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/LookupTest.php index de593bce57..8f42a65402 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/LookupTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/LookupTest.php @@ -1,23 +1,28 @@ requireMongoDB32('$lookup tests require at least MongoDB 3.2.0'); $this->insertTestData(); } public function testLookupStage() { - $builder = $this->dm->createAggregationBuilder(\Documents\SimpleReferenceUser::class); + $builder = $this->dm->createAggregationBuilder(SimpleReferenceUser::class); $builder ->lookup('user') ->alias('user'); @@ -29,8 +34,8 @@ public function testLookupStage() 'localField' => 'userId', 'foreignField' => '_id', 'as' => 'user', - ] - ] + ], + ], ]; $this->assertEquals($expectedPipeline, $builder->getPipeline()); @@ -44,7 +49,7 @@ public function testLookupStage() public function testLookupStageWithFieldNameTranslation() { - $builder = $this->dm->createAggregationBuilder(\Documents\SimpleReferenceUser::class); + $builder = $this->dm->createAggregationBuilder(SimpleReferenceUser::class); $builder ->lookup(CmsComment::class) ->localField('id') @@ -58,8 +63,8 @@ public function testLookupStageWithFieldNameTranslation() 'localField' => '_id', 'foreignField' => 'ip', 'as' => 'user', - ] - ] + ], + ], ]; $this->assertEquals($expectedPipeline, $builder->getPipeline()); @@ -67,9 +72,9 @@ public function testLookupStageWithFieldNameTranslation() public function testLookupStageWithClassName() { - $builder = $this->dm->createAggregationBuilder(\Documents\SimpleReferenceUser::class); + $builder = $this->dm->createAggregationBuilder(SimpleReferenceUser::class); $builder - ->lookup(\Documents\User::class) + ->lookup(User::class) ->localField('userId') ->foreignField('_id') ->alias('user'); @@ -81,8 +86,8 @@ public function testLookupStageWithClassName() 'localField' => 'userId', 'foreignField' => '_id', 'as' => 'user', - ] - ] + ], + ], ]; $this->assertEquals($expectedPipeline, $builder->getPipeline()); @@ -96,7 +101,7 @@ public function testLookupStageWithClassName() public function testLookupStageWithCollectionName() { - $builder = $this->dm->createAggregationBuilder(\Documents\SimpleReferenceUser::class); + $builder = $this->dm->createAggregationBuilder(SimpleReferenceUser::class); $builder ->lookup('randomCollectionName') ->localField('userId') @@ -110,8 +115,8 @@ public function testLookupStageWithCollectionName() 'localField' => 'userId', 'foreignField' => '_id', 'as' => 'user', - ] - ] + ], + ], ]; $this->assertEquals($expectedPipeline, $builder->getPipeline()); @@ -124,24 +129,22 @@ public function testLookupStageWithCollectionName() public function testLookupStageReferenceMany() { - $builder = $this->dm->createAggregationBuilder(\Documents\SimpleReferenceUser::class); + $builder = $this->dm->createAggregationBuilder(SimpleReferenceUser::class); $builder ->unwind('$users') ->lookup('users') ->alias('users'); $expectedPipeline = [ - [ - '$unwind' => '$users', - ], + ['$unwind' => '$users'], [ '$lookup' => [ 'from' => 'users', 'localField' => 'users', 'foreignField' => '_id', 'as' => 'users', - ] - ] + ], + ], ]; $this->assertEquals($expectedPipeline, $builder->getPipeline()); @@ -157,24 +160,22 @@ public function testLookupStageReferenceMany() public function testLookupStageReferenceManyStoreAsRef() { - $builder = $this->dm->createAggregationBuilder(\Documents\ReferenceUser::class); + $builder = $this->dm->createAggregationBuilder(ReferenceUser::class); $builder ->unwind('$referencedUsers') ->lookup('referencedUsers') ->alias('users'); $expectedPipeline = [ - [ - '$unwind' => '$referencedUsers', - ], + ['$unwind' => '$referencedUsers'], [ '$lookup' => [ 'from' => 'users', 'localField' => 'referencedUsers.id', 'foreignField' => '_id', 'as' => 'users', - ] - ] + ], + ], ]; $this->assertEquals($expectedPipeline, $builder->getPipeline()); @@ -188,39 +189,9 @@ public function testLookupStageReferenceManyStoreAsRef() $this->assertSame('malarzm', $result[1]['users'][0]['username']); } - public function testLookupStageReferenceManyWithoutUnwindMongoDB32() - { - $this->skipOnMongoDB34('$lookup tests without unwind will not work on MongoDB 3.4.0'); - - $builder = $this->dm->createAggregationBuilder(\Documents\SimpleReferenceUser::class); - $builder - ->lookup('users') - ->alias('users'); - - $expectedPipeline = [ - [ - '$lookup' => [ - 'from' => 'users', - 'localField' => 'users', - 'foreignField' => '_id', - 'as' => 'users', - ] - ] - ]; - - $this->assertEquals($expectedPipeline, $builder->getPipeline()); - - $result = $builder->execute()->toArray(); - - $this->assertCount(1, $result); - $this->assertCount(0, $result[0]['users']); - } - public function testLookupStageReferenceManyWithoutUnwindMongoDB34() { - $this->requireMongoDB34('$lookup tests with unwind require at least MongoDB 3.4.0'); - - $builder = $this->dm->createAggregationBuilder(\Documents\SimpleReferenceUser::class); + $builder = $this->dm->createAggregationBuilder(SimpleReferenceUser::class); $builder ->lookup('users') ->alias('users'); @@ -232,8 +203,8 @@ public function testLookupStageReferenceManyWithoutUnwindMongoDB34() 'localField' => 'users', 'foreignField' => '_id', 'as' => 'users', - ] - ] + ], + ], ]; $this->assertEquals($expectedPipeline, $builder->getPipeline()); @@ -248,7 +219,7 @@ public function testLookupStageReferenceManyWithoutUnwindMongoDB34() public function testLookupStageReferenceOneInverse() { - $builder = $this->dm->createAggregationBuilder(\Documents\User::class); + $builder = $this->dm->createAggregationBuilder(User::class); $builder ->match() ->field('username') @@ -258,7 +229,7 @@ public function testLookupStageReferenceOneInverse() $expectedPipeline = [ [ - '$match' => ['username' => 'alcaeus'] + '$match' => ['username' => 'alcaeus'], ], [ '$lookup' => [ @@ -266,8 +237,8 @@ public function testLookupStageReferenceOneInverse() 'localField' => '_id', 'foreignField' => 'userId', 'as' => 'simpleReferenceOneInverse', - ] - ] + ], + ], ]; $this->assertEquals($expectedPipeline, $builder->getPipeline()); @@ -280,7 +251,7 @@ public function testLookupStageReferenceOneInverse() public function testLookupStageReferenceManyInverse() { - $builder = $this->dm->createAggregationBuilder(\Documents\User::class); + $builder = $this->dm->createAggregationBuilder(User::class); $builder ->match() ->field('username') @@ -290,7 +261,7 @@ public function testLookupStageReferenceManyInverse() $expectedPipeline = [ [ - '$match' => ['username' => 'alcaeus'] + '$match' => ['username' => 'alcaeus'], ], [ '$lookup' => [ @@ -298,8 +269,8 @@ public function testLookupStageReferenceManyInverse() 'localField' => '_id', 'foreignField' => 'users', 'as' => 'simpleReferenceManyInverse', - ] - ] + ], + ], ]; $this->assertEquals($expectedPipeline, $builder->getPipeline()); @@ -312,7 +283,7 @@ public function testLookupStageReferenceManyInverse() public function testLookupStageReferenceOneInverseStoreAsRef() { - $builder = $this->dm->createAggregationBuilder(\Documents\User::class); + $builder = $this->dm->createAggregationBuilder(User::class); $builder ->match() ->field('username') @@ -322,7 +293,7 @@ public function testLookupStageReferenceOneInverseStoreAsRef() $expectedPipeline = [ [ - '$match' => ['username' => 'alcaeus'] + '$match' => ['username' => 'alcaeus'], ], [ '$lookup' => [ @@ -330,8 +301,8 @@ public function testLookupStageReferenceOneInverseStoreAsRef() 'localField' => '_id', 'foreignField' => 'referencedUser.id', 'as' => 'embeddedReferenceOneInverse', - ] - ] + ], + ], ]; $this->assertEquals($expectedPipeline, $builder->getPipeline()); @@ -344,7 +315,7 @@ public function testLookupStageReferenceOneInverseStoreAsRef() public function testLookupStageReferenceManyInverseStoreAsRef() { - $builder = $this->dm->createAggregationBuilder(\Documents\User::class); + $builder = $this->dm->createAggregationBuilder(User::class); $builder ->match() ->field('username') @@ -354,7 +325,7 @@ public function testLookupStageReferenceManyInverseStoreAsRef() $expectedPipeline = [ [ - '$match' => ['username' => 'alcaeus'] + '$match' => ['username' => 'alcaeus'], ], [ '$lookup' => [ @@ -362,8 +333,8 @@ public function testLookupStageReferenceManyInverseStoreAsRef() 'localField' => '_id', 'foreignField' => 'referencedUsers.id', 'as' => 'embeddedReferenceManyInverse', - ] - ] + ], + ], ]; $this->assertEquals($expectedPipeline, $builder->getPipeline()); @@ -376,7 +347,7 @@ public function testLookupStageReferenceManyInverseStoreAsRef() public function testLookupToShardedCollectionThrowsException() { - $builder = $this->dm->createAggregationBuilder(\Documents\User::class); + $builder = $this->dm->createAggregationBuilder(User::class); $this->expectException(MappingException::class); $builder @@ -396,22 +367,22 @@ public function testLookupToShardedReferenceThrowsException() private function insertTestData() { - $user1 = new \Documents\User(); + $user1 = new User(); $user1->setUsername('alcaeus'); - $user2 = new \Documents\User(); + $user2 = new User(); $user2->setUsername('malarzm'); - $user3 = new \Documents\User(); + $user3 = new User(); $user3->setUsername('jmikola'); $this->dm->persist($user1); $this->dm->persist($user2); - $simpleReferenceUser = new \Documents\SimpleReferenceUser(); + $simpleReferenceUser = new SimpleReferenceUser(); $simpleReferenceUser->user = $user1; $simpleReferenceUser->addUser($user1); $simpleReferenceUser->addUser($user2); - $referenceUser = new \Documents\ReferenceUser(); + $referenceUser = new ReferenceUser(); $referenceUser->setReferencedUser($user1); $referenceUser->addReferencedUser($user1); $referenceUser->addReferencedUser($user2); @@ -420,4 +391,80 @@ private function insertTestData() $this->dm->persist($referenceUser); $this->dm->flush(); } + + public function testLookupStageAndDefaultAlias() + { + $builder = $this->dm->createAggregationBuilder(SimpleReferenceUser::class); + $builder + ->lookup('user'); + + $expectedPipeline = [ + [ + '$lookup' => [ + 'from' => 'users', + 'localField' => 'userId', + 'foreignField' => '_id', + 'as' => 'user', + ], + ], + ]; + + $this->assertEquals($expectedPipeline, $builder->getPipeline()); + + $result = $builder->execute()->toArray(); + + $this->assertCount(1, $result); + $this->assertCount(1, $result[0]['user']); + $this->assertSame('alcaeus', $result[0]['user'][0]['username']); + } + + public function testLookupStageWithFieldNameTranslationAndDefaultAlias() + { + $builder = $this->dm->createAggregationBuilder(SimpleReferenceUser::class); + $builder + ->lookup(CmsComment::class) + ->localField('id') + ->foreignField('authorIp'); + + $expectedPipeline = [ + [ + '$lookup' => [ + 'from' => 'CmsComment', + 'localField' => '_id', + 'foreignField' => 'ip', + 'as' => 'user', + ], + ], + ]; + + $this->assertEquals($expectedPipeline, $builder->getPipeline()); + } + + public function testLookupStageWithClassNameAndDefaultAlias() + { + $builder = $this->dm->createAggregationBuilder(SimpleReferenceUser::class); + $builder + ->lookup(User::class) + ->localField('userId') + ->foreignField('_id'); + + $expectedPipeline = [ + [ + '$lookup' => [ + 'from' => 'users', + 'localField' => 'userId', + 'foreignField' => '_id', + 'as' => 'user', + ], + ], + ]; + + $this->assertEquals($expectedPipeline, $builder->getPipeline()); + + $result = $builder->execute()->toArray(); + + $this->assertCount(1, $result); + $this->assertCount(1, $result[0]['user']); + $this->assertSame('alcaeus', $result[0]['user'][0]['username']); + } }