Skip to content

Commit 7d4b851

Browse files
committed
improve test coverage
1 parent 0a998dc commit 7d4b851

File tree

6 files changed

+112
-15
lines changed

6 files changed

+112
-15
lines changed

src/Document.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ public function setDescribedByLink(string $href, array $meta=[]): void {
140140
}
141141

142142
/**
143-
* @throws InputException if the $level is unknown
144143
* @throws InputException if the $level is DocumentLevelEnum::Resource
145144
*/
146145
public function addMeta(string $key, mixed $value, DocumentLevelEnum $level=DocumentLevelEnum::Root): void {
@@ -163,9 +162,6 @@ public function addMeta(string $key, mixed $value, DocumentLevelEnum $level=Docu
163162

164163
case DocumentLevelEnum::Resource:
165164
throw new InputException('level "resource" can only be set on a ResourceDocument');
166-
167-
default:
168-
throw new InputException('unknown level "'.$level->value.'"');
169165
}
170166
}
171167

src/objects/RelationshipObject.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public function __construct(
4545
* @param CollectionDocument|ResourceInterface|ResourceInterface[]|null $relation
4646
* @param array<string, ?string> $links
4747
* @param array<string, mixed> $meta
48-
*
49-
* @throws InputException if $relation is not one of the supported formats
5048
*/
5149
public static function fromAnything(
5250
array|CollectionDocument|ResourceInterface|null $relation,
@@ -66,9 +64,6 @@ public static function fromAnything(
6664
elseif ($relation === null) {
6765
$relationshipObject = new RelationshipObject(RelationshipTypeEnum::ToOne);
6866
}
69-
else {
70-
throw new InputException('unknown format of relation "'.get_debug_type($relation).'"');
71-
}
7267

7368
return $relationshipObject;
7469
}

src/objects/ResourceIdentifierObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function setMetaObject(MetaObject $metaObject): void {
112112
*/
113113
public static function fromResourceObject(ResourceObject $resourceObject): static {
114114
if ($resourceObject->hasIdentification() === false) {
115-
throw new InputException('resource has no identification yet<');
115+
throw new InputException('resource has no identification yet');
116116
}
117117

118118
$resourceIdentifierObject = new static($resourceObject->type, $resourceObject->primaryId());

tests/ResourceDocumentTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use alsvanzelf\jsonapi\ResourceDocument;
88
use alsvanzelf\jsonapi\enums\DocumentLevelEnum;
9+
use alsvanzelf\jsonapi\enums\RelationshipTypeEnum;
910
use alsvanzelf\jsonapi\exceptions\Exception;
1011
use alsvanzelf\jsonapi\exceptions\InputException;
1112
use alsvanzelf\jsonapi\interfaces\ExtensionInterface;
@@ -100,6 +101,66 @@ public function testAddRelationship_DoNotIncludeContainedResources(): void {
100101
parent::assertArrayNotHasKey('included', $array);
101102
}
102103

104+
public function testAddRelationship_IdentifierOnlyObject(): void {
105+
$document = new ResourceDocument();
106+
$document->setPrimaryResource(new ResourceIdentifierObject('user', 42));
107+
108+
$this->expectException(Exception::class);
109+
$this->expectExceptionMessage('the resource is an identifier-only object');
110+
111+
$document->addRelationship('foo', null);
112+
}
113+
114+
public function testAddLink_IdentifierOnlyObject(): void {
115+
$document = new ResourceDocument();
116+
$document->setPrimaryResource(new ResourceIdentifierObject('user', 42));
117+
118+
$this->expectException(Exception::class);
119+
$this->expectExceptionMessage('the resource is an identifier-only object');
120+
121+
$document->addLink('foo', null);
122+
}
123+
124+
public function testSetSelfLink_IdentifierOnlyObject(): void {
125+
$document = new ResourceDocument();
126+
$document->setPrimaryResource(new ResourceIdentifierObject('user', 42));
127+
128+
$this->expectException(Exception::class);
129+
$this->expectExceptionMessage('the resource is an identifier-only object');
130+
131+
$document->setSelfLink('https://jsonapi.org');
132+
}
133+
134+
public function testSetAttributesObject_IdentifierOnlyObject(): void {
135+
$document = new ResourceDocument();
136+
$document->setPrimaryResource(new ResourceIdentifierObject('user', 42));
137+
138+
$this->expectException(Exception::class);
139+
$this->expectExceptionMessage('the resource is an identifier-only object');
140+
141+
$document->setAttributesObject(new AttributesObject());
142+
}
143+
144+
public function testAddRelationshipObject_IdentifierOnlyObject(): void {
145+
$document = new ResourceDocument();
146+
$document->setPrimaryResource(new ResourceIdentifierObject('user', 42));
147+
148+
$this->expectException(Exception::class);
149+
$this->expectExceptionMessage('the resource is an identifier-only object');
150+
151+
$document->addRelationshipObject('foo', new RelationshipObject(RelationshipTypeEnum::ToOne));
152+
}
153+
154+
public function testSetRelationshipsObject_IdentifierOnlyObject(): void {
155+
$document = new ResourceDocument();
156+
$document->setPrimaryResource(new ResourceIdentifierObject('user', 42));
157+
158+
$this->expectException(Exception::class);
159+
$this->expectExceptionMessage('the resource is an identifier-only object');
160+
161+
$document->setRelationshipsObject(new RelationshipsObject());
162+
}
163+
103164
public function testAddMeta_HappyPath(): void {
104165
$document = new ResourceDocument();
105166
$document->addMeta('foo', 'root', DocumentLevelEnum::Root);

tests/helpers/RequestParserTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,9 @@ public function testFromSuperglobals_WithPhpInputStream(): void {
8787
$_SERVER['REQUEST_URI'] = '/';
8888
$_SERVER['CONTENT_TYPE'] = ContentTypeEnum::Official->value;
8989

90+
// empty $_POST so we get a bit more test coverage for input stream processing
9091
$_GET = [];
91-
$_POST = [
92-
'meta' => [
93-
'foo' => 'bar',
94-
],
95-
];
92+
$_POST = [];
9693

9794
$requestParser = RequestParser::fromSuperglobals();
9895

tests/objects/ResourceIdentifierObjectTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
use alsvanzelf\jsonapi\exceptions\DuplicateException;
88
use alsvanzelf\jsonapi\exceptions\Exception;
9+
use alsvanzelf\jsonapi\exceptions\InputException;
910
use alsvanzelf\jsonapi\interfaces\ExtensionInterface;
1011
use alsvanzelf\jsonapi\objects\ResourceIdentifierObject;
12+
use alsvanzelf\jsonapi\objects\ResourceObject;
1113
use PHPUnit\Framework\TestCase;
1214

1315
class 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

Comments
 (0)