コード例 #1
0
ファイル: QueryTest.php プロジェクト: poulikov/mongodb-odm
 public function testDateRange()
 {
     $article1 = new Article();
     $article1->setTitle('test');
     $article1->setBody('test');
     $article1->setCreatedAt('1985-09-01 00:00:00');
     $article2 = new Article();
     $article2->setTitle('test');
     $article2->setBody('test');
     $article2->setCreatedAt('1985-09-02 00:00:00');
     $article3 = new Article();
     $article3->setTitle('test');
     $article3->setBody('test');
     $article3->setCreatedAt('1985-09-03 00:00:00');
     $article4 = new Article();
     $article4->setTitle('test');
     $article4->setBody('test');
     $article4->setCreatedAt('1985-09-04 00:00:00');
     $this->dm->persist($article1);
     $this->dm->persist($article2);
     $this->dm->persist($article3);
     $this->dm->persist($article4);
     $this->dm->flush();
     $this->dm->clear();
     $query = $this->dm->createQuery('Documents\\Article');
     $query->whereRange('createdAt', new \MongoDate(strtotime('1985-09-01')), new \MongoDate(strtotime('1985-09-04')));
     $articles = array_values($query->execute());
     $this->assertEquals(2, count($articles));
     $this->assertEquals('1985-09-02', $articles[0]->getCreatedAt()->format('Y-m-d'));
     $this->assertEquals('1985-09-03', $articles[1]->getCreatedAt()->format('Y-m-d'));
 }
コード例 #2
0
 public function testChangeSetIsUpdated()
 {
     $this->dm->getEventManager()->addEventListener(Events::preUpdate, new ChangeSetIsUpdatedListener());
     $a = new Article();
     $a->setTitle('Title');
     $this->dm->persist($a);
     $this->dm->flush();
     $a->setBody('Body');
     $this->dm->flush();
     $this->dm->clear();
     $a = $this->dm->find(Article::class, $a->getId());
     $this->assertEquals('Changed', $a->getBody());
 }
コード例 #3
0
 public function testQueryIsIterable()
 {
     $article = new Article();
     $article->setTitle('test');
     $this->dm->persist($article);
     $this->dm->flush(null, array('safe' => true));
     $qb = $this->dm->createQueryBuilder('Documents\\Article');
     $query = $qb->getQuery();
     $this->assertTrue($query instanceof \Doctrine\MongoDB\IteratorAggregate);
     foreach ($query as $article) {
         $this->assertEquals('Documents\\Article', get_class($article));
     }
 }
コード例 #4
0
 public function testCollectionField()
 {
     $classMetadata = $this->dm->getClassMetadata('Documents\\Article');
     $persister = new BasicDocumentPersister($this->dm, $classMetadata);
     $this->dm->getUnitOfWork()->setDocumentPersister('Documents\\Article', $persister);
     $article = new Article();
     $article->setTitle('test');
     $article->setBody('test');
     $article->setCreatedAt('1985-09-04 00:00:00');
     $article->addTag('tag 1');
     $article->addTag('tag 2');
     $article->addTag('tag 3');
     $article->addTag('tag 4');
     $this->dm->persist($article);
     $this->dm->getUnitOfWork()->computeChangeSets();
     $update = $persister->prepareUpdateData($article);
     $this->assertTrue(array_key_exists('$pushAll', $update));
     $this->assertTrue(array_key_exists('tags', $update['$pushAll']));
     $this->assertEquals(4, count($update['$pushAll']['tags']));
     $this->assertFalse(array_key_exists('$pullAll', $update));
     $this->dm->flush();
     $this->dm->clear();
     unset($article);
     $article = $this->dm->findOne('Documents\\Article');
     $this->assertEquals(array('tag 1', 'tag 2', 'tag 3', 'tag 4'), $article->getTags());
     $article->removeTag('tag 1');
     $article->removeTag('tag 3');
     $article->addTag('tag 5');
     $this->dm->getUnitOfWork()->computeChangeSets();
     $update = $persister->prepareUpdateData($article);
     $this->assertTrue(array_key_exists('$pushAll', $update));
     $this->assertTrue(array_key_exists('tags', $update['$pushAll']));
     $this->assertEquals(1, count($update['$pushAll']['tags']));
     $this->assertTrue(array_key_exists('$pullAll', $update));
     $this->assertTrue(array_key_exists('tags', $update['$pullAll']));
     $this->assertEquals(2, count($update['$pullAll']['tags']));
     $this->dm->flush();
     $this->dm->clear();
     unset($article);
     $article = $this->dm->findOne('Documents\\Article');
     $this->assertEquals(array('tag 2', 'tag 4', 'tag 5'), $article->getTags());
 }