public function testIfWillPreventSaveOfModelIfNotValid() { $model = new ModelWithRequiredValidator(); $em = new EntityManager($model); $saved = $em->save(); $this->assertFalse($saved); $model->login = '******'; $saved2 = $em->save(); $this->assertTrue($saved2); }
public function testIfWillAllowNullForMongoId() { $model = new ModelWithNullableMongoId(); $em = new EntityManager($model); $em->save(); $this->assertInstanceOf(\MongoId::class, $model->_id); $this->assertNull($model->parentId); $model->parentId = new \MongoId(); $em->save(); $this->assertInstanceOf(\MongoId::class, $model->_id); $this->assertInstanceOf(\MongoId::class, $model->parentId); }
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)); }
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); }
/** * Save referenced model * @param AnnotatedInterface $referenced * @param DbRef $dbRef */ public static function save(AnnotatedInterface $referenced, DbRef $dbRef) { // Ensure ref is same as referenced model PkManager::applyToModel($referenced, $dbRef->pk); $em = new EntityManager($referenced); $em->save(); }
public function testIfWillProperlyStoreAndLoadTree() { $model = new ModelWithEmbedTree(); $id = $model->_id = new MongoId(); $model->name = 'plants'; $model->children = [new ModelWithEmbedTree('trees', [new ModelWithEmbedTree('oaks', [new ModelWithEmbedTree('Quercus'), new ModelWithEmbedTree('Cerris')]), new ModelWithEmbedTree('chestnuts', [new ModelWithEmbedTree('Sativa'), new ModelWithEmbedTree('Castanea')])])]; $this->checkTree($model); $em = new EntityManager($model); $em->save(); $found = (new Finder($model))->findByPk($id); $this->assertSame((string) $id, (string) $found->_id); $this->checkTree($found); }
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); }
public function testIfWillProperlyStoreAndLoadTree() { $model = new ModelWithSimpleTree(); $id = $model->_id = new MongoId(); $model->name = 'plants'; $model->children = [new ModelWithSimpleTree('trees', [new ModelWithSimpleTree('oaks', [new ModelWithSimpleTree('Quercus'), new ModelWithSimpleTree('Cerris')]), new ModelWithSimpleTree('chestnuts', [new ModelWithSimpleTree('Sativa'), new ModelWithSimpleTree('Castanea')])])]; $this->checkTree($model); $em = new EntityManager($model); $em->save(); $found = (new Finder($model))->findByPk($id); $this->assertSame((string) $id, (string) $found->_id); $this->checkTree($found); // Find some sub tree $attributes = ['name' => 'oaks']; $found2 = (new Finder($model))->findByAttributes($attributes); $this->assertNotNull($found2); $this->assertSame(2, count($found2->children), 'That has 2 child nodes'); $this->assertSame('Cerris', $found2->children[1]->name, 'That has 2nd child node of name Cerris'); }
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; }
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); }
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'); }
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); }
/** * 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)); }
public function testIfWillRefreshModelWithI18N() { $model = new ModelWithI18N(); $model->_id = new MongoId(); $model->title = 'title'; $em = new EntityManager($model); $em->save(); $model->title = 'another'; $refreshed = $em->refresh(); $this->assertTrue($refreshed); $this->assertSame('title', $model->title); $em->deleteAll(); $notRefreshed = $em->refresh(); $this->assertFalse($notRefreshed); }