public function testToArrayIncludesTheResourcesRepresentation() { $post = (object) ['id' => 1, 'foo' => 'bar']; $resource = new Resource($post, new PostSerializer2()); $document = new Document($resource); $this->assertEquals(['data' => $resource->toArray()], $document->toArray()); }
public function testToArrayReturnsArrayOfResources() { $serializer = new PostSerializer3(); $post1 = (object) ['id' => 1, 'foo' => 'bar']; $post2 = new Resource((object) ['id' => 2, 'foo' => 'baz'], $serializer); $collection = new Collection([$post1, $post2], $serializer); $resource1 = new Resource($post1, $serializer); $resource2 = $post2; $this->assertEquals([$resource1->toArray(), $resource2->toArray()], $collection->toArray()); }
public function testCanMergeWithAnotherResource() { $post1 = (object) ['id' => '123', 'foo' => 'bar', 'comments' => [1]]; $post2 = (object) ['id' => '123', 'baz' => 'qux', 'comments' => [1, 2]]; $resource1 = new Resource($post1, new PostSerializer4()); $resource2 = new Resource($post2, new PostSerializer4()); $resource1->with(['comments']); $resource2->with(['comments']); $resource1->merge($resource2); $this->assertEquals(['type' => 'posts', 'id' => '123', 'attributes' => ['baz' => 'qux', 'foo' => 'bar'], 'relationships' => ['comments' => ['data' => [['type' => 'comments', 'id' => '1'], ['type' => 'comments', 'id' => '2']]]]], $resource1->toArray()); }