|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Swis\JsonApi\Client\Tests; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Swis\JsonApi\Client\Collection; |
| 7 | +use Swis\JsonApi\Client\Document; |
| 8 | +use Swis\JsonApi\Client\Errors\ErrorCollection; |
| 9 | +use Swis\JsonApi\Client\Item; |
| 10 | + |
| 11 | +class DocumentTest extends TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @test |
| 15 | + */ |
| 16 | + public function it_returns_only_filled_properties_in_toArray() |
| 17 | + { |
| 18 | + $document = new Document(); |
| 19 | + |
| 20 | + $this->assertEquals([], $document->toArray()); |
| 21 | + |
| 22 | + $document->setLinks( |
| 23 | + [ |
| 24 | + 'self' => 'http://example.com/articles', |
| 25 | + 'next' => 'http://example.com/articles?page[offset]=2', |
| 26 | + 'last' => 'http://example.com/articles?page[offset]=10', |
| 27 | + ] |
| 28 | + ); |
| 29 | + $document->setData( |
| 30 | + (new Item(['title' => 'JSON:API paints my bikeshed!'])) |
| 31 | + ->setType('articles') |
| 32 | + ->setId(1) |
| 33 | + ); |
| 34 | + $document->setIncluded( |
| 35 | + new Collection( |
| 36 | + [ |
| 37 | + (new Item(['firstName' => 'Dan', 'lastName' => 'Gebhardt', 'twitter' => 'dgeb'])) |
| 38 | + ->setType('people') |
| 39 | + ->setId(9), |
| 40 | + ] |
| 41 | + ) |
| 42 | + ); |
| 43 | + $document->setMeta(['copyright' => 'Copyright 2015 Example Corp.']); |
| 44 | + $document->setErrors( |
| 45 | + new ErrorCollection( |
| 46 | + [ |
| 47 | + ['id' => 'error-1'], |
| 48 | + ] |
| 49 | + ) |
| 50 | + ); |
| 51 | + $document->setJsonapi(['version' => '1.0']); |
| 52 | + |
| 53 | + $this->assertEquals( |
| 54 | + [ |
| 55 | + 'links' => [ |
| 56 | + 'self' => 'http://example.com/articles', |
| 57 | + 'next' => 'http://example.com/articles?page[offset]=2', |
| 58 | + 'last' => 'http://example.com/articles?page[offset]=10', |
| 59 | + ], |
| 60 | + 'data' => [ |
| 61 | + 'type' => 'articles', |
| 62 | + 'id' => 1, |
| 63 | + 'attributes' => [ |
| 64 | + 'title' => 'JSON:API paints my bikeshed!', |
| 65 | + ], |
| 66 | + ], |
| 67 | + 'included' => [ |
| 68 | + [ |
| 69 | + 'type' => 'people', |
| 70 | + 'id' => 9, |
| 71 | + 'attributes' => [ |
| 72 | + 'firstName' => 'Dan', |
| 73 | + 'lastName' => 'Gebhardt', |
| 74 | + 'twitter' => 'dgeb', |
| 75 | + ], |
| 76 | + ], |
| 77 | + ], |
| 78 | + 'meta' => [ |
| 79 | + 'copyright' => 'Copyright 2015 Example Corp.', |
| 80 | + ], |
| 81 | + 'errors' => [ |
| 82 | + [ |
| 83 | + 'id' => 'error-1', |
| 84 | + ], |
| 85 | + ], |
| 86 | + 'jsonapi' => [ |
| 87 | + 'version' => '1.0', |
| 88 | + ], |
| 89 | + ], |
| 90 | + $document->toArray() |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
0 commit comments