Exemplo n.º 1
0
 /**
  * This test runs a simple populate on entity with no validators.
  *
  * It tests:
  * - all attribute types
  * - all reference attributes: many2one, one2many and many2many
  * - it tests both on-fly creation and linking with existing entities
  */
 public function testPopulateNoValidationAllNew()
 {
     $many2one = new Many2One();
     $many2one->char = 'many2oneExisting';
     $many2one->save();
     $many2many = new Many2Many();
     $many2many->char = 'many2many1';
     $many2many->save();
     $many2many2 = new Many2Many();
     $many2many2->char = 'many2many2';
     $many2many2->save();
     $data = ['boolean' => true, 'char' => 'char', 'skip' => 'this value will not be set', 'integer' => 12, 'calculation' => 5, 'float' => 20.35, 'date' => '2016-03-14', 'datetime' => '2016-03-14T13:45:20+0000', 'arr' => [1, 2, 3], 'object' => ['key1' => 'value', 'key2' => 12], 'geoPoint' => ['lat' => 50, 'lng' => 100, 'stripThisKey' => 'whatever'], 'many2oneNew' => ['char' => 'many2oneNew'], 'many2oneExisting' => $many2one, 'one2many' => [['char' => 'one2many1'], ['char' => 'one2many2']], 'many2many' => [$many2many->id, $many2many2]];
     $class = Lib\Classes::ENTITY_NO_VALIDATION;
     $entity = new $class();
     $entity->populate($data)->save();
     // Test current $entity state
     $this->assertEntityStateNoValidation($entity);
     // Load entity from DB and test state again
     $id = $entity->id;
     Entity::getInstance()->remove($entity);
     $entity = $class::findById($id);
     $this->assertEntityStateNoValidation($entity);
     // Test toArray conversion
     $array = new ArrayObject($entity->toArray('*,float:2,arr,object[key1],dynamicWithParams:4,many2oneNew[char,relations.integer],one2many,many2many', 2));
     $this->assertEquals('char', $array->keyNested('char'));
     $this->assertEquals(4, $array->keyNested('float'));
     $this->assertArrayNotHasKey('boolean', $array->val());
     $this->assertArrayNotHasKey('skip', $array->val());
     $this->assertEquals([1, 2, 3], $array->keyNested('arr'));
     $this->assertEquals('value', $array->keyNested('object.key1'));
     $this->assertNull($array->keyNested('object.key2'));
     // GeoPoint should return an array of lat/lng values
     $this->assertEquals(50, $array->keyNested('geoPoint.lat'));
     $this->assertEquals(100, $array->keyNested('geoPoint.lng'));
     // If return value of dynamic attribute is AbstractEntity or EntityCollection,
     // EntityDataExtractor should call toArray() an those objects
     $this->assertEquals(24, $array->key('dynamicWithDefaultParams'));
     $this->assertEquals(48, $array->key('dynamicWithParams'));
     $this->assertInternalType('array', $array->key('dynamicEntity'));
     $this->assertInternalType('array', $array->key('dynamicEntityCollection'));
     $this->assertCount(2, $array->key('dynamicEntityCollection'));
     $this->assertEquals('many2oneExisting', $array->keyNested('dynamicEntityCollection.0.char'));
     $this->assertEquals('many2oneNew', $array->keyNested('dynamicEntityCollection.1.char'));
     // GeoPoint attribute should strips all values not related to mongo Point
     $this->assertArrayNotHasKey('stripThisKey', $array->keyNested('geoPoint'));
     $this->assertEquals('many2oneNew', $array->keyNested('many2oneNew.char'));
     $this->assertEquals(12, $array->keyNested('many2oneNew.relations.0.integer'));
     $this->assertEquals('one2many1', $array->keyNested('one2many.0.char'));
     $this->assertEquals('one2many2', $array->keyNested('one2many.1.char'));
     $this->assertEquals('many2many1', $array->keyNested('many2many.0.char'));
     $this->assertEquals('many2many2', $array->keyNested('many2many.1.char'));
 }
Exemplo n.º 2
0
 public function testGetNestedKeys()
 {
     $array = ['k1' => 'test', 'k2' => ['k3' => ['k4' => 'deepest']]];
     $a = new ArrayObject($array);
     $this->assertEquals('deepest', $a->keyNested('k2.k3.k4'));
     $this->assertEquals('default', $a->keyNested('k2.k7', 'default', true));
     $a->keyNested('k3.k7', 'value');
     $this->assertEquals('value', $a->keyNested('k3.k7'));
 }
Exemplo n.º 3
0
 /**
  * Set or get given $value for given $key.
  *
  * @param string $key Key to set/get
  * @param mixed  $value Value to set
  * @param bool   $setOnlyIfNotExists Set default value or not
  */
 private function handleNestedValue($key, $value, $setOnlyIfNotExists)
 {
     $array = $this->val();
     if (strpos($key, '.') !== false) {
         $keys = explode('.', trim($key, '.'), 2);
         if (!isset($array[$keys[0]])) {
             $array[$keys[0]] = [];
         }
         $targetArray = new ArrayObject($array[$keys[0]]);
         $targetArray->keyNested($keys[1], $value, $setOnlyIfNotExists);
         $this->keyNested($keys[0], $targetArray->val());
     } else {
         $array[$key] = $value;
         $this->val($array);
     }
 }
Exemplo n.º 4
0
 public function testStdClassConversion()
 {
     $obj = new \stdClass();
     $obj->name = 'Webiny';
     $obj->language = 'PHP';
     $arr = new ArrayObject($obj);
     $this->assertEquals('Webiny', $arr->key('name'));
     $this->assertEquals('PHP', $arr->key('language'));
     $obj2 = new \stdClass();
     $obj2->url = 'webiny.com';
     $obj->nested = [$obj2];
     $arr = new ArrayObject($obj);
     $this->assertEquals('webiny.com', $arr->keyNested('nested.0.url'));
 }