public function testFindByRefOneFull()
 {
     $user = new User();
     $account = new Account('name');
     $user->setAccount($account);
     $this->dm->persist($user);
     $this->dm->persist($account);
     $this->dm->flush();
     $this->assertSame($user, $this->dm->getRepository(User::class)->findOneBy(['account' => $account]));
 }
 public function testFindByRefOneWithoutTargetDocumentFull()
 {
     $user = new User();
     $account = new Account('name');
     $user->setAccount($account);
     $this->dm->persist($user);
     $this->dm->persist($account);
     $this->dm->flush();
     $query = $this->dm->getUnitOfWork()->getDocumentPersister(Account::class)->prepareQueryOrNewObj(['user' => $user]);
     $expectedQuery = ['user.$ref' => 'users', 'user.$id' => new \MongoId($user->getId()), 'user.$db' => DOCTRINE_MONGODB_DATABASE];
     $this->assertEquals($expectedQuery, $query);
     $this->assertSame($account, $this->dm->getRepository(Account::class)->findOneBy(['user' => $user]));
 }
Example #3
0
 public function testOneReference()
 {
     $account = new Account();
     $account->setName('Test Account');
     $user = new User();
     $user->setUsername('jwage');
     $user->setAccount($account);
     $this->dm->persist($user);
     $this->dm->flush();
     $this->dm->flush();
     $this->dm->clear();
     $accountId = $user->getAccount()->getId();
     $user2 = $this->dm->createQuery('Documents\\User')->where('id', $user->getId())->getSingleResult();
 }
 public function testOneReference()
 {
     $account = new Account();
     $account->setName('Test Account');
     $user = new User();
     $user->setUsername('jwage');
     $user->setAccount($account);
     $this->dm->persist($user);
     $this->dm->flush();
     $this->dm->flush();
     $this->dm->clear();
     $accountId = $user->getAccount()->getId();
     $qb = $this->dm->createQueryBuilder('Documents\\User')->field('id')->equals($user->getId());
     $query = $qb->getQuery();
     $user2 = $query->getSingleResult();
 }
Example #5
0
 public function testCascadeInsertUpdateAndRemove()
 {
     $account = new Account();
     $account->setName('Jon Test Account');
     $user = new User();
     $user->setUsername('jon');
     $user->setPassword('changeme');
     $user->setAccount($account);
     $this->dm->persist($user);
     $this->dm->flush();
     $account->setName('w00t');
     $this->dm->flush();
     $this->assertEquals('w00t', $user->getAccount()->getName());
     $this->dm->remove($user);
     $this->dm->flush();
     $this->dm->clear();
 }
Example #6
0
 public function testRemove()
 {
     $account = new Account();
     $account->setName('Jon Test Account');
     $user = new User();
     $user->setUsername('jon');
     $user->setPassword('changeme');
     $user->setAccount($account);
     $this->dm->persist($user);
     $this->dm->flush();
     $this->dm->remove($user);
     $this->dm->flush();
     $account = $this->dm->find('Documents\\Account', $account->getId());
     $this->assertNull($account);
     $user = $this->dm->find('Documents\\User', $user->getId());
     $this->assertNull($user);
 }
 /**
  * @group replication_lag
  */
 public function testPrimeReferencesInvokesPrimer()
 {
     $group1 = new Group();
     $group2 = new Group();
     $account = new Account();
     $user = new User();
     $user->addGroup($group1);
     $user->addGroup($group2);
     $user->setAccount($account);
     $this->dm->persist($user);
     $this->dm->flush();
     $this->dm->clear();
     $invokedArgs = array();
     $primer = function (DocumentManager $dm, ClassMetadata $class, array $ids, array $hints) use(&$invokedArgs) {
         $invokedArgs[] = func_get_args();
     };
     $this->dm->createQueryBuilder('Documents\\User')->field('account')->prime($primer)->field('groups')->prime($primer)->slaveOkay(true)->getQuery()->toArray();
     $this->assertCount(2, $invokedArgs, 'Primer was invoked once for each referenced class.');
     $this->assertArrayHasKey(Query::HINT_SLAVE_OKAY, $invokedArgs[0][3], 'Primer was invoked with UnitOfWork hints from original query.');
     $this->assertTrue($invokedArgs[0][3][Query::HINT_SLAVE_OKAY], 'Primer was invoked with UnitOfWork hints from original query.');
     $accountIds = array($account->getId());
     $groupIds = array($group1->getId(), $group2->getId());
     $this->assertEquals($accountIds, $invokedArgs[0][2]);
     $this->assertEquals($groupIds, $invokedArgs[1][2]);
 }
Example #8
0
 public function testReplaceEntireGroupsArray()
 {
     $account = new Account();
     $account->setName('Jon Test Account');
     $user = new User();
     $user->setUsername('jon333');
     $user->setPassword('changeme');
     $user->setAccount($account);
     $group2 = new Group('member');
     $user->addGroup(new Group('administrator'));
     $user->addGroup($group2);
     $user->addGroup(new Group('moderator'));
     $this->dm->persist($user);
     $this->dm->flush();
     $this->dm->clear();
     $user = $this->dm->find('Documents\\User', $user->getId());
     $this->assertNotNull($user);
     // Issue is collection must be initialized
     $groups = $user->getGroups();
     $groups[0];
     // initialize collection
     // reffectively remove two of the groups
     //$user->getGroups()->clear();
     //$user->getGroups()->add($group2);
     $user->setGroups(array($group2));
     $this->assertEquals(1, count($user->getGroups()));
     $this->dm->flush();
     $this->dm->clear();
     $user = $this->dm->find('Documents\\User', $user->getId());
     $this->assertEquals(1, count($user->getGroups()));
 }
 public function testReplaceGroups()
 {
     $this->dm->getUnitOfWork()->setDocumentPersister('Documents\\User', new BasicDocumentPersister($this->dm, $this->classMetadata));
     $account = new Account();
     $account->setName('Jon Test Account');
     $user = new User();
     $user->setUsername('jon');
     $user->setPassword('changeme');
     $user->setAccount($account);
     $user->addGroup(new Group('administrator'));
     $user->addGroup(new Group('member'));
     $user->addGroup(new Group('moderator'));
     $this->dm->persist($user);
     $this->dm->flush();
     $this->dm->clear();
     unset($user, $account);
     $user = $this->dm->findOne('Documents\\User');
     $user->removeGroup('moderator');
     $user->removeGroup('member');
     $this->assertEquals(1, count($user->getGroups()));
     $user->addGroup(new Group('seller'));
     $user->addGroup(new Group('supplier'));
     $this->assertEquals(3, count($user->getGroups()));
     $this->dm->getUnitOfWork()->setDocumentPersister('Documents\\User', $this->persister);
     $this->persister->expects($this->once())->method('update')->with($user);
     $this->dm->getUnitOfWork()->computeChangeSets();
     $update = $this->persister->prepareUpdateData($user);
     $this->assertFalse(array_key_exists('$set', $update));
     $this->assertFalse(array_key_exists('$unset', $update));
     $this->assertTrue(array_key_exists('$pullAll', $update));
     $this->assertTrue(array_key_exists('groups', $update['$pullAll']));
     $this->assertEquals(2, count($update['$pullAll']['groups']));
     $this->assertTrue(array_key_exists('$pushAll', $update));
     $this->assertTrue(array_key_exists('groups', $update['$pushAll']));
     $this->assertEquals(2, count($update['$pushAll']['groups']));
     $this->dm->flush();
     $this->dm->clear();
     unset($user);
     $user = $this->dm->findOne('Documents\\User');
     $this->assertEquals(3, count($user->getGroups()));
 }
Example #10
0
 public function testFindUser()
 {
     $account = new Account();
     $account->setName('Jon Test Account');
     $user1 = new CustomUser();
     $user1->setId('userId');
     $user1->setUsername('user1');
     $user1->setPassword('changeme');
     $user1->setAccount($account);
     $user2 = new User();
     $user2->setUsername('user2');
     $user2->setPassword('changeme');
     $user2->setAccount($account);
     $user3 = new User();
     $user3->setUsername('user3');
     $user3->setPassword('changeme');
     $user3->setAccount($account);
     $this->dm->persist($user1);
     $this->dm->persist($user2);
     $this->dm->persist($user3);
     $this->dm->flush();
     $this->dm->clear();
     unset($user1, $user2, $user3);
     $user = $this->dm->find('Documents\\CustomUser', 'userId');
     $this->assertNotNull($user);
     $this->assertEquals('userId', $user->getId());
     $this->assertEquals('user1', $user->getUsername());
     $this->dm->clear();
     unset($user);
     $this->assertNull($this->dm->find('Documents\\User', 'userId'));
     $this->assertNull($this->dm->find('Documents\\CustomUser', 'asd'));
 }