8000 Merge tag 'v0.11.4' into develop · CodingSeo/laravel-json-api@253ef22 · GitHub
[go: up one dir, main page]

Skip to content

Commit 253ef22

Browse files
committed
Merge tag 'v0.11.4' into develop
Fix generator commands in Laravel 5.5 and ensure Eloquent adapter uses default pagination.
2 parents f0f4668 + 2d6167f commit 253ef22

File tree

6 files changed

+144
-4
lines changed

6 files changed

+144
-4
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
All notable changes to this project will be documented in this file. This project adheres to
33
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
44

5+
## [0.11.4] - 2018-01-25
6+
7+
### Fixed
8+
- [#138](https://github.com/cloudcreativity/laravel-json-api/issues/138)
9+
Generator commands not working since Laravel v5.5.28.
10+
- [#131](https://github.com/cloudcreativity/laravel-json-api/issues/131)
11+
Ensure Eloquent adapter uses default pagination parameters.
12+
513
## [0.11.3] - 2017-12-01
614

715
### Fixed

src/Console/Commands/AbstractGeneratorCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function __construct(Filesystem $files, Repository $apiRepository)
8989
}
9090

9191
/**
92-
* @return bool|null
92+
* @return int
9393
*/
9494
public function handle()
9595
{
@@ -99,8 +99,8 @@ public function handle()
9999
}
100100

101101
/** @todo remove when removing support for Laravel 5.4 */
102-
if (is_callable(['parent', 'fire'])) {
103-
return (parent::fire() !== false) ? 0 : 1;
102+
if (method_exists($this, 'fire')) {
103+
return ($this->fire() !== false) ? 0 : 1;
104104
}
105105

106106
return (parent::handle() !== false) ? 0 : 1;

src/Store/EloquentAdapter.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Illuminate\Support\Collection;
2929
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
3030
use Neomerx\JsonApi\Contracts\Encoder\Parameters\SortParameterInterface;
31+
use Neomerx\JsonApi\Encoder\Parameters\EncodingParameters;
3132

3233
/**
3334
* Class EloquentAdapter
@@ -154,7 +155,9 @@ public function query(EncodingParametersInterface $parameters)
154155
throw new RuntimeException('Paging parameters exist but paging is not supported.');
155156
}
156157

157-
return $pagination->isEmpty() ? $this->all($query) : $this->paginate($query, $parameters);
158+
return $pagination->isEmpty() ?
159+
$this->all($query) :
160+
$this->paginate($query, $this->normalizeParameters($parameters, $pagination));
158161
}
159162

160163
/**
@@ -388,4 +391,26 @@ protected function columnForField($field, Model $model)
388391
return $model::$snakeAttributes ? Str::underscore($field) : Str::camelize($field);
389392
}
390393

394+
/**
395+
* Normalize parameters for pagination.
396+
*
397+
* This is a temporary solution for Issue #131 in the v0.11.x series.
398+
*
399+
* @param EncodingParametersInterface $parameters
400+
* @param Collection $extractedPagination
401+
* @return EncodingParameters
402+
* @see https://github.com/cloudcreativity/laravel-json-api/issues/131
403+
*/
404+
private function normalizeParameters(EncodingParametersInterface $parameters, Collection $extractedPagination)
405+
{
406+
return new EncodingParameters(
407+
$parameters->getIncludePaths(),
408+
$parameters->getFieldSets(),
409+
$parameters->getSortParameters(),
410+
$extractedPagination->all(),
411+
$parameters->getFilteringParameters(),
412+
$parameters->getUnrecognizedParameters()
413+
);
414+
}
415+
391416
}

tests/Integration/PaginationTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ protected function setUp()
3939
$this->posts = factory(Post::class, 4)->create();
4040
}
4141

42+
/**
43+
* An adapter's default pagination is used if no pagination parameters are sent.
44+
*
45+
* @see https://github.com/cloudcreativity/laravel-json-api/issues/131
46+
*/
47+
public function testDefaultPagination()
48+
{
49+
$response = $this->doSearch();
50+
$response->assertSearchResponse()->assertContainsOnly(['posts' => $this->posts->modelKeys()]);
51+
52+
$response->assertJson([
53+
'meta' => [
54+
'page' => [
55+
'current-page' => 1,
56+
'per-page' => 10,
57+
'from' => 1,
58+
'to' => 4,
59+
'total' => 4,
60+
'last-page' => 1,
61+
],
62+
],
63+
]);
64+
}
65+
4266
public function testPage1()
4367
{
4468
$response = $this->doSearch(['page' => ['number' => 1, 'size' => 3]]);

tests/Integration/ValidationTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2018 Cloud Creativity Limited
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace CloudCreativity\LaravelJsonApi\Tests\Integration;
20+
21+
use CloudCreativity\LaravelJsonApi\Routing\ApiGroup;
22+
use CloudCreativity\LaravelJsonApi\Tests\Models\Comment;
23+
24+
class ValidationTest extends TestCase
25+
{
26+
27+
/**
28+
* @var string
29+
*/
30+
protected $resourceType;
31+
32+
/**
33+
* @return void
34+
*/
35+
protected function setUp()
36+
{
37+
parent::setUp();
38+
$this->withDefaultApi([], function (ApiGroup $api) {
39+
$api->resource('posts');
40+
$api->resource('comments');
41+
});
42+
}
43+
44+
/**
45+
* The client must receive a 400 error with a correct JSON API pointer if an invalid
46+
* resource type is sent for a resource relationship.
47+
*
48+
* @see https://github.com/cloudcreativity/laravel-json-api/issues/139
49+
*/
50+
public function testRejectsUnrecognisedTypeInResourceRelationship()
51+
{
52+
$this->resourceType = 'comments';
53+
$comment = factory(Comment::class)->make();
54+
55+
$data = [
56+
'type' => 'comments',
57+
'attributes' => [
58+
'content' => $comment->content,
59+
],
60+
'relationships' => [
61+
'post' => [
62+
'data' => [
63+
'type' => 'post', // invalid type as expecting the plural,
64+
'id' => (string) $comment->post_id,
65+
],
66+
],
67+
],
68+
];
69+
70+
$this->doCreate($data)
71+
->assertStatus(400)
72+
->assertErrors()
73+
->assertPointers('/data/relationships/post/data/type');
74+
}
75+
}

tests/JsonApi/Posts/Adapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
class Adapter extends EloquentAdapter
1212
{
1313

14+
/**
15+
* @var array
16+
*/
17+
protected 5CE0 $defaultPagination = [
18+
'number' => 1,
19+
'size' => 10,
20+
];
21+
1422
/**
1523
* Adapter constructor.
1624
*

0 commit comments

Comments
 (0)
0