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); }
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); }
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); }
public function testIfWillDeleteAllPlainDocumentsByPk() { $pks = []; $model = new PlainWithBasicAttributes(); $model->_id = new MongoId(); $pks[] = $model->_id; $em = new EntityManager($model); $em->save(); $model = new PlainWithBasicAttributes(); $model->_id = new MongoId(); $pks[] = $model->_id; $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(3, $count); $em->deleteAllByPk($pks); $found = $finder->findByPk($model->_id); $this->assertInstanceOf(PlainWithBasicAttributes::class, $found); $countAfterDelete = $finder->count(); $this->assertSame(1, $countAfterDelete); }
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); }
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 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 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); }
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); }
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']); }
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 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); } } }
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); }