Example #1
0
 public function testIfWillCountByAttributes()
 {
     $model = new WithBaseAttributes();
     $model->string = 'foo';
     $em = new EntityManager($model);
     $em->insert();
     $model = new WithBaseAttributes();
     $model->string = 'foo';
     $em->insert($model);
     $model = new WithBaseAttributes();
     $model->string = 'foo';
     $em->insert($model);
     // Some other models
     $model = new WithBaseAttributes();
     $model->string = 'blah';
     $em->insert($model);
     $model = new WithBaseAttributes();
     $model->string = 'blah';
     $em->insert($model);
     $finder = new Finder($model);
     $count = $finder->count();
     $this->assertSame(5, $count);
     $attributesCount = $finder->countByAttributes(['string' => 'foo']);
     $this->assertSame(3, $attributesCount);
 }
Example #2
0
 public function testIfCanFindByAttributes()
 {
     $model = new WithBaseAttributes();
     $model->int = 10;
     $em = new EntityManager($model);
     $em->insert();
     $model = new WithBaseAttributes();
     $model->int = 20;
     $em->insert($model);
     $finder = new Finder($model);
     $found = $finder->findByAttributes(['int' => 10]);
     $this->assertInstanceof(WithBaseAttributes::class, $found);
     $this->assertSame(10, $found->int);
 }
Example #3
0
 public function testIfWillDeepEmbed()
 {
     $model = new PlainDeepEmbedded();
     $model->_id = new MongoId();
     $withPlain = new WithPlainEmbedded();
     $stats = new SimplePlainEmbedded();
     $stats->active = false;
     $stats->name = 'buried stats';
     $stats->visits = 100002;
     $withPlain->title = 'first level';
     $withPlain->stats = $stats;
     $model->withPlain = $withPlain;
     $model->title = 'deep blue';
     $em = new EntityManager($model);
     $em->insert();
     $finder = new Finder($model);
     $found = $finder->findByPk($model->_id);
     $this->assertNotNull($found);
     $this->assertTrue($found instanceof PlainDeepEmbedded);
     $this->assertSame($found->title, $model->title);
     $this->assertNotNull($found->withPlain);
     $this->assertTrue($found->withPlain instanceof WithPlainEmbedded);
     $this->assertSame($found->withPlain->title, $model->withPlain->title);
     $this->assertNotNull($found->withPlain->stats);
     $this->assertTrue($found->withPlain->stats instanceof SimplePlainEmbedded);
 }
Example #4
0
 public function testIfExistsByCriteria()
 {
     $model = new WithBaseAttributes();
     $model->int = 10;
     $em = new EntityManager($model);
     $em->insert();
     $model = new WithBaseAttributes();
     $model->int = 20;
     $em->insert($model);
     $finder = new Finder($model);
     $criteria = new Criteria();
     $criteria->int = 10;
     $this->assertTrue($finder->exists($criteria));
     $criteria = new Criteria();
     $criteria->int = 100;
     $this->assertFalse($finder->exists($criteria));
 }
Example #5
0
 public function testIfWillFindAllUsingCursor()
 {
     $model = new WithBaseAttributes();
     $model->string = 'foo';
     $em = new EntityManager($model);
     $em->insert();
     $model = new WithBaseAttributes();
     $model->string = 'foo';
     $em->insert($model);
     $model = new WithBaseAttributes();
     $model->string = 'foo';
     $em->insert($model);
     $finder = new Finder($model);
     $cursor = $finder->withCursor()->findAll();
     $this->assertSame(3, count($cursor));
     $this->assertInstanceOf(Cursor::class, $cursor);
     foreach ($cursor as $found) {
         $this->assertInstanceof(WithBaseAttributes::class, $found);
         $this->assertSame('foo', $found->string);
     }
 }
Example #6
0
 public function testIfPlainObjectWillRefer()
 {
     $model = new WithPlainDbRef();
     $model->_id = new MongoId();
     $model->title = 'stats';
     $model->stats = new SimplePlainDbRef();
     $model->stats->active = true;
     $model->stats->name = 'www';
     $model->stats->visits = 10000;
     $em = new EntityManager($model);
     $em->insert();
     $finder = new Finder($model);
     $found = $finder->findByPk($model->_id);
     $this->assertNotNull($found);
     $this->assertSame($found->title, $model->title);
     $this->assertTrue($found instanceof WithPlainDbRef);
     $this->assertTrue($found->stats instanceof SimplePlainDbRef);
 }
Example #7
0
 public function testIfWIllConvertDbRefToJsonArray()
 {
     $model = new WithPlainDbRef();
     $model->_id = new MongoId();
     $model->title = 'stats';
     $model->stats = new SimplePlainDbRef();
     $model->stats->active = true;
     $model->stats->name = 'www';
     $model->stats->visits = 10000;
     $em = new EntityManager($model);
     $em->insert();
     $finder = new Finder($model);
     $found = $finder->findByPk($model->_id);
     $json = JsonArray::fromModel($found);
     $this->assertSame($found->title, $json['title']);
     $this->assertSame($found->stats->active, $json['stats']['active']);
     $this->assertSame($found->stats->name, $json['stats']['name']);
 }
Example #8
0
 public function testIfWillFindPlainDocumentWithCompositePkWithWrongPkTypes()
 {
     //exit;
     $model = new CompositePrimaryKey();
     $model->title = 'fooo';
     $IdOne = new MongoId();
     $IdTwo = 2;
     $IdThree = (string) new MongoId();
     $model->primaryOne = $IdOne;
     $model->primaryTwo = $IdTwo;
     $model->primaryThree = $IdThree;
     $em = new EntityManager($model);
     $em->insert();
     $finder = new Finder($model);
     // Wrong types on purpose
     $found = $finder->findByPk(['primaryOne' => (string) $IdOne, 'primaryTwo' => (string) $IdTwo, 'primaryThree' => new MongoId($IdThree)]);
     $this->assertNotNull($found);
     $this->assertTrue($found instanceof CompositePrimaryKey);
     $this->assertSame($model->title, $found->title);
 }
Example #9
0
 public function testIfWillReferenceArrayOfDifferentTypeDocuments()
 {
     $model = new WithPlainDbRefArrayDifferentTypes();
     $id = new MongoId();
     $model->_id = $id;
     $model->title = 'some title';
     $data = [['active' => false, 'name' => 'stats one', 'visits' => 100, '_type' => SimplePlainDbRef::class], ['active' => true, 'name' => 'stats two', 'visits' => 1000, '_type' => SimplePlainDbRefSecond::class], ['active' => false, 'name' => 'stats three', 'visits' => 10000, '_type' => SimplePlainDbRefSecond::class]];
     $stats = [];
     foreach ($data as $key => $value) {
         $referenced = new $value['_type']();
         foreach ($value as $field => $fieldValue) {
             if ($field == '_type') {
                 continue;
             }
             $referenced->{$field} = $fieldValue;
         }
         $stats[$key] = $referenced;
         $model->stats[$key] = $referenced;
     }
     $em = new EntityManager($model);
     $em->insert();
     $finder = new Finder($model);
     $found = $finder->findByPk($id);
     $this->assertNotNull($found);
     $this->assertTrue($found instanceof WithPlainDbRefArray);
     $this->assertSame(count($stats), count($found->stats));
     foreach ($data as $key => $value) {
         $this->assertNotNull($found->stats[$key]);
         //			$this->write(sprintf('Should be of type: %s', $value['_type']));
         $this->assertSame(get_class($found->stats[$key]), $value['_type']);
         $this->assertTrue($found->stats[$key] instanceof $value['_type']);
         foreach ($value as $field => $fieldValue) {
             if ($field == '_type') {
                 continue;
             }
             $this->assertSame($found->stats[$key]->{$field}, $fieldValue);
         }
     }
 }
Example #10
0
 public function testIfWillFindAllByCompositePks()
 {
     $pks[] = ['primaryOne' => new \MongoId(), 'primaryTwo' => 1, 'primaryThree' => 'one', 'title' => 'xxx'];
     $pks[] = ['primaryOne' => new \MongoId(), 'primaryTwo' => 2, 'primaryThree' => 'two', 'title' => 'yyy'];
     $pks[] = ['primaryOne' => new \MongoId(), 'primaryTwo' => 3, 'primaryThree' => 'three', 'title' => 'zzz'];
     foreach ($pks as $i => $keys) {
         $model = new CompositePrimaryKey();
         $em = new EntityManager($model);
         foreach ($keys as $field => $value) {
             $model->{$field} = $value;
         }
         $em->insert();
     }
     $model = new CompositePrimaryKey();
     $em = new EntityManager($model);
     $model->primaryOne = new \MongoId();
     $model->primaryTwo = 12;
     $model->primaryThree = 'ddd';
     $em->insert();
     $finder = new Finder($model);
     $all = $finder->findAllByPk($pks);
     $this->assertSame(3, count($all));
     foreach ($all as $found) {
         $this->assertInstanceof(CompositePrimaryKey::class, $found);
         $this->assertTrue(in_array($found->title, ['xxx', 'yyy', 'zzz']));
     }
 }