Ejemplo 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'));
 }
Ejemplo n.º 2
0
 /**
  * Returns an array of all routes within the collection.
  *
  * @return array
  */
 public function all()
 {
     return $this->routes->val();
 }
Ejemplo n.º 3
0
 /**
  * Get all session values.
  *
  * @return array Key-value array of all session entries.
  */
 public function getAll()
 {
     return $this->sessionBag->val();
 }
Ejemplo n.º 4
0
 /**
  * Returns the current cache control headers as an array.
  *
  * @return array
  */
 public function getCacheControl()
 {
     return $this->cacheControl->val();
 }
Ejemplo n.º 5
0
 /**
  * Set url query param.
  *
  * @param StringObject|ArrayObject|string|array $query  Query params.
  * @param bool                                  $append Do you want to append or overwrite current query param.
  *                                                      In case when you are appending values, the values from $query,
  *                                                      that already exist in the current query, will be overwritten
  *                                                      by the ones inside the $query.
  *
  * @throws UrlObjectException
  * @return $this
  */
 public function setQuery($query, $append = false)
 {
     if ($this->isStdObject($query)) {
         $query = $query->val();
     }
     if ($append && $this->query != '') {
         if ($this->isString($this->query)) {
             $currentQuery = new StringObject($this->query);
             $currentQuery = $currentQuery->parseString();
         } else {
             $currentQuery = new ArrayObject($this->query);
         }
         if ($this->isStdObject($query)) {
             if (StdObjectWrapper::isArrayObject($append)) {
                 $query = $query->val();
             } else {
                 if (StdObjectWrapper::isStringObject($query)) {
                     $query = $query->parseString()->val();
                 } else {
                     throw new UrlObjectException(UrlObjectException::MSG_INVALID_ARG, ['$query', 'StringObject|ArrayObject|string|array']);
                 }
             }
         } else {
             if ($this->isString($query)) {
                 $query = new StringObject($query);
                 $query = $query->parseString()->val();
             }
         }
         $currentQuery->merge($query);
         $query = $currentQuery->val();
     }
     $this->query = $query;
     $this->rebuildUrl();
     return $this;
 }
Ejemplo n.º 6
0
 /**
  * @dataProvider arraySet1
  */
 public function testDiffKeys($array)
 {
     $compareArray = ['k1', 'k2'];
     $a = new ArrayObject($array);
     @$a->diffKeys($compareArray);
     @($array = array_diff_key($array, $compareArray));
     $this->assertSame($array, $a->val());
 }
Ejemplo n.º 7
0
 public function testArrayObjectConstructor()
 {
     $arrayObject = new ArrayObject(['foo' => 'value']);
     $jr = new JsonResponse($arrayObject);
     $this->assertSame(json_encode($arrayObject->val()), $jr->getContent());
 }
Ejemplo n.º 8
0
 /**
  * (PHP 5 &gt;= 5.0.0)<br/>
  * Retrieve an external iterator
  * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
  * @return Traversable An instance of an object implementing <b>Iterator</b> or
  * <b>Traversable</b>
  */
 public function getIterator()
 {
     return new \ArrayIterator($this->data->val());
 }
Ejemplo n.º 9
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);
     }
 }
Ejemplo n.º 10
0
 /**
  * Returns all the attributes.
  *
  * @return array
  */
 public function getAttributes()
 {
     return $this->attributes->val();
 }
Ejemplo n.º 11
0
 /**
  * Returns an array of current headers.
  *
  * @return array
  */
 public function getHeaders()
 {
     return $this->headers->val();
 }