public function setUp() { parent::setUp(); $this->ids = array(); $groupA = new Group('groupA'); $groupB = new Group('groupB'); $profile = new Profile(); $profile->setFirstname('Timothy'); $tim = new User(); $tim->setUsername('Tim'); $tim->setHits(10); $tim->addGroup($groupA); $tim->addGroup($groupB); $tim->setProfile($profile); $this->dm->persist($tim); $john = new User(); $john->setUsername('John'); $john->setHits(10); $this->dm->persist($john); $this->dm->flush(); $this->dm->clear(); $this->ids['tim'] = $tim->getId(); $this->ids['john'] = $john->getId(); $this->fc = $this->dm->getFilterCollection(); }
public function testSlaveOkayOnPersistentCollection() { $user = new User(); $user->addGroup(new Group('Test')); $this->dm->persist($user); $this->dm->flush(); $this->dm->clear(); $user = $this->dm->getRepository('Documents\\User')->createQueryBuilder()->slaveOkay(false)->getQuery()->getSingleResult(); $this->assertEquals(array(), $user->getGroups()->getHints()); $this->dm->clear(); $users = $this->dm->getRepository('Documents\\User')->createQueryBuilder()->getQuery()->execute(); $this->assertEquals(array(), $users->getHints()); $users = array_values($users->toArray()); $user = $users[0]; $this->assertEquals(array(), $user->getGroups()->getHints()); $this->dm->clear(); $user = $this->dm->getRepository('Documents\\User')->createQueryBuilder()->slaveOkay(true)->getQuery()->getSingleResult(); $this->assertEquals(array(Query::HINT_SLAVE_OKAY => true), $user->getGroups()->getHints()); $this->dm->clear(); $users = $this->dm->getRepository('Documents\\User')->createQueryBuilder()->getQuery()->execute()->slaveOkay(true); $this->assertEquals(array(Query::HINT_SLAVE_OKAY => true), $users->getHints()); $users = array_values($users->toArray()); $user = $users[0]; $this->assertEquals(array(Query::HINT_SLAVE_OKAY => true), $user->getGroups()->getHints()); $this->dm->clear(); $user = $this->dm->getRepository('Documents\\User')->createQueryBuilder()->getQuery()->getSingleResult(); $groups = $user->getGroups(); $groups->setHints(array(Query::HINT_SLAVE_OKAY => true)); $this->assertEquals(array(Query::HINT_SLAVE_OKAY => true), $groups->getHints()); }
public function testFlushOptions() { $user = new User(); $user->setUsername('jwage'); $this->dm->persist($user); $this->dm->flush(); $user->setUsername('ok'); $this->dm->flush(); }
public function setUp() { parent::setUp(); $user = new User(); $user->addGroup(new Group('Test')); $this->dm->persist($user); $this->dm->flush(); $this->dm->clear(); }
public function testFindByEmbedMany() { $user = new User(); $phonenumber = new Phonenumber('12345678'); $user->addPhonenumber($phonenumber); $this->dm->persist($user); $this->dm->flush(); $this->assertSame($user, $this->dm->getRepository(User::class)->findOneBy(['phonenumbers' => $phonenumber])); }
public function testFlushOptions() { $user = new User(); $user->setUsername('jwage'); $this->dm->persist($user); $this->dm->flush(null, array('safe' => true)); $user->setUsername('ok'); $this->dm->flush(null, array('safe' => true)); }
public function testFindAndModifyAlt() { $doc = new User(); $doc->setUsername('jwage'); $this->dm->persist($doc); $this->dm->flush(); // test update findAndModify $q = $this->dm->createQueryBuilder()->findAndUpdate('Documents\\User')->returnNew(true)->field('username')->equals('jwage')->field('username')->set('Romain Neutron')->getQuery(); $result = $q->execute(); // Test the username was set $this->assertEquals('Romain Neutron', $result->getUsername()); }
public function testManyEmbedded() { $user = new \Documents\User(); $user->addPhonenumber(new Phonenumber('6155139185')); $user->addPhonenumber(new Phonenumber('6153303769')); $this->dm->persist($user); $this->dm->flush(); $this->dm->clear(); $user2 = $this->dm->createQueryBuilder('Documents\\User')->field('id')->equals($user->getId())->getQuery()->getSingleResult(); $this->assertNotNull($user2); $this->assertEquals($user->getPhonenumbers()->toArray(), $user2->getPhonenumbers()->toArray()); }
public function setUp() { if (version_compare(phpversion('mongo'), '1.3.0', '<')) { $this->markTestSkipped('This test is not applicable to driver versions < 1.3.0'); } parent::setUp(); $user = new User(); $user->addGroup(new Group('Test')); $this->dm->persist($user); $this->dm->flush(); $this->dm->clear(); }
public function testDetatchNewDocumentDuringOnFlush() { // create a test document $test = new User(); $test->setUsername('toby'); $this->dm->persist($test); $this->dm->flush(); $this->dm->clear(); $repository = $this->dm->getRepository(get_class($test)); $test = $repository->find($test->getId()); $this->assertNull($test); }
public function testCountFoundOnlyBehavior() { $usernames = array('David', 'Xander', 'Alex', 'Kris', 'Jon'); foreach ($usernames as $username) { $user = new User(); $user->setUsername($username); $this->dm->persist($user); } $this->dm->flush(); $cursor = $this->dm->createQueryBuilder('Documents\\User')->sort('username', 'asc')->limit(2)->getQuery()->execute(); $this->assertEquals(5, $cursor->count()); $this->assertEquals(2, $cursor->count(true)); }
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 testDetach() { $user = new User(); $user->setUsername('jon'); $user->setPassword('changeme'); $this->dm->persist($user); $this->dm->flush(); $user->setUsername('whoop'); $this->dm->detach($user); $this->dm->flush(); $this->dm->clear(); $user2 = $this->dm->find('Documents\\User', $user->getId()); $this->assertEquals('jon', $user2->getUsername()); }
/** * 创建公司 * @param \Documents\User $user 公司的创建者 * @return \Documents\Company - 返回的是新创建的公司的对象 * @throws \Angel_Exception_Company */ public function createCompany(\Documents\User $user) { $result = null; if ($user->isStartup()) { $company = new $this->_document_class(); $company->owner = $user; $this->_dm->persist($company); $this->_dm->flush(); $result = $company; } else { // 只有创业者才能创建公司 throw new \Angel_Exception_Company(Angel_Exception_Company::ONLY_STARTUP_CAN_CREATE_COMPANY); } return $result; }
public function testDates() { $user = new User(); $user->setUsername('w00ting'); $this->dm->persist($user); $this->dm->flush(); $this->assertTrue($user->getCreatedAt() instanceof \DateTime); $user->setCreatedAt('1985-09-01 00:00:00'); $this->dm->flush(); $this->dm->clear(); $user = $this->dm->getRepository('Documents\\User')->findOneByUsername('w00ting'); $this->assertEquals('w00ting', $user->getUsername()); $this->assertTrue($user->getCreatedAt() instanceof \DateTime); $this->assertEquals('09/01/1985', $user->getCreatedAt()->format('m/d/Y')); }
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(); }
public function testOldDate() { $user = new User(); $user->setUsername('datetest'); $user->setCreatedAt('1900-01-01'); $this->dm->persist($user); $this->dm->flush(); $user->setUsername('datetest2'); $this->dm->flush(); $this->dm->clear(); $test = $this->dm->getDocumentCollection('Documents\\User')->findOne(array('username' => 'datetest2')); $this->assertTrue(isset($test['createdAt'])); $user = $this->dm->getRepository('Documents\\User')->findOneBy(array('username' => 'datetest2')); $this->assertTrue($user->getCreatedAt() instanceof \DateTime); $this->assertEquals('1900-01-01', $user->getCreatedAt()->format('Y-m-d')); }
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); }
public function testUnsetFromEmbeddedCollection() { $userRepository = $this->dm->getRepository('Documents\\User'); $user = new User(); $user->addGroup(new Group('group1')); $user->addGroup(new Group('group2')); $user->addGroup(new Group('group3')); $this->dm->persist($user); $this->dm->flush(); $this->dm->clear(); $this->assertCount(3, $user->getGroups()); $user = $userRepository->find($user->getId()); $user->getGroups()->remove(0); $this->dm->flush(); $this->dm->clear(); $user = $userRepository->find($user->getId()); $this->assertCount(2, $user->getGroups()); }
public function testClonedPersistentCollectionUpdatesCorrectly() { $u = new User(); $u->getPhonenumbers()->add(new Phonenumber('123456')); $u2 = new User(); $u2->getPhonenumbers()->add(new Phonenumber('9876543')); $u2->getPhonenumbers()->add(new Phonenumber('7654321')); $this->dm->persist($u); $this->dm->persist($u2); $this->dm->flush(); $u2->setPhonenumbers(clone $u->getPhonenumbers()); $this->dm->flush(); $this->dm->clear(); $u = $this->dm->find(get_class($u), $u->getId()); $this->assertCount(1, $u->getPhonenumbers()); $u2 = $this->dm->find(get_class($u2), $u2->getId()); $this->assertCount(1, $u2->getPhonenumbers()); $this->assertSame('123456', $u2->getPhonenumbers()->first()->getPhonenumber()); }
/** * 把关注用户的行为纪录到消息流 * @param \Documents\User $user - 谁来关注了 * @param \Documents\User $target_user - 谁被关注了 * if there is no exception, this function always return true */ public function recordFollowUserFeed(\Documents\User $user, \Documents\User $target_user) { $type = \Documents\Feed::FEED_TYPE_STARTUP_FOLLOW; if ($target_user->isInvestor()) { $type = \Documents\Feed::FEED_TYPE_INVESTOR_FOLLOW; } $date = new \DateTime(); $date->sub(new \DateInterval('PT24H')); $feed = $this->_dm->createQueryBuilder($this->_document_class)->field('user.$id')->equals($user->id)->field('feed_type')->equals($type)->field('created_at')->gt($date)->getQuery()->getSingleResult(); if (!$feed) { $feed = new $this->_document_class(); $feed->user = $user; $feed->feed_type = $type; } $feed->addFollowedUser($target_user); $this->_dm->persist($feed); $this->_dm->flush(); return true; }
public function testUpdateCollectionDuringOnFlushAndRecomputSingleDocumentChangeSet() { // create a test document $test = new User(); $test->setUsername('toby'); $test->addPhonenumber(new Phonenumber('1111')); $this->dm->persist($test); $this->dm->flush(); $test->setUsername('lucy'); $this->dm->flush(); $this->dm->clear(); $repository = $this->dm->getRepository(get_class($test)); $test = $repository->findOneBy(array('username' => 'lucy')); $phonenumbers = array(); foreach ($test->getPhonenumbers() as $phonenumber) { $phonenumbers[] = $phonenumber->getPhonenumber(); } sort($phonenumbers); $this->assertEquals(array('1111', '2222'), $phonenumbers); }
public function testRecreateShouldPreserveSorting() { $usernames = array('David', 'Xander', 'Alex', 'Kris', 'Jon'); foreach ($usernames as $username) { $user = new User(); $user->setUsername($username); $this->dm->persist($user); } $this->dm->flush(); $qb = $this->dm->createQueryBuilder('Documents\\User')->sort('username', 'asc'); $cursor = $qb->getQuery()->execute(); sort($usernames); foreach ($usernames as $username) { $this->assertEquals($username, $cursor->getNext()->getUsername()); } $cursor->recreate(); foreach ($usernames as $username) { $this->assertEquals($username, $cursor->getNext()->getUsername()); } }
public function testManyEmbeddedAddAndPersist() { $user = new User(); $user->addPhoneNumber(new Phonenumber('111-111-1111')); $user->addPhoneNumber(new Phonenumber('222-222-2222')); $this->dm->persist($user); $this->dm->flush(); $this->dm->clear(); $user = $this->dm->find('Documents\\User', $user->getId()); $phoneNumbers = $user->getPhoneNumbers(); $this->assertCount(2, $phoneNumbers); $this->assertFalse($phoneNumbers->isInitialized()); $user->addPhoneNumber(new Phonenumber('333-333-3333')); $this->assertCount(3, $phoneNumbers); $this->assertFalse($phoneNumbers->isInitialized()); $this->dm->persist($user); $this->dm->flush(); $phoneNumbers->initialize(); $this->assertCount(3, $phoneNumbers); $this->assertTrue($phoneNumbers->isInitialized()); }
public function testManyReference() { $user = new \Documents\User(); $user->addGroup(new Group('Group 1')); $user->addGroup(new Group('Group 2')); $this->dm->persist($user); $this->dm->flush(); $groups = $user->getGroups(); $this->assertTrue($groups instanceof PersistentCollection); $this->assertTrue($groups[0]->getId() !== ''); $this->assertTrue($groups[1]->getId() !== ''); $user2 = $this->dm->createQuery('Documents\\User')->where('id', $user->getId())->getSingleResult(); $groups = $user2->getGroups(); $this->assertTrue($groups instanceof PersistentCollection); $this->assertTrue($groups[0] instanceof Group); $this->assertTrue($groups[1] instanceof Group); unset($groups[0]); $this->dm->flush(); $this->dm->clear(); $user3 = $this->dm->createQuery('Documents\\User')->where('id', $user->getId())->getSingleResult(); $this->assertEquals(1, count($user2->getGroups())); }
/** * Tests Bi-Directional Reference "one to many" with nullable=true flag * * @url http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/bidirectional-references.html */ public function testOneToMany() { $user = new User(); $user->setUsername('w00ting'); $this->dm->persist($user); $this->dm->flush(); $post1 = new BlogPost(); $post1->name = 'post1'; $post1->setUser($user); $post2 = new BlogPost(); $post2->name = 'post2'; $post2->setUser($user); $post3 = new BlogPost(); $post3->name = 'post3'; $post3->setUser($user); $this->dm->persist($post1); $this->dm->persist($post2); $this->dm->persist($post3); $this->dm->flush(); $post1->setUser(null); $this->dm->flush(); $this->assertNull($post1->user); }
public function testManyReference() { $user = new \Documents\User(); $user->addGroup(new Group('Group 1')); $user->addGroup(new Group('Group 2')); $this->dm->persist($user); $this->dm->flush(); $groups = $user->getGroups(); $this->assertTrue($groups instanceof PersistentCollection); $this->assertTrue($groups[0]->getId() !== ''); $this->assertTrue($groups[1]->getId() !== ''); $this->dm->clear(); $qb = $this->dm->createQueryBuilder('Documents\\User')->field('id')->equals($user->getId()); $query = $qb->getQuery(); $user2 = $query->getSingleResult(); $groups = $user2->getGroups(); $this->assertFalse($groups->isInitialized()); $groups->count(); $this->assertFalse($groups->isInitialized()); $groups->isEmpty(); $this->assertFalse($groups->isInitialized()); $groups = $user2->getGroups(); $this->assertTrue($groups instanceof PersistentCollection); $this->assertTrue($groups[0] instanceof Group); $this->assertTrue($groups[1] instanceof Group); $this->assertTrue($groups->isInitialized()); unset($groups[0]); $groups[1]->setName('test'); $this->dm->flush(); $this->dm->clear(); $qb = $this->dm->createQueryBuilder('Documents\\User')->field('id')->equals($user->getId()); $query = $qb->getQuery(); $user3 = $query->getSingleResult(); $groups = $user3->getGroups(); $this->assertEquals('test', $groups[0]->getName()); $this->assertEquals(1, count($groups)); }
public function testUnsetFromEmbeddedCollection() { $user = new User(); $user->setUsername('jon'); $user->addGroup(new Group('test group 1')); $user->addGroup(new Group('test group 2')); $user->addGroup(new Group('test group 3')); $this->dm->persist($user); $this->dm->flush(); $this->dm->clear(); $user = $this->dm->find('Documents\\User', $user->getId()); $groups = $user->getGroups(); unset($groups[0]); $this->assertEquals(2, count($user->getGroups())); $this->dm->flush(); $this->dm->clear(); $user = $this->dm->getRepository('Documents\\User')->findOneBy(array('username' => 'jon')); $this->assertEquals(2, count($user->getGroups())); }
public function testRemoveEmbeddedDocument() { $address = new Address(); $address->setAddress('6512 Mercomatic Ct.'); $user = new User(); $user->setUsername('jwagettt'); $user->setAddress($address); $user->addPhonenumber(new Phonenumber('6155139185')); $user->addPhonenumber(new Phonenumber('6155139185')); $this->dm->persist($user); $this->dm->flush(); $user->removeAddress(); $user->getPhonenumbers()->remove(0); $user->getPhonenumbers()->remove(1); $this->dm->flush(); $check = $this->dm->getDocumentCollection('Documents\\User')->findOne(); $this->assertEmpty($check['phonenumbers']); $this->assertFalse(isset($check['address'])); }
public function testQueryReferences() { $group = new \Documents\Group('Test Group'); $user = new User(); $user->setUsername('cool'); $user->addGroup($group); $this->dm->persist($user); $this->dm->flush(); $qb = $this->dm->createQueryBuilder('Documents\\User')->field('groups')->references($group); $query = $qb->getQuery(); $user2 = $query->getSingleResult(); $this->assertSame($user, $user2); }