Skip to content

Commit b8e2b5a

Browse files
committed
Merge remote-tracking branch 'origin/develop' into request-validation
2 parents 341f874 + 272bd2c commit b8e2b5a

File tree

7 files changed

+79
-82
lines changed

7 files changed

+79
-82
lines changed

src/Exceptions/HttpException.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
namespace Huntie\JsonApi\Exceptions;
44

55
use Huntie\JsonApi\Support\JsonApiErrors;
6+
use Illuminate\Foundation\Application;
67
use Illuminate\Http\Response;
7-
use Illuminate\Http\Exception\HttpResponseException;
8+
9+
class_alias(
10+
version_compare(Application::VERSION, '5.4.0', '>=') ?
11+
'Illuminate\Http\Exceptions\HttpResponseException' :
12+
'Illuminate\Http\Exception\HttpResponseException',
13+
__NAMESPACE__ . '\HttpResponseException'
14+
);
815

916
class HttpException extends HttpResponseException
1017
{

src/Serializers/ResourceSerializer.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public function scopeIncludes($include)
6969
public function toResourceIdentifier()
7070
{
7171
return [
72-
'type' => $this->getRecordType(),
73-
'id' => $this->record->id,
72+
'type' => $this->getResourceType(),
73+
'id' => $this->getPrimaryKey(),
7474
];
7575
}
7676

@@ -137,17 +137,29 @@ protected function getIncludedData()
137137
}
138138

139139
/**
140-
* Return the primary record type name.
140+
* Return the primary resource type name.
141141
*
142142
* @return string
143143
*/
144-
protected function getRecordType()
144+
protected function getResourceType()
145145
{
146146
$modelName = collect(explode('\\', get_class($this->record)))->last();
147147

148148
return snake_case(str_plural($modelName), '-');
149149
}
150150

151+
/**
152+
* Return the primary key value for the resource.
153+
*
154+
* @return int|string
155+
*/
156+
protected function getPrimaryKey()
157+
{
158+
$value = $this->record->getKey();
159+
160+
return is_int($value) ? $value : (string) $value;
161+
}
162+
151163
/**
152164
* Return the attribute object data for the primary record.
153165
*
@@ -157,7 +169,7 @@ protected function transformRecordAttributes()
157169
{
158170
$attributes = array_diff_key($this->record->toArray(), $this->record->getRelations());
159171
$attributes = array_except($attributes, ['id']);
160-
$fields = array_get($this->fields, $this->getRecordType());
172+
$fields = array_get($this->fields, $this->getResourceType());
161173

162174
if (!empty($fields)) {
163175
$attributes = array_only($attributes, $fields);

tests/Serializers/CollectionSerializerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testToResourceCollection()
1717
{
1818
$serializer = new CollectionSerializer(factory(User::class, 5)->make());
1919

20-
$this->seeJsonApiObjectCollection($serializer->serializeToObject(), 5);
20+
$this->assertJsonApiObjectCollection($serializer->serializeToObject(), 5);
2121
}
2222

2323
/**
@@ -59,7 +59,7 @@ public function testIncludedRecords()
5959
$included = $serializer->getIncludedRecords();
6060

6161
$this->assertInstanceOf(Collection::class, $included);
62-
$this->seeJsonApiObjectCollection(['data' => $included->toArray()], 6);
62+
$this->assertJsonApiObjectCollection(['data' => $included->toArray()], 6);
6363

6464
foreach ($included as $record) {
6565
$this->assertEquals('posts', $record['type'], 'Unexpected record type included with collection');

tests/Serializers/JsonApiSerializerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testSerializeToObject()
1717
$document = $serializer->serializeToObject();
1818

1919
$this->assertInternalType('array', $document);
20-
$this->seeJsonApiResourceObject($document);
20+
$this->assertJsonApiResourceObject($document);
2121
}
2222

2323
/**

tests/Serializers/RelationshipSerializerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testHasOneToResourceLinkage()
2020
$serializer = new RelationshipSerializer($post, 'author');
2121
$relationship = $serializer->toResourceLinkage();
2222

23-
$this->seeJsonApiResourceIdentifier(['data' => $relationship]);
23+
$this->assertJsonApiResourceIdentifier(['data' => $relationship]);
2424
$this->assertEquals([
2525
'type' => 'users',
2626
'id' => $post->author->id,
@@ -38,7 +38,7 @@ public function testHasManyToResourceLinkage()
3838
$serializer = new RelationshipSerializer($user, 'posts');
3939
$relationship = $serializer->toResourceLinkage();
4040

41-
$this->seeJsonApiIdentifierCollection(['data' => $relationship->toArray()], 2);
41+
$this->assertJsonApiIdentifierCollection(['data' => $relationship->toArray()], 2);
4242

4343
foreach ($relationship as $identifier) {
4444
$this->assertEquals('posts', $identifier['type']);
@@ -56,7 +56,7 @@ public function testHasOneToResourceCollection()
5656
$serializer = new RelationshipSerializer($post, 'author');
5757
$relationship = $serializer->toResourceCollection();
5858

59-
$this->seeJsonApiResourceObject(['data' => $relationship]);
59+
$this->assertJsonApiResourceObject(['data' => $relationship]);
6060
$this->assertEquals('users', $relationship['type']);
6161
}
6262

@@ -71,7 +71,7 @@ public function testHasManyToResourceCollection()
7171
$serializer = new RelationshipSerializer($user, 'posts');
7272
$relationship = $serializer->toResourceCollection();
7373

74-
$this->seeJsonApiObjectCollection(['data' => $relationship->toArray()], 2);
74+
$this->assertJsonApiObjectCollection(['data' => $relationship->toArray()], 2);
7575

7676
foreach ($relationship as $identifier) {
7777
$this->assertEquals('posts', $identifier['type']);

tests/Serializers/ResourceSerializerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function testResourceRelationships()
7979

8080
$this->assertArrayHasKey('relationships', $resource);
8181
$this->assertArrayHasKey('author', $resource['relationships']);
82-
$this->seeJsonApiResourceIdentifier($resource['relationships']['author']);
82+
$this->assertJsonApiResourceIdentifier($resource['relationships']['author']);
8383
$this->assertEquals('users', $resource['relationships']['author']['data']['type']);
8484
$this->assertArrayHasKey('comments', $resource['relationships']);
8585
$this->assertCount(2, $resource['relationships']['comments']['data']);
@@ -97,7 +97,7 @@ public function testIncludedRecords()
9797
$included = $serializer->getIncludedRecords();
9898

9999
$this->assertInstanceOf(Collection::class, $included);
100-
$this->seeJsonApiObjectCollection(['data' => $included->toArray()], 4);
100+
$this->assertJsonApiObjectCollection(['data' => $included->toArray()], 4);
101101

102102
foreach ($included as $record) {
103103
$this->assertRegExp('/posts|comments/', $record['type'], 'Unexpected record type included with resource');
@@ -117,7 +117,7 @@ public function testScopeIncludedRecords()
117117
$included = $serializer->getIncludedRecords();
118118

119119
$this->assertInstanceOf(Collection::class, $included);
120-
$this->seeJsonApiObjectCollection(['data' => $included->toArray()], 2);
120+
$this->assertCount(2, $included->toArray());
121121

122122
foreach ($included as $record) {
123123
$this->assertEquals('posts', $record['type'], 'scopeIncludes() failed to return relationship subset');

tests/Support/JsonApiAssertions.php

Lines changed: 44 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,110 +3,88 @@
33
namespace Huntie\JsonApi\Tests\Support;
44

55
/**
6-
* Extend TestCase with additional JSON API assertions.
7-
*
8-
* @method seeJsonStructure
6+
* Extend TestCase with additional JSON API related assertions.
97
*/
108
trait JsonApiAssertions
119
{
1210
/**
13-
* Assert that a document containing a valid JSON API resource identifier
14-
* object is returned.
11+
* Assert that all given keys are set in an associative array. Nested
12+
* members may be specified using dot notation.
1513
*
16-
* @param array|null $responseData
14+
* @param array $keys
15+
* @param array $array
1716
*
18-
* @return $this
17+
* @throws PHPUnit_Framework_AssertionFailedError
1918
*/
20-
public function seeJsonApiResourceIdentifier($responseData = null)
19+
public function assertArrayHasAll(array $keys, array $array)
2120
{
22-
return $this->seeJsonStructure([
23-
'data' => [
24-
'type',
25-
'id',
26-
]
27-
], $responseData);
21+
foreach ($keys as $key) {
22+
if (!array_has($array, $key)) {
23+
$this->fail('Failed asserting that key "' . $key . '" exists in input array.');
24+
}
25+
}
2826
}
2927

3028
/**
31-
* Assert that a document containing a valid JSON API resource object is
32-
* returned.
29+
* Assert that an array contains a valid JSON API resource identifier.
3330
*
34-
* @param array|null $responseData
31+
* @param array $array
3532
*
36-
* @return $this
33+
* @throws PHPUnit_Framework_AssertionFailedError
3734
*/
38-
public function seeJsonApiResourceObject($responseData = null)
35+
public function assertJsonApiResourceIdentifier(array $array)
3936
{
40-
return $this->seeJsonStructure([
41-
'data' => [
42-
'type',
43-
'id',
44-
'attributes',
45-
]
46-
], $responseData);
37+
$this->assertArrayHasAll(['data.type', 'data.id'], $array);
4738
}
4839

4940
/**
50-
* Assert that a document containing a valid JSON API resource identifier
51-
* object collection is returned.
41+
* Assert that an array contains a valid JSON API resource object.
5242
*
53-
* @param array|null $responseData
54-
* @param int|null $count
43+
* @param array $array
5544
*
56-
* @return $this
45+
* @throws PHPUnit_Framework_AssertionFailedError
5746
*/
58-
public function seeJsonApiIdentifierCollection($responseData = null, $count = null)
47+
public function assertJsonApiResourceObject(array $array)
5948
{
60-
return $this->seeJsonApiCollection([
61-
'type',
62-
'id',
63-
], $responseData, $count);
49+
$this->assertArrayHasAll(['data.type', 'data.id', 'data.attributes'], $array);
6450
}
6551

6652
/**
67-
* Assert that a document containing a valid JSON API resource object
68-
* collection is returned.
53+
* Assert that an array contains a valid JSON API resource identifier
54+
* object collection.
6955
*
70-
* @param array|null $responseData
71-
* @param int|null $count
56+
* @param array $array
57+
* @param int $count
7258
*
73-
* @return $this
59+
* @throws PHPUnit_Framework_AssertionFailedError
7460
*/
75-
public function seeJsonApiObjectCollection($responseData = null, $count = null)
61+
public function assertJsonApiIdentifierCollection(array $array, $count = null)
7662
{
77-
return $this->seeJsonApiCollection([
78-
'type',
79-
'id',
80-
'attributes',
81-
], $responseData, $count);
63+
$this->assertArrayHasKey('data', $array, 'No data key for collection');
64+
65+
foreach ($array['data'] as $identifier) {
66+
$this->assertArrayHasAll(['type', 'id'], (array) $identifier);
67+
}
68+
69+
$this->assertCount($count, $array['data'], 'Incorrect object count returned in collection');
8270
}
8371

8472
/**
85-
* Assert that a JSON API document contains a collection of objects defined
86-
* by a given pattern.
73+
* Assert that an array contains a valid JSON API resource object collection.
8774
*
88-
* @param array $pattern
89-
* @param array|null $responseData
90-
* @param int|null $count
75+
* @param array $array
76+
* @param int $count
9177
*
92-
* @return $this
78+
* @throws PHPUnit_Framework_AssertionFailedError
9379
*/
94-
private function seeJsonApiCollection($pattern, $responseData = null, $count = null)
80+
public function assertJsonApiObjectCollection(array $array, $count = null)
9581
{
96-
if (!$responseData) {
97-
$responseData = json_decode($this->response->getContent(), true);
98-
}
99-
100-
$this->seeJsonStructure([
101-
'data' => [
102-
'*' => $pattern,
103-
]
104-
], $responseData);
82+
$this->assertArrayHasKey('data', $array, 'No data key for collection');
10583

106-
if ($count) {
107-
$this->assertCount($count, $responseData['data'], 'Incorrect object count returned in collection');
84+
foreach ($array['data'] as $object) {
85+
$this->assertArrayHasAll(['type', 'id', 'attributes'], (array) $object);
10886
}
10987

110-
return $this;
88+
$this->assertCount($count, $array['data'], 'Incorrect object count returned in collection');
11189
}
11290
}

0 commit comments

Comments
 (0)