- md5
- 93ae9628a0fb264c956e1e6021efd8f8
- sha1
- 5982ecd024a97dd715ca8e1980ff28be2d6474b4
- sha256
- 6de3647d9b01e9ff33f8513147f5917fe22c5625f564f035f05b3e75f5df2e24
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
<?php
/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tobscure\Tests\JsonApi;
use Tobscure\JsonApi\AbstractSerializer;
use Tobscure\JsonApi\Collection;
use Tobscure\JsonApi\Document;
use Tobscure\JsonApi\Relationship;
use Tobscure\JsonApi\Resource;
/**
* This is the document test class.
*
* @author Toby Zerner <[email protected]>
*/
class DocumentTest extends AbstractTestCase
{
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 testItCanBeSerializedToJson()
{
$this->assertEquals('[]', (string) new Document());
}
public function testToArrayIncludesIncludedResources()
{
$comment = (object) ['id' => 1, 'foo' => 'bar'];
$post = (object) ['id' => 1, 'foo' => 'bar', 'comments' => [$comment]];
$resource = new Resource($post, new PostSerializer2);
$includedResource = new Resource($comment, new CommentSerializer2);
$document = new Document($resource->with('comments'));
$this->assertEquals([
'data' => $resource->toArray(),
'included' => [
$includedResource->toArray()
]
], $document->toArray());
}
public function testNoEmptyAttributes()
{
$post = (object) [
'id' => 1,
];
$resource = new Resource($post, new PostSerializerEmptyAttributes2);
$document = new Document($resource);
$this->assertEquals('{"data":{"type":"posts","id":"1"}}', (string) $document, 'Attributes should be omitted');
}
}
class PostSerializer2 extends AbstractSerializer
{
protected $type = 'posts';
public function getAttributes($post, array $fields = null)
{
return ['foo' => $post->foo];
}
public function comments($post)
{
return new Relationship(new Collection($post->comments, new CommentSerializer2));
}
}
class PostSerializerEmptyAttributes2 extends PostSerializer2
{
public function getAttributes($post, array $fields = null)
{
return [];
}
}
class CommentSerializer2 extends AbstractSerializer
{
protected $type = 'comments';
public function getAttributes($comment, array $fields = null)
{
return ['foo' => $comment->foo];
}
}
The same content is in 9 releases, Joomla 4.1.0-rc1 to 4.1.2. See them all, or every version of this file.