|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace JsonApiPhp\JsonApi\Test; |
| 4 | + |
| 5 | +use JsonApiPhp\JsonApi\Attribute; |
| 6 | +use JsonApiPhp\JsonApi\DataDocument; |
| 7 | +use JsonApiPhp\JsonApi\EmptyRelationship; |
| 8 | +use JsonApiPhp\JsonApi\Link\RelatedLink; |
| 9 | +use JsonApiPhp\JsonApi\Link\SelfLink; |
| 10 | +use JsonApiPhp\JsonApi\Meta; |
| 11 | +use JsonApiPhp\JsonApi\NewResourceObject; |
| 12 | +use JsonApiPhp\JsonApi\ResourceIdentifier; |
| 13 | +use JsonApiPhp\JsonApi\ResourceIdentifierCollection; |
| 14 | +use JsonApiPhp\JsonApi\ToMany; |
| 15 | +use JsonApiPhp\JsonApi\ToNull; |
| 16 | +use JsonApiPhp\JsonApi\ToOne; |
| 17 | + |
| 18 | +class NewResourceObjectTest extends BaseTestCase |
| 19 | +{ |
| 20 | + public function testFullFledgedResourceObject() |
| 21 | + { |
| 22 | + $this->assertEncodesTo( |
| 23 | + ' |
| 24 | + { |
| 25 | + "data": { |
| 26 | + "type": "apples", |
| 27 | + "attributes": { |
| 28 | + "title": "Rails is Omakase" |
| 29 | + }, |
| 30 | + "meta": {"foo": "bar"}, |
| 31 | + "relationships": { |
| 32 | + "author": { |
| 33 | + "meta": {"foo": "bar"}, |
| 34 | + "data": null |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + ', |
| 40 | + new DataDocument( |
| 41 | + new NewResourceObject( |
| 42 | + 'apples', |
| 43 | + new Meta('foo', 'bar'), |
| 44 | + new Attribute('title', 'Rails is Omakase'), |
| 45 | + new ToNull( |
| 46 | + 'author', |
| 47 | + new Meta('foo', 'bar') |
| 48 | + ) |
| 49 | + ) |
| 50 | + ) |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + public function testRelationshipWithSingleIdLinkage() |
| 55 | + { |
| 56 | + $this->assertEncodesTo( |
| 57 | + ' |
| 58 | + { |
| 59 | + "data": { |
| 60 | + "type": "basket", |
| 61 | + "relationships": { |
| 62 | + "content": { |
| 63 | + "data": {"type": "apples", "id": "1"} |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + ', |
| 69 | + new DataDocument( |
| 70 | + new NewResourceObject( |
| 71 | + 'basket', |
| 72 | + new ToOne('content', new ResourceIdentifier('apples', '1')) |
| 73 | + ) |
| 74 | + ) |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + public function testRelationshipWithMultiIdLinkage() |
| 79 | + { |
| 80 | + $this->assertEncodesTo( |
| 81 | + ' |
| 82 | + { |
| 83 | + "data": { |
| 84 | + "type": "basket", |
| 85 | + "relationships": { |
| 86 | + "content": { |
| 87 | + "data": [{ |
| 88 | + "type": "apples", |
| 89 | + "id": "1" |
| 90 | + },{ |
| 91 | + "type": "pears", |
| 92 | + "id": "2" |
| 93 | + }] |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + ', |
| 99 | + new DataDocument( |
| 100 | + new NewResourceObject( |
| 101 | + 'basket', |
| 102 | + new ToMany( |
| 103 | + 'content', |
| 104 | + new ResourceIdentifierCollection( |
| 105 | + new ResourceIdentifier('apples', '1'), |
| 106 | + new ResourceIdentifier('pears', '2') |
| 107 | + ) |
| 108 | + ) |
| 109 | + ) |
| 110 | + ) |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + public function testRelationshipWithEmptyMultiIdLinkage() |
| 115 | + { |
| 116 | + $this->assertEncodesTo( |
| 117 | + ' |
| 118 | + { |
| 119 | + "data": { |
| 120 | + "type": "basket", |
| 121 | + "relationships": { |
| 122 | + "content": { |
| 123 | + "data": [] |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + ', |
| 129 | + new DataDocument( |
| 130 | + new NewResourceObject( |
| 131 | + 'basket', |
| 132 | + new ToMany('content', new ResourceIdentifierCollection()) |
| 133 | + ) |
| 134 | + ) |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + public function testRelationshipWithNoData() |
| 139 | + { |
| 140 | + $this->assertEncodesTo( |
| 141 | + ' |
| 142 | + { |
| 143 | + "data": { |
| 144 | + "type": "basket", |
| 145 | + "relationships": { |
| 146 | + "empty": { |
| 147 | + "links": { |
| 148 | + "related": "/foo" |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + ', |
| 155 | + new DataDocument( |
| 156 | + new NewResourceObject( |
| 157 | + 'basket', |
| 158 | + new EmptyRelationship('empty', new RelatedLink('/foo')) |
| 159 | + ) |
| 160 | + ) |
| 161 | + ); |
| 162 | + |
| 163 | + $this->assertEncodesTo( |
| 164 | + ' |
| 165 | + { |
| 166 | + "data": { |
| 167 | + "type": "basket", |
| 168 | + "relationships": { |
| 169 | + "empty": { |
| 170 | + "links": { |
| 171 | + "related": "/foo", |
| 172 | + "self": "/bar" |
| 173 | + }, |
| 174 | + "meta": { |
| 175 | + "foo": "bar" |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + ', |
| 182 | + new DataDocument( |
| 183 | + new NewResourceObject( |
| 184 | + 'basket', |
| 185 | + new EmptyRelationship('empty', new RelatedLink('/foo'), new SelfLink('/bar'), new Meta('foo', 'bar')) |
| 186 | + ) |
| 187 | + ) |
| 188 | + ); |
| 189 | + } |
| 190 | + |
| 191 | + public function testResourceFieldsMustBeUnique() |
| 192 | + { |
| 193 | + $this->expectException(\LogicException::class); |
| 194 | + $this->expectExceptionMessage("Field 'foo' already exists"); |
| 195 | + new NewResourceObject( |
| 196 | + 'apples', |
| 197 | + new Attribute('foo', 'bar'), |
| 198 | + new ToOne('foo', new ResourceIdentifier('apples', '1')) |
| 199 | + ); |
| 200 | + } |
| 201 | + |
| 202 | + public function testNameValidation() |
| 203 | + { |
| 204 | + $this->expectException(\DomainException::class); |
| 205 | + new NewResourceObject('invalid:id'); |
| 206 | + } |
| 207 | +} |
0 commit comments