public function testSourceParameter()
 {
     $messages = new MessageBag(['foo.bar.baz' => 'Some detail']);
     $expected = new Error();
     $expected->setSourceParameter('filter.foo.bar.baz')->setDetail('Some detail');
     $bag = new ErrorBag($messages, null, 'filter', true);
     $this->assertEquals($expected, current($bag->toArray()));
 }
 public function testAddErrorWithParameter()
 {
     $expected = new Error('123');
     $expected->setDetail('Expecting to see bar as the value');
     $expected->setSourceParameter($param = 'foobar');
     $this->willSee('my-error', '123', 'Expecting to see {foo} as the value');
     $this->assertEquals($expected, $this->addErrorWithParameter('my-error', $param, ['foo' => 'bar']));
     $this->assertError($expected);
 }
 /**
  * ErrorBag constructor.
  * @param MessageBag $messages
  * @param ErrorInterface|null $prototype
  *      an error prototype that the source and detail from the message bag should be set on.
  * @param string|null $sourcePrefix
  * @param bool $isParameter
  *      true to set error source as a parameter, rather than a pointer.
  */
 public function __construct(MessageBag $messages, ErrorInterface $prototype = null, $sourcePrefix = null, $isParameter = false)
 {
     parent::__construct($messages);
     $this->prototype = $prototype ? Error::cast($prototype) : new Error();
     $this->sourcePrefix = $sourcePrefix ? (string) $sourcePrefix : null;
     $this->isParameter = (bool) $isParameter;
 }
 /**
  * @inheritdoc
  */
 public function filterParametersMessages(MessageBag $messages, $statusCode = self::STATUS_INVALID_FILTERS)
 {
     $prototype = $this->repository->error(self::FILTER_PARAMETERS_MESSAGES);
     $prototype = Error::cast($prototype)->setStatus($statusCode);
     $prefix = QueryParametersParserInterface::PARAM_FILTER;
     $errors = new ErrorBag($messages, $prototype, $prefix, true);
     return $errors->toArray();
 }
 /**
  * @param ErrorInterface|string $error
  * @param array $values
  * @return MutableErrorInterface
  *      the error that was added.
  */
 public function addError($error, array $values = [])
 {
     if ($error instanceof ErrorInterface) {
         $error = Error::cast($error);
     } else {
         $error = $this->getErrorRepository()->error($error, $values);
     }
     $this->getErrors()->add($error);
     return $error;
 }
 public function testMerge()
 {
     $a = new BaseError(null, null, 422);
     $b = new Error(null, null, 400);
     $c = new BaseError(null, null, 500);
     $merge = new ErrorCollection();
     $merge->add($a)->add($b);
     $errors = new MutableErrorCollection([$c]);
     $expected = [Error::cast($c), Error::cast($a), $b];
     $this->assertEquals($errors, $errors->merge($merge));
     $this->assertEquals($expected, $errors->getArrayCopy());
 }
 public function testDataAcceptableReturnsErrors()
 {
     $content = '{"data": {"type": "users", "id": "99"}}';
     $document = $this->decode($content);
     $validator = $this->hasOne(false, true, function () {
         $error = new Error();
         $error->setDetail('Foobar');
         return new MutableErrorCollection([$error]);
     });
     $this->assertFalse($validator->isValid($document));
     $this->assertDetailIs($validator->getErrors(), '/data', 'Foobar');
 }
 /**
  * @param $key
  * @param array $values
  * @return Error
  */
 protected function make($key, array $values)
 {
     $error = Error::create($this->get($key));
     if ($this->replacer && $error->hasDetail()) {
         $detail = $this->replacer->replace($error->getDetail(), $values);
         $error->setDetail($detail);
     }
     return $error;
 }
 /**
  * @param ErrorInterface $error
  * @return $this
  */
 public function add(ErrorInterface $error)
 {
     parent::add(Error::cast($error));
     return $this;
 }
Esempio n. 10
0
 public function testMerge()
 {
     $id = '123';
     $aboutLink = new Link('/api/errors/123');
     $status = '500';
     $code = 'error_code';
     $title = 'An Error';
     $detail = 'This is the error detail';
     $source = [Error::SOURCE_POINTER => '/data/attributes'];
     $meta = ['foo' => 'bar'];
     $error = new BaseError($id, $aboutLink, $status, $code, $title, $detail, $source, $meta);
     $expected = new Error($id, [], $status, $code, $title, $detail, $source, $meta);
     $expected->setAboutLink($aboutLink);
     $actual = new Error();
     $actual->merge($error);
     $this->assertEquals($expected, $actual);
 }