|
3 | 3 | namespace FreeElephants\JsonApi\DTO; |
4 | 4 |
|
5 | 5 | use FreeElephants\JsonApi\AbstractTestCase; |
| 6 | +use FreeElephants\JsonApi\DTO\Exception\UnexpectedValueException; |
6 | 7 | use Nyholm\Psr7\ServerRequest; |
7 | 8 |
|
8 | 9 | class DocumentTest extends AbstractTestCase |
9 | 10 | { |
10 | 11 |
|
11 | | - public function testFromRequest() |
| 12 | + public function testFromRequest(): void |
12 | 13 | { |
13 | 14 | $request = new ServerRequest('POST', '/foo'); |
14 | 15 | $rawJson = <<<JSON |
@@ -56,6 +57,94 @@ public function testFromRequest() |
56 | 57 |
|
57 | 58 | $this->assertJsonStringEqualsJsonString($rawJson, json_encode($fooDTO)); |
58 | 59 | } |
| 60 | + |
| 61 | + public function testFromRequestWithUnexpectedAttributes(): void |
| 62 | + { |
| 63 | + $request = new ServerRequest('POST', '/foo'); |
| 64 | + $rawJson = <<<JSON |
| 65 | +{ |
| 66 | + "data": { |
| 67 | + "id": "123", |
| 68 | + "type": "foo", |
| 69 | + "attributes": { |
| 70 | + "foo": "bar", |
| 71 | + "date": "2012-04-23T18:25:43.511+03:00", |
| 72 | + "unexpectedAttribute": true, |
| 73 | + "nested": { |
| 74 | + "someNestedStructure": { |
| 75 | + "someKey": "someValue" |
| 76 | + } |
| 77 | + }, |
| 78 | + "nullableObjectField": null, |
| 79 | + "nullableScalarField": null, |
| 80 | + "nullableScalarFilledField": "baz" |
| 81 | + }, |
| 82 | + "relationships": { |
| 83 | + "baz": { |
| 84 | + "data": { |
| 85 | + "type": "bazs", |
| 86 | + "id": "baz-id" |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | +JSON; |
| 93 | + $request->getBody()->write($rawJson); |
| 94 | + |
| 95 | + $this->expectException(UnexpectedValueException::class); |
| 96 | + FooDocument::fromHttpMessage($request); |
| 97 | + } |
| 98 | + |
| 99 | + public function testFromRequestWithAllowUnexpectedAttributes(): void |
| 100 | + { |
| 101 | + $request = new ServerRequest('POST', '/foo'); |
| 102 | + $rawJson = <<<JSON |
| 103 | +{ |
| 104 | + "data": { |
| 105 | + "id": "123", |
| 106 | + "type": "foo", |
| 107 | + "attributes": { |
| 108 | + "foo": "bar", |
| 109 | + "date": "2012-04-23T18:25:43.511+03:00", |
| 110 | + "unexpectedAttribute": true, |
| 111 | + "nested": { |
| 112 | + "someNestedStructure": { |
| 113 | + "someKey": "someValue" |
| 114 | + } |
| 115 | + }, |
| 116 | + "nullableObjectField": null, |
| 117 | + "nullableScalarField": null, |
| 118 | + "nullableScalarFilledField": "baz" |
| 119 | + }, |
| 120 | + "relationships": { |
| 121 | + "baz": { |
| 122 | + "data": { |
| 123 | + "type": "bazs", |
| 124 | + "id": "baz-id" |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | +JSON; |
| 131 | + $request->getBody()->write($rawJson); |
| 132 | + |
| 133 | + $fooDTO = FooDocument::fromHttpMessage($request, true); |
| 134 | + |
| 135 | + $this->assertInstanceOf(FooResource::class, $fooDTO->data); |
| 136 | + $this->assertInstanceOf(FooAttributes::class, $fooDTO->data->attributes); |
| 137 | + $this->assertSame('foo', $fooDTO->data->type); |
| 138 | + $this->assertSame('bar', $fooDTO->data->attributes->foo); |
| 139 | + $this->assertEquals(new \DateTime('2012-04-23T18:25:43.511+03'), $fooDTO->data->attributes->date); |
| 140 | + $this->assertSame('someValue', $fooDTO->data->attributes->nested->someNestedStructure->someKey); |
| 141 | + $this->assertSame('baz-id', $fooDTO->data->relationships->baz->data->id); |
| 142 | + $this->assertNull($fooDTO->data->attributes->nullableObjectField); |
| 143 | + $this->assertNull($fooDTO->data->attributes->nullableScalarField); |
| 144 | + $this->assertSame('baz', $fooDTO->data->attributes->nullableScalarFilledField); |
| 145 | + |
| 146 | + $this->assertJsonStringNotEqualsJsonString($rawJson, json_encode($fooDTO), 'Ignored attributes not present in resulted dto'); |
| 147 | + } |
59 | 148 | } |
60 | 149 |
|
61 | 150 | class FooDocument extends AbstractDocument |
|
0 commit comments