Пример #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
 public function testIfWillResizeSavedImage()
 {
     // Temp file location
     $fileName = __DIR__ . '/logo-1024.png';
     $md5 = md5_file($fileName);
     $model = new ModelWithEmbeddedFile();
     $model->file = new Image();
     $model->file->set($fileName);
     $em = new EntityManager($model);
     $em->save();
     $finder = new Finder($model);
     $found = $finder->findByPk($model->_id);
     /* @var $found ModelWithEmbeddedFile */
     $file = $found->file->get()->getBytes();
     $this->assertSame($fileName, $found->file->filename);
     $this->assertSame($md5, md5($file));
     $image = $found->file;
     $params = new ImageParams();
     $params->width = 100;
     $params->height = 100;
     /* @var $image Image */
     $scaledName = tempnam('/tmp/', 'image-test') . '.png';
     $image->get($params)->write($scaledName);
     $this->assertTrue(file_exists($scaledName));
     $gd = new GD($scaledName);
     $dimensions = (object) $gd->getCurrentDimensions();
     codecept_debug($dimensions);
     $this->assertSame($params->width, $dimensions->width);
     $this->assertSame($params->height, $dimensions->height);
 }
Пример #3
0
 public function testIfWillDeleteEmbeddedImage()
 {
     $fileName = __DIR__ . '/logo-1024.png';
     $md5 = md5_file($fileName);
     // NOTE: Must work fine even if _id is not set
     $model = new ModelWithEmbeddedImage();
     $model->file = new Image();
     $model->file->set($fileName);
     $em = new EntityManager($model);
     $em->save();
     $finder = new Finder($model);
     $found = $finder->findByPk($model->_id);
     /* @var $found ModelWithEmbeddedImage */
     $file = $found->file->get()->getBytes();
     $this->assertSame($md5, md5($file));
     // Resize image
     $params = new ImageParams();
     $params->width = 100;
     $params->height = 100;
     $resized = $found->file->get($params)->getBytes();
     // Check if was resized
     $this->assertTrue($file > $resized);
     $mangan = new Mangan();
     $gfs = $mangan->getDbInstance()->getGridFS();
     $tmp = $mangan->getDbInstance()->getGridFS(File::TmpPrefix);
     $criteria = ['parentId' => $found->file->_id];
     $this->assertSame(1, $gfs->count($criteria));
     $this->assertSame(1, $tmp->count($criteria));
     $deleted = $found->delete();
     $this->assertTrue($deleted);
     $this->assertSame(0, $gfs->count($criteria));
     $this->assertSame(0, $tmp->count($criteria));
 }
Пример #4
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);
 }
Пример #5
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);
 }
Пример #6
0
 public function testIfWillSaveAndLoadRelatedModel()
 {
     $model = new ModelWithSingleSimpleRelation();
     $id = $model->_id = new MongoId();
     $model->stats = new RelatedStats();
     $model->stats->name = 'one';
     $em = new EntityManager($model);
     $em->save();
     $finder = new Finder($model);
     $found = $finder->findByPk($id);
     $this->assertInstanceOf(RelatedStats::class, $model->stats);
     $this->assertSame($model->stats->name, $found->stats->name);
 }
Пример #7
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);
 }
Пример #8
0
 public function canRollbackTest()
 {
     $model = new ModelTransactional();
     $finder = new Finder($model);
     $tx = new Transaction($model);
     $model->_id = new MongoId();
     $model->title = 'blah';
     $model->save();
     $found = $finder->findByPk($model->_id);
     $this->assertInstanceOf(ModelTransactional::class, $found);
     $tx->rollback();
     $found2 = $finder->findByPk($model->_id);
     $this->assertNull($found2);
 }
Пример #9
0
 public function testIfExistsByCriteriaI18N()
 {
     $langs = ['en', 'pl'];
     $model = new ModelWithI18N();
     $model->setLanguages($langs);
     $model->title = 'foot';
     $finder = new Finder($model);
     $criteria = new Criteria();
     $criteria->title = 'foot';
     // Should not exists
     $this->assertFalse($finder->exists($criteria));
     (new EntityManager($model))->insert();
     // Should exists
     $this->assertTrue($finder->exists($criteria));
 }
Пример #10
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']);
 }
Пример #11
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);
 }
Пример #12
0
 public function testIfWillNoUpdateRefObject()
 {
     $model = new ModelWithNotUpdatableDbRef();
     $stats = new SimplePlainDbRef();
     $stats->name = 'one';
     $statsId = $stats->_id = new MongoId();
     $saved = (new EntityManager($stats))->save();
     $this->assertTrue($saved);
     $model->stats = $stats;
     $statsFinder = new Finder($stats);
     $found = $statsFinder->findByPk($statsId);
     $this->assertSame('one', $found->name);
     $model->stats->name = 'two';
     $saved2 = (new EntityManager($model))->save();
     $this->assertTrue($saved2);
     $found = $statsFinder->findByPk($statsId);
     $this->assertSame('one', $found->name);
 }
Пример #13
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);
 }
Пример #14
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');
 }
Пример #15
0
 public function testIfWillStoreI18NFields()
 {
     $model = new ModelWithI18N();
     $model->setLanguages(['en', 'pl']);
     $model->_id = new MongoId();
     $model->setLang('en');
     $model->title = 'english';
     $model->active = true;
     $em = new EntityManager($model);
     $em->save();
     $finder = new Finder($model);
     $found = $finder->findByPk($model->_id);
     $this->assertNotNull($found);
     $found->setLang('en');
     $this->assertSame($model->title, $found->title);
     $this->assertSame($model->active, $found->active);
     $found->setLang('pl');
     $model->title = 'english';
     $model->active = true;
 }
Пример #16
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');
 }
Пример #17
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);
     }
 }
Пример #18
0
 public function testIfWillUpdateByModifyDocument()
 {
     $model = new ModelWithI18N();
     $model->_id = new \MongoId();
     $model->active = true;
     $model->title = 'foo';
     $em = new EntityManager($model);
     $finder = new Finder($model);
     $em->save();
     $found = $finder->findByPk($model->_id);
     $this->assertSame($model->title, $found->title);
     $this->assertSame($model->active, $found->active);
     $em = new EntityManager($found);
     $found->title = 'bar';
     // This attribute should be ignored
     $found->active = false;
     $em->update(['title'], true);
     $updated = $finder->findByPk($model->_id);
     $this->assertSame($found->title, $updated->title);
     $this->assertSame($model->active, $updated->active);
 }
Пример #19
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);
         }
     }
 }
Пример #20
0
 public function testIfWillResolveNotFoundClass()
 {
     define('BogusClass', 'SomeClass');
     $model = new WithPlainEmbedded();
     $model->_id = new MongoId();
     $model->stats = new SimplePlainEmbedded();
     $em = new EntityManager($model);
     $em->save();
     $pkCriteria = PkManager::prepareFromModel($model)->getConditions();
     $set = ['$set' => ['stats._class' => BogusClass]];
     $em->getCollection()->update($pkCriteria, $set);
     $finder = new Finder($model);
     try {
         $finder->findByPk($model->_id);
         $this->assertFalse(true);
     } catch (ManganException $ex) {
         $this->assertTrue(true);
     }
     // Attach class not found handlers
     new NotFoundResolver($model, [BogusClass => SimplePlainEmbedded::class]);
     $found = $finder->findByPk($model->_id);
     $this->assertInstanceOf(WithPlainEmbedded::class, $found);
     $this->assertInstanceOf(SimplePlainEmbedded::class, $found->stats);
 }
Пример #21
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);
 }
Пример #22
0
 /**
  * Fetches the data from the persistent data storage.
  * @return Document[]|Cursor list of data items
  * @since v1.0
  */
 protected function fetchData()
 {
     $criteria = $this->configureFetch();
     // Finally apply all to finder
     return $this->finder->findAll($criteria);
 }
Пример #23
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']));
     }
 }
Пример #24
0
 /**
  * Get finder instance
  * @return FinderInterface
  */
 private function _getFinder()
 {
     if (null === $this->_finder) {
         $this->_finder = Finder::create($this);
     }
     return $this->_finder;
 }
Пример #25
0
 public function __construct($model, $em = null)
 {
     parent::__construct($model, $em);
     // Cannot use cursors in raw finder, as it will clash with PkManager
     $this->withCursor(false);
 }
Пример #26
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);
 }
Пример #27
0
 /**
  * Restore trashed item
  * @return boolean
  * @throws Exception
  * @Ignored
  */
 public function restore()
 {
     if (!$this instanceof TrashInterface) {
         // When trying to restore normal document instead of trash item
         throw new Exception(sprintf('Restore can be performed only on `%s` instance', TrashInterface::class));
     }
     $em = new EntityManager($this->data);
     // Set scenario to `restore` for model, which is just about to be restored
     ScenarioManager::setScenario($this->data, TrashInterface::ScenarioRestore);
     if (!Event::valid($this->data, TrashInterface::EventBeforeRestore)) {
         return false;
     }
     $saved = $em->save();
     if (!$saved) {
         return false;
     }
     $finder = new Finder($this->data);
     $model = $finder->find(PkManager::prepareFromModel($this->data));
     if (!$model) {
         return false;
     }
     $eventAfter = new RestoreEvent();
     $eventAfter->setTrashed($this->data);
     $eventAfter->setTrash($this);
     if (!Event::valid($model, TrashInterface::EventAfterRestore, $eventAfter)) {
         return false;
     }
     $trashEm = new EntityManager($this);
     $this->data = null;
     // Use deleteOne, to avoid beforeDelete event,
     // which should be raised only when really removing document:
     // when emtying trash
     return $trashEm->deleteOne(PkManager::prepareFromModel($this));
 }
Пример #28
0
 /**
  * Validates the attribute of the object.
  * If there is any error, the error message is added to the object.
  * @param AnnotatedInterface $model the object being validated
  * @param string $attribute the attribute being validated
  */
 public function isValid(AnnotatedInterface $model, $attribute)
 {
     $value = $model->{$attribute};
     if ($this->allowEmpty && empty($value)) {
         return true;
     }
     $className = empty($this->className) ? get_class($model) : $this->className;
     $compareModel = new $className();
     $criteria = (new Criteria())->decorateWith($compareModel);
     $criteria->addCond($attribute, '==', $value);
     if ($this->criteria !== []) {
         $criteria->mergeWith($this->criteria);
     }
     ScenarioManager::setScenario($compareModel, ValidatorInterface::ScenarioValidate);
     $finder = new Finder($compareModel);
     $found = $finder->find($criteria);
     // Not found entirely
     if (null === $found) {
         return true;
     }
     // Same pk
     if (PkManager::compare($found, $model)) {
         return true;
     }
     $label = ManganMeta::create($model)->field($attribute)->label;
     $this->addError('msgTaken', ['{attribute}' => $label, '{value}' => $value]);
     return false;
 }