/** * */ public function setUp() { $this->included = new Included(); $this->included->add('post.user_comment'); //will cause error $this->fields = new Fields(); $this->fields->addField('post', 'title'); $this->fields->addField('blog', 'post'); //will cause error $mappings = HelperMapping::complex(); $this->serializer = new JsonApiSerializer(new JsonApiTransformer(new Mapper($mappings))); }
public function testItCanGetWillReturnErrorResponseBecauseOfInvalidParams() { $this->fields = new Fields(); $this->fields->addField('superhero', 'power'); $this->resource = new GetResource($this->serializer, $this->fields, $this->included); $response = $this->resource->get(10, Post::class, $this->findOneCallable); $this->assertInstanceOf(Response::class, $response); $this->assertEquals(400, $response->getStatusCode()); }
public function testItCanGetWillReturnErrorResponseBecauseOfInvalidParams() { $this->fields = new Fields(); $this->fields->addField('superhero', 'power'); $this->resource = new ListResource($this->serializer, $this->page, $this->fields, $this->sorting, $this->included, $this->filters); $response = $this->resource->get($this->totalAmountCallable, $this->resultsCallable, $this->routeUri, Post::class); $this->assertInstanceOf(Response::class, $response); $this->assertEquals(400, $response->getStatusCode()); }
public function testItCanAssertAndThrowExceptionForInvalidQueryParamValues() { $fields = new Fields(); $included = new Included(); $sorting = new Sorting(); $errorBag = new ErrorBag(); $hasError = false; try { $fields->addField('superhero', 'power'); QueryObject::assert($this->serializer, $fields, $included, $sorting, $errorBag, Post::class); } catch (QueryException $e) { $hasError = true; } $this->assertTrue($hasError); }
/** * @return \NilPortugues\Api\JsonApi\Http\Request\Parameters\Fields */ public function getFields() { $fields = (array) $this->getQueryParam('fields', null); $fields = array_filter($fields); $object = new Fields(); foreach ($fields as $type => &$members) { $members = \explode(',', $members); $members = \array_map('trim', $members); foreach ($members as $member) { $object->addField($type, $member); } } return $object; }
/** * */ public function testItWillSerializeToJsonApiAComplexObjectAndFilterFields() { $mappings = HelperMapping::complex(); $mapper = new Mapper($mappings); $expected = <<<JSON { "data":{ "type":"post", "id":"9", "attributes":{ "title":"Hello World" }, "links":{ "self":{ "href":"http://example.com/posts/9" }, "comments":{ "href":"http://example.com/posts/9/comments" } } }, "links":{ "self":{ "href":"http://example.com/posts/9" }, "comments":{ "href":"http://example.com/posts/9/comments" } }, "meta":{ "author":{ "name":"Nil Portugués Calderó", "email":"*****@*****.**" }, "is_devel":true }, "jsonapi":{ "version":"1.0" } } JSON; $post = HelperFactory::complexPost(); $transformer = new JsonApiTransformer($mapper); $transformer->setMeta(['author' => ['name' => 'Nil Portugués Calderó', 'email' => '*****@*****.**']]); $transformer->addMeta('is_devel', true); $fields = new Fields(); $fields->addField('post', 'title'); $this->assertEquals(\json_decode($expected, true), \json_decode((new JsonApiSerializer($transformer))->serialize($post, $fields), true)); }
public function setUp() { $this->fields = new Fields(); $this->fields->addField('employee', 'name'); $this->fields->addField('employee', 'surname'); }