Пример #1
0
 public function testIfWillProperlyConvertRawI18NToJson()
 {
     $model = new WithI18N();
     $model->setLanguages(['en', 'pl']);
     $model->setLang('en');
     $model->name = 'January';
     $model->setLang('pl');
     $model->name = 'Styczeń';
     $json = JsonArray::fromModel($model);
     $rawI18N = $json['rawI18N'];
     $this->assertTrue(array_key_exists('name', $rawI18N));
     $this->assertTrue(array_key_exists('en', $rawI18N['name']));
     $this->assertTrue(array_key_exists('pl', $rawI18N['name']));
     $this->assertSame('January', $rawI18N['name']['en']);
     $this->assertSame('Styczeń', $rawI18N['name']['pl']);
 }
Пример #2
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']);
 }
 /**
  * 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);
 }
Пример #4
0
 public function testIfWillConvertToJsonArrayOfDifferentTypeDocuments()
 {
     $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);
     $json = JsonArray::fromModel($found);
     $this->assertNotNull($found);
     $this->assertTrue($found instanceof WithPlainDbRefArray);
     $this->assertSame(count($stats), count($found->stats));
     foreach ($data as $key => $value) {
         $this->assertNotNull($json['stats'][$key]);
         codecept_debug(sprintf('Should be of type: %s', $value['_type']));
         $this->assertSame($json['stats'][$key]['_class'], $value['_type']);
         foreach ($value as $field => $fieldValue) {
             if ($field == '_type') {
                 continue;
             }
             $this->assertSame($json['stats'][$key][$field], $fieldValue);
         }
     }
 }
Пример #5
0
 public function testIfWillConvertDocumentInstanceToJson()
 {
     $src = ['string' => 'Some new string value', 'int' => 15100900, 'float' => 455.34];
     $model = new DocumentBaseAttributes();
     foreach ($src as $field => $value) {
         $model->{$field} = $value;
     }
     $data = JsonArray::fromModel($model);
     foreach ($data as $field => $value) {
         if ($field === '_id' || $field === 'id') {
             $this->assertSame((string) $value, (string) $model->{$field}, "Tested field name is `{$field}`");
         } else {
             $this->assertSame($value, $model->{$field}, "Tested field name is `{$field}`");
         }
     }
     $this->assertTrue(isset($data['_class']));
     $this->assertSame(DocumentBaseAttributes::class, $data['_class']);
     // `meta` property should be ignored
     $this->assertFalse(isset($data['meta']));
 }