public function testIfWillCompareModel() { $langs = []; $model = new WithEmbeddedArrayI18NModel(); // Attach first $m = new ModelWithI18N(); $m->setLang('en'); $m->_id = new \MongoId(); $m->setLanguages($langs); $m->layout = 'new'; $m->title = 'New York'; $m->setLang('pl'); $m->title = 'Nowy Jork'; $m->setLang('en'); $model->pages[] = $m; // Attach second $m2 = new ModelWithI18N(); $m2->setLang('en'); $m2->_id = new \MongoId(); $m2->setLanguages($langs); $m2->layout = 'new'; $m2->title = 'Prague'; $m2->setLang('pl'); $m2->title = 'Praga'; $m2->setLang('en'); $model->pages[] = $m2; $comparator = new ModelComparator($this); $data = RawArray::fromModel($model); foreach ([0, 1] as $i) { $this->assertSame($model->pages[$i]->_id, $data['pages'][$i]['_id']); $this->assertSame($model->pages[$i]->title, $data['pages'][$i]['title']['en']); $this->assertSame($model->pages[$i]->layout, $data['pages'][$i]['layout']); } $comparator->compare($data, $model); }
public function testToArray() { $model = new BaseAttributesNoAnnotations(); $array = RawArray::fromModel($model); $this->assertSame($model->int, $array['int']); $this->assertSame($model->string, $array['string']); $this->assertSame($model->bool, $array['bool']); $this->assertSame($model->float, $array['float']); $this->assertSame($model->array, $array['array']); $this->assertSame($model->null, $array['null']); }
public function testIfWillConvertToArrayWithEmbeddedDocuments() { $model = new WithPlainEmbedded(); $model->title = 'stats'; $model->stats = new SimplePlainEmbedded(); $model->stats->active = true; $model->stats->name = 'foo'; $model->stats->visits = 1233; $raw = RawArray::fromModel($model); $this->assertSame($raw['title'], $model->title); foreach (['active', 'name', 'visits'] as $name) { $this->assertSame($model->stats->{$name}, $raw['stats'][$name]); } }
/** * This is use case, where model is initialized from external JSON * And order of elements are changed in json. */ public function testIfWillProperlyStoreI18NFieldsWhenChangedOrderFromExternalSource() { $langs = ['en', 'pl']; $model = new WithEmbeddedArrayI18NModel(); // Attach single $one = new ModelWithI18N(); $one->setLanguages($langs); $one->layout = 'new'; $one->title = 'New York'; $one->setLang('pl'); $one->title = 'Nowy Jork'; $one->setLang('en'); $model->page = $one; // Attach first $m = new ModelWithI18N(); $m->setLanguages($langs); $m->layout = 'new'; $m->title = 'New York'; $m->setLang('pl'); $m->title = 'Nowy Jork'; $m->setLang('en'); $model->pages[] = $m; // Attach second $m = new ModelWithI18NSecond(); $m->setLanguages($langs); $m->layout = 'new'; $m->title = 'Prague'; $m->setLang('pl'); $m->title = 'Praga'; $m->setLang('en'); $model->pages[] = $m; // This are expected values $expectedData = RawArray::fromModel($model); $expectedData['pages'] = array_reverse($expectedData['pages']); // Now assume that external json data arrived $externalData = JsonArray::fromModel($model); $externalData['pages'] = array_reverse($externalData['pages']); $expectedModel = JsonArray::toModel($externalData, $model, $model); $comparator = new ModelComparator($this); $comparator->compare($expectedData, $expectedModel); $raw = RawArray::fromModel($expectedModel); }
/** * Replaces the current document. * * **NOTE: This will overwrite entire document.** * Any filtered out properties will be removed as well. * * The record is inserted as a documnent into the database collection, if exists it will be replaced. * * Validation will be performed before saving the record. If the validation fails, * the record will not be saved. You can call {@link getErrors()} to retrieve the * validation errors. * * @param boolean $runValidation whether to perform validation before saving the record. * If the validation fails, the record will not be saved to database. * * @return boolean whether the saving succeeds * @since v1.0 */ public function replace($runValidation = true) { if (!$runValidation || $this->validator->validate()) { $model = $this->model; if ($this->_beforeSave($model)) { $data = RawArray::fromModel($model); $rawResult = $this->_collection->save($data, $this->options->getSaveOptions()); $result = $this->_result($rawResult, true); if ($result) { $this->_afterSave($model); return true; } throw new ManganException("Can't save the document to disk, or attempting to save an empty document"); } return false; } else { return false; } }