|
5 | 5 | namespace Undabot\JsonApi\Tests\Unit\Encoding\PhpArray\Encode; |
6 | 6 |
|
7 | 7 | use PHPUnit\Framework\TestCase; |
8 | | -use Undabot\JsonApi\Definition\Encoding\PhpArrayToLinkCollectionEncoderInterface; |
9 | | -use Undabot\JsonApi\Definition\Encoding\PhpArrayToMetaEncoderInterface; |
| 8 | +use Undabot\JsonApi\Definition\Model\Resource\Relationship\RelationshipCollectionInterface; |
10 | 9 | use Undabot\JsonApi\Implementation\Encoding\Exception\JsonApiEncodingException; |
| 10 | +use Undabot\JsonApi\Implementation\Encoding\PhpArrayToLinkCollectionEncoder; |
| 11 | +use Undabot\JsonApi\Implementation\Encoding\PhpArrayToMetaEncoder; |
11 | 12 | use Undabot\JsonApi\Implementation\Encoding\PhpArrayToRelationshipCollectionEncoder; |
12 | 13 | use Undabot\JsonApi\Implementation\Model\Resource\Relationship\Data\ToManyRelationshipData; |
13 | 14 | use Undabot\JsonApi\Implementation\Model\Resource\Relationship\Relationship; |
| 15 | +use Undabot\JsonApi\Implementation\Model\Resource\ResourceIdentifier; |
14 | 16 |
|
15 | 17 | /** |
16 | 18 | * @internal |
17 | | - * @coversNothing |
| 19 | + * @covers \Undabot\JsonApi\Implementation\Encoding\PhpArrayToRelationshipCollectionEncoder |
18 | 20 | * |
19 | 21 | * @small |
20 | 22 | */ |
21 | 23 | final class PhpArrayToRelationshipCollectionEncoderTest extends TestCase |
22 | 24 | { |
23 | | - /** @var PhpArrayToRelationshipCollectionEncoder */ |
| 25 | + /** @var PhpArrayToRelationshipCollectionEncoder $encoder */ |
24 | 26 | private $encoder; |
25 | 27 |
|
26 | 28 | protected function setUp(): void |
27 | 29 | { |
28 | | - $phpArrayToMetaEncoder = $this->createMock(PhpArrayToMetaEncoderInterface::class); |
29 | | - $phpArrayToLinkCollectionEncoder = $this->createMock(PhpArrayToLinkCollectionEncoderInterface::class); |
| 30 | + $phpArrayToMetaEncoder = new PhpArrayToMetaEncoder(); |
| 31 | + $phpArrayToLinkCollectionEncoder = new PhpArrayToLinkCollectionEncoder(); |
30 | 32 |
|
31 | 33 | $this->encoder = new PhpArrayToRelationshipCollectionEncoder( |
32 | 34 | $phpArrayToMetaEncoder, |
@@ -76,20 +78,43 @@ public function testValidRelationshipsArrayIsEncodedToRelationshipsCollection(): |
76 | 78 |
|
77 | 79 | $relationshipsCollection = $this->encoder->encode($validRelationshipsArray); |
78 | 80 |
|
79 | | - /** @var Relationship singleRelationship */ |
80 | | - $singleRelationship = $relationshipsCollection->getRelationshipByName('fakeResourceName'); |
81 | | - |
82 | | - static::assertCount(1, $relationshipsCollection->getRelationships()); |
| 81 | + $this->validateRelationship($relationshipsCollection, 'fakeResourceName'); |
83 | 82 |
|
84 | | - /** @var ToManyRelationshipData relationshipData */ |
85 | | - $relationshipData = $singleRelationship->getData(); |
| 83 | + /** @var ResourceIdentifier $resourceIdentifier */ |
| 84 | + foreach ($relationshipsCollection->getRelationshipByName('fakeResourceName')->getData()->getData()->getResourceIdentifiers() as $resourceIdentifier) { |
| 85 | + static::assertInstanceOf(ResourceIdentifier::class, $resourceIdentifier); |
| 86 | + static::assertNull($resourceIdentifier->getMeta()); |
| 87 | + } |
| 88 | + } |
86 | 89 |
|
87 | | - static::assertInstanceOf(ToManyRelationshipData::class, $relationshipData); |
| 90 | + public function testValidRelationshipsArrayIsEncodedToRelationshipsCollectionWithMetaAttributesGiven(): void |
| 91 | + { |
| 92 | + $validRelationshipsArray = [ |
| 93 | + 'fakeResourceName' => [ |
| 94 | + 'type' => 'fakeResourceNames', |
| 95 | + 'data' => [ |
| 96 | + ['id' => 'rand-str-Id-1', 'type' => 'fakeResourceName', 'meta' => []], |
| 97 | + ['id' => 'rand-str-Id-2', 'type' => 'fakeResourceName', 'meta' => ['foo' => 'bar']], |
| 98 | + ['id' => 'rand-str-Id-3', 'type' => 'fakeResourceName', 'meta' => ['foo' => ['bar' => 'baz']]], |
| 99 | + ], |
| 100 | + ], |
| 101 | + ]; |
88 | 102 |
|
89 | | - /** @var ToManyRelationshipData $relationships */ |
90 | | - $relationships = $singleRelationship->getData(); |
| 103 | + $relationshipsCollection = $this->encoder->encode($validRelationshipsArray); |
91 | 104 |
|
92 | | - static::assertCount(3, $relationships->getData()); |
| 105 | + $this->validateRelationship($relationshipsCollection, 'fakeResourceName'); |
| 106 | + |
| 107 | + /** @var array<int,ResourceIdentifier> $resourceIdentifiers */ |
| 108 | + $resourceIdentifiers = $relationshipsCollection->getRelationshipByName('fakeResourceName')->getData()->getData()->getResourceIdentifiers(); |
| 109 | + $firstResourceIdentifier = $resourceIdentifiers[0] ?? null; |
| 110 | + $secondResourceIdentifier = $resourceIdentifiers[1] ?? null; |
| 111 | + $thirdResourceIdentifier = $resourceIdentifiers[2] ?? null; |
| 112 | + static::assertInstanceOf(ResourceIdentifier::class, $firstResourceIdentifier); |
| 113 | + static::assertInstanceOf(ResourceIdentifier::class, $secondResourceIdentifier); |
| 114 | + static::assertInstanceOf(ResourceIdentifier::class, $thirdResourceIdentifier); |
| 115 | + static::assertSame([], $firstResourceIdentifier->getMeta()->getData()); |
| 116 | + static::assertSame(['foo' => 'bar'], $secondResourceIdentifier->getMeta()->getData()); |
| 117 | + static::assertSame(['foo' => ['bar' => 'baz']], $thirdResourceIdentifier->getMeta()->getData()); |
93 | 118 | } |
94 | 119 |
|
95 | 120 | public function testMissingRelationshipTypeRaisesException(): void |
@@ -123,4 +148,22 @@ public function testMissingRelationshipIdRaisesException(): void |
123 | 148 | $this->expectExceptionMessage('Resource identifier must have key `id`'); |
124 | 149 | $this->encoder->encode($invalidRelationshipArray); |
125 | 150 | } |
| 151 | + |
| 152 | + private function validateRelationship(RelationshipCollectionInterface $relationshipsCollection, string $relationshipName): void |
| 153 | + { |
| 154 | + /** @var Relationship $singleRelationship */ |
| 155 | + $singleRelationship = $relationshipsCollection->getRelationshipByName($relationshipName); |
| 156 | + |
| 157 | + static::assertCount(1, $relationshipsCollection->getRelationships()); |
| 158 | + |
| 159 | + /** @var ToManyRelationshipData $relationshipData */ |
| 160 | + $relationshipData = $singleRelationship->getData(); |
| 161 | + |
| 162 | + static::assertInstanceOf(ToManyRelationshipData::class, $relationshipData); |
| 163 | + |
| 164 | + /** @var ToManyRelationshipData $relationships */ |
| 165 | + $relationships = $singleRelationship->getData(); |
| 166 | + |
| 167 | + static::assertCount(3, $relationships->getData()); |
| 168 | + } |
126 | 169 | } |
0 commit comments