66
77use alsvanzelf \jsonapi \exceptions \DuplicateException ;
88use alsvanzelf \jsonapi \exceptions \Exception ;
9+ use alsvanzelf \jsonapi \exceptions \InputException ;
910use alsvanzelf \jsonapi \interfaces \ExtensionInterface ;
1011use alsvanzelf \jsonapi \objects \ResourceIdentifierObject ;
12+ use alsvanzelf \jsonapi \objects \ResourceObject ;
1113use PHPUnit \Framework \TestCase ;
1214
1315class ResourceIdentifierObjectTest extends TestCase {
@@ -55,6 +57,35 @@ public function testSetLocalId_WithIdAlreadySet(): void {
5557 $ resourceIdentifierObject ->setLocalId ('uuid-1 ' );
5658 }
5759
60+ public function testFromResourceObject_HappyPath (): void {
61+ $ resource = new ResourceObject ('test ' , 1 );
62+ $ resource ->addAttribute ('foo ' , 'bar ' );
63+
64+ $ array = $ resource ->toArray ();
65+
66+ parent ::assertSame ('test ' , $ array ['type ' ]);
67+ parent ::assertSame ('1 ' , $ array ['id ' ]);
68+ parent ::assertArrayHasKey ('attributes ' , $ array );
69+
70+ $ resourceIdentifierObject = ResourceIdentifierObject::fromResourceObject ($ resource );
71+
72+ $ array = $ resourceIdentifierObject ->toArray ();
73+
74+ parent ::assertSame ('test ' , $ array ['type ' ]);
75+ parent ::assertSame ('1 ' , $ array ['id ' ]);
76+ parent ::assertArrayNotHasKey ('attributes ' , $ array );
77+ }
78+
79+ public function testFromResourceObject_NoFullIdentification (): void {
80+ $ resource = new ResourceObject ();
81+ $ array = $ resource ->toArray ();
82+
83+ $ this ->expectException (InputException::class);
84+ $ this ->expectExceptionMessage ('resource has no identification yet ' );
85+
86+ ResourceIdentifierObject::fromResourceObject ($ resource );
87+ }
88+
5889 public function testEquals_HappyPath (): void {
5990 $ one = new ResourceIdentifierObject ('test ' , 1 );
6091 $ two = new ResourceIdentifierObject ('test ' , 2 );
@@ -168,6 +199,13 @@ public function testGetIdentificationKey_NoFullIdentification(): void {
168199 $ resourceIdentifierObject ->getIdentificationKey ();
169200 }
170201
202+ public function testIsEmpty_IdWithoutType (): void {
203+ $ resourceIdentifierObject = new ResourceIdentifierObject ();
204+ $ resourceIdentifierObject ->setId (42 );
205+
206+ parent ::assertFalse ($ resourceIdentifierObject ->isEmpty ());
207+ }
208+
171209 public function testIsEmpty_WithAtMembers (): void {
172210 $ resourceIdentifierObject = new ResourceIdentifierObject ();
173211
@@ -190,4 +228,14 @@ public function testIsEmpty_WithExtensionMembers(): void {
190228
191229 parent ::assertFalse ($ resourceIdentifierObject ->isEmpty ());
192230 }
231+
232+ public function testPrimaryId_NoFullIdentification (): void {
233+ $ resourceIdentifierObject = new ResourceIdentifierObject ();
234+ $ primaryIdMethod = new \ReflectionMethod ($ resourceIdentifierObject , 'primaryId ' );
235+
236+ $ this ->expectException (Exception::class);
237+ $ this ->expectExceptionMessage ('resource has no identification yet ' );
238+
239+ $ primaryIdMethod ->invoke ($ resourceIdentifierObject );
240+ }
193241}
0 commit comments