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