/** * Register http assertion methods. * * @param Assertion $assertion */ public function __invoke(Assertion $assertion) { /** * Asserts that the allowed headers match the expected array of methods. */ $assertion->addMethod('allow', function ($expected, $message = "") { $this->flag('message', $message); return new AllowedMatcher($expected); }); /** * Asserts that the response has the expected status code. */ $assertion->addMethod('status', function ($expected, $message = "") { $this->flag('message', $message); return new StatusMatcher($expected); }); /** * Property converts response body to json object (or array) and sets it * as the new subject of the assertion chain. */ $assertion->addProperty('json', function ($assoc = false, $depth = 512, $options = 0) { $parser = new JsonParser($this->getActual()); return $this->setActual($parser->getJsonObject($assoc, $depth, $options)); }); }
$parser->getJsonObject(false, 512, 0); })->to->throw('InvalidArgumentException'); }); it('should throw an exception for malformed json', function () { $this->response->setContent('{"name":"brian"'); $parser = new JsonParser($this->response); expect(function () use($parser) { $parser->getJsonObject(false, 512, 0); })->to->throw('InvalidArgumentException'); }); it('should return an associative array if the $assoc parameter is set to true', function () { $this->parser = new JsonParser($this->response); $array = $this->parser->getJsonObject(true, 512, 0); expect($array)->to->equal($this->data); }); it('should throw an exception if the maximum stack depth is exceeded', function () { $this->data['name'] = ['first' => 'austin', 'last' => 'morris']; $this->response->setContent(json_encode($this->data)); $parser = new JsonParser($this->response); expect(function () use($parser) { $parser->getJsonObject(true, 2, 0); })->to->throw('InvalidArgumentException'); }); it('should honor the $options parameter', function () { $this->response->setContent('{"name":"brian","age":987345989834753984759837459837459837422}'); $this->parser = new JsonParser($this->response); $array = $this->parser->getJsonObject(true, 512, JSON_BIGINT_AS_STRING); expect($array['age'])->to->a('string'); }); }); });