예제 #1
0
 public function testWillUpsertAndUpdateModelWithSameKeyAsCriteria()
 {
     $model = new ModelWithCustomIdAsSecondaryKey();
     $model->id = '123';
     $model->name = 'john';
     $em = new EntityManager($model);
     $finder = new Finder($model, $em);
     $criteria = new Criteria();
     $criteria->id = $model->id;
     $result1 = $em->updateOne($criteria);
     $this->assertTrue($result1, 'That update was successfull');
     $count = $finder->count();
     $this->assertSame(1, $count, 'That one document was inserted');
     $found = $finder->find($criteria);
     //found
     $this->assertSame('john', $found->name, 'That stored document has proper `name`');
     //found
     $model->id = '666';
     $model->name = 'joe';
     $criteria->id = 123;
     $result2 = $em->updateOne($criteria);
     $this->assertTrue($result2, 'That second update was successfull');
     $count2 = $finder->count();
     $this->assertSame(1, $count2, 'That one document was updated, not inserted');
     $criteria->id = 666;
     $model = $finder->find($criteria);
     $this->assertNotNull($model, 'That id was in fact changed');
     $this->assertSame('joe', $model->name, 'That stored document has proper `name`');
     $this->assertSame('666', $model->id, 'That stored document has proper `id`');
 }
예제 #2
0
 /**
  * Returns the total number of data items.
  * When {@link pagination} is set false, this returns the same value as {@link itemCount}.
  * @return integer total number of possible data items.
  */
 public function getTotalItemCount()
 {
     if ($this->totalItemCount === null) {
         $this->totalItemCount = $this->finder->count($this->getCriteria());
     }
     return $this->totalItemCount;
 }
예제 #3
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);
 }
예제 #4
0
 public function testIfWillGenerateAndStoreNonActivationKeyWithArrayCallback()
 {
     $model = new ModelWithSecretField();
     $em = new EntityManager($model);
     $finder = new Finder($model);
     $model->activationKey = true;
     $em->upsert();
     $found = $finder->find();
     $this->assertSame(1, $finder->count(), 'That only one document is in collection');
     $this->assertSame(40, strlen($found->activationKey), 'That non empty activation key was saved as hash');
     $hash1 = $found->activationKey;
     $found->activationKey = '';
     $em = new EntityManager($found);
     $em->upsert();
     $found2 = $finder->find();
     $this->assertSame(1, $finder->count(), 'That only one document is in collection');
     $this->assertSame(40, strlen($found2->activationKey), 'That empty activation key is hash');
     $this->assertSame($hash1, $found2->activationKey, 'That empty activation key was NOT saved');
 }
예제 #5
0
 public function testIfWillUpdateAll()
 {
     $model = new DocumentBaseAttributes();
     $model->bool = false;
     $model->string = 'Las Vegas';
     $model->save();
     $model = new DocumentBaseAttributes();
     $model->bool = true;
     $model->string = 'Las Palmas';
     $model->save();
     $model = new DocumentBaseAttributes();
     $model->bool = false;
     $model->string = 'Las Cruces';
     $model->save();
     $em = new EntityManager($model);
     // With true
     $modifier = new Modifier(['int' => ['set' => 1]]);
     $criteria = new Criteria();
     $criteria->bool = true;
     $ok = $em->updateAll($modifier, $criteria);
     $this->assertTrue($ok);
     $criteria = new Criteria();
     $criteria->int = 1;
     $finder = new Finder($model);
     $modified = $finder->count($criteria);
     $this->assertSame(1, $modified);
     $found = $finder->find($criteria);
     $this->assertSame(1, $found->int);
     // With false
     $modifier = new Modifier();
     $modifier->set('int', 2);
     $criteria = new Criteria();
     $criteria->bool = false;
     $ok = $em->updateAll($modifier, $criteria);
     $this->assertTrue($ok);
     $criteria = new Criteria();
     $criteria->int = 2;
     $finder = new Finder($model);
     $modified = $finder->count($criteria);
     $this->assertSame(2, $modified);
     $found = $finder->find($criteria);
     $this->assertSame(2, $found->int);
 }
예제 #6
0
 public function testIfWillSkipUnsafeAttributeOnSaveWhenUpdatingModel()
 {
     $model = new ModelWithUnsafeAttribute();
     $model->active = true;
     $em = new EntityManager($model);
     $em->save();
     $finder = new Finder($model, $em);
     $found = $finder->findByPk($model->_id);
     $this->assertNotNull($found, 'That model was saved');
     $this->assertTrue($found->active, 'That value was set');
     // Update model from external data
     // NOTE: Creating model from external data will not work, as there is no way to take value from
     $data = ['active' => false];
     $model2 = SafeArray::toModel($data, null, $found);
     $this->assertTrue($model2->active, 'That value was ignored on mass set, as it is unsafe');
     $em2 = new EntityManager($model2);
     $em2->save();
     $found2 = $finder->findByPk($found->_id);
     $this->assertTrue($found2->active, 'That value was not updated in db');
     $this->assertSame(1, $finder->count(), 'That only one model was saved');
 }
예제 #7
0
 public function testIfWillFindAllByAttributes()
 {
     $model = new WithBaseAttributes();
     $model->string = 'foo';
     $em = new EntityManager($model);
     $model = new WithBaseAttributes();
     $model->string = 'foo';
     $em->insert($model);
     $model = new WithBaseAttributes();
     $model->string = 'foo';
     $em->insert($model);
     $model = new WithBaseAttributes();
     $model->string = 'foo';
     $em->insert($model);
     $model = new WithBaseAttributes();
     $model->string = 'blah';
     $em->insert($model);
     $model = new WithBaseAttributes();
     $model->string = 'blah';
     $em->insert($model);
     $model = new WithBaseAttributes();
     $model->string = 'blah';
     $em->insert($model);
     $finder = new Finder($model);
     $all = $finder->findAllByAttributes(['string' => 'foo']);
     $this->assertSame(6, $finder->count());
     $this->assertSame(3, count($all));
     foreach ($all as $found) {
         $this->assertInstanceof(WithBaseAttributes::class, $found);
         $this->assertSame('foo', $found->string);
     }
 }
예제 #8
0
 public function testIfWillDeleteAllPlainDocument()
 {
     $model = new PlainWithBasicAttributes();
     $model->_id = new MongoId();
     $em = new EntityManager($model);
     $em->save();
     $model = new PlainWithBasicAttributes();
     $model->_id = new MongoId();
     $em = new EntityManager($model);
     $em->save();
     $finder = new Finder($model);
     $count = $finder->count();
     $this->assertSame(2, $count);
     $em->deleteAll();
     $deletedCount = $finder->count();
     $this->assertSame(0, $deletedCount);
 }