Example #1
0
 /**
  * Test a GET request for a RestfulItem with fields specified to return.
  */
 public function testGetWithFields()
 {
     /** @var MockPersistent $itemObj */
     $itemObj = MockPersistent::findOne(['name' => 'MockPersistent #1'], [], $this->fixture);
     $this->mockEnvironment(['PATH_INFO' => '/collection/' . $itemObj->_id, 'QUERY_STRING' => 'fields=name,text', 'REQUEST_METHOD' => 'GET']);
     try {
         $response = $this->tacit->invoke();
         $item = json_decode($response->getBody(), true);
         $data = $itemObj->toArray(Collection::getMask($itemObj, ['integer', 'float', 'date', 'arrayOfStrings']));
         $matches = array_uintersect_assoc($data, $item, array($this, 'compareMultidimensionalArray'));
         $this->assertEquals($data, $matches);
     } catch (RestfulException $e) {
         $this->fail($e->getMessage());
     }
 }
Example #2
0
 /**
  * Hydrate this instance using the provided data and an optional mask.
  *
  * @param array $data
  * @param bool|array $mask
  */
 public function hydrate(array $data, $mask = false)
 {
     if (!is_array($mask) && false !== $mask) {
         $mask = Collection::getMask($this, [], ['_id']);
     }
     foreach ($data as $key => $value) {
         if (false === $mask || in_array($key, $mask)) {
             $this->{$key} = $value;
             $this->_dirty[$key] = $key;
         }
     }
     if (!$this->isNew()) {
         $this->_dirty = array();
     }
 }
Example #3
0
 /**
  * PUT the representation of this item specified in the request entity.
  *
  * NOTE: This will replace the item with the properties specified in request
  * entity. Properties not provided will be reset to default values for the item.
  */
 public function put()
 {
     /** @var \Tacit\Model\Persistent $modelClass */
     $modelClass = static::$modelClass;
     $criteria = $this->criteria(func_get_args());
     /** @var \Tacit\Model\Persistent $item */
     $item = $modelClass::findOne($criteria, [], $this->app->container->get('repository'));
     if (null === $item) {
         throw new NotFoundException($this);
     }
     try {
         /** @var \Tacit\Model\Persistent $newItem */
         $newItem = new $modelClass();
         $data = array_replace_recursive(array_filter($newItem->toArray(), [$this, 'filterNull']), $this->app->request->post(null, []));
         $data = $this->putBeforeSet($data);
         $item->fromArray($data, Collection::getMask($item));
         $item->save();
     } catch (OperationalException $e) {
         $e->next($this);
     } catch (ModelValidationException $e) {
         throw new UnacceptableEntityException($this, 'Resource validation failed', $e->getMessage(), $e->getMessages(), $e);
     } catch (\Exception $e) {
         throw new ServerErrorException($this, 'Error updating resource', $e->getMessage(), null, $e);
     }
     $this->respondWithItem($item, new static::$transformer());
 }
Example #4
0
 /**
  * Test Collection::getMask() with an object instance.
  *
  * @param array $expected
  * @param array $exclude
  *
  * @dataProvider providerGetMaskFromClass
  */
 public function testGetMaskFromClass(array $expected, array $exclude)
 {
     $this->assertEquals($expected, array_values(Collection::getMask('Tacit\\Test\\Model\\MockPersistent', $exclude)));
 }