10000 [Tests] Add belongs-to-many tests · isabella232/laravel-json-api@1f0698b · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f0698b

Browse files
committed
[Tests] Add belongs-to-many tests
1 parent a46e9cd commit 1f0698b

File tree

14 files changed

+798
-8
lines changed

14 files changed

+798
-8
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright 2020 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace DummyApp\JsonApi\Roles;
19+
20+
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
21+
use CloudCreativity\LaravelJsonApi\Eloquent\HasMany;
22+
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
23+
use DummyApp\Role;
24+
use Illuminate\Support\Collection;
25+
26+
class Adapter extends AbstractAdapter
27+
{
28+
29+
/**
30+
* Adapter constructor.
31+
*
32+
* @param StandardStrategy $paging
33+
*/
34+
public function __construct(StandardStrategy $paging)
35+
{
36+
parent::__construct(new Role(), $paging);
37+
}
38+
39+
/**
40+
* @return HasMany
41+
*/
42+
protected function users(): HasMany
43+
{
44+
return $this->hasMany();
45+
}
46+
47+
/**
48+
* @inheritDoc
49+
*/
50+
protected function filter($query, Collection $filters)
51+
{
52+
if ($name = $filters->get('name')) {
53+
$query->where('name', 'like', "{$name}%");
54+
}
55+
}
56+
57+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright 2020 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace DummyApp\JsonApi\Roles;
19+
20+
use DummyApp\Role;
21+
use Neomerx\JsonApi\Schema\SchemaProvider;
22+
23+
class Schema extends SchemaProvider
24+
{
25+
26+
/**
27+
* @var string
28+
*/
29+
protected $resourceType = 'roles';
30+
31+
/**
32+
* @param Role $resource
33+
* @return string
34+
*/
35+
public function getId($resource)
36+
{
37+
return (string) $resource->getRouteKey();
38+
}
39+
40+
/**
41+
* @param Role $resource
42+
* @return array
43+
*/
44+
public function getAttributes($resource)
45+
{
46+
return [
47+
'createdAt' => $resource->created_at,
48+
'name' => $resource->name,
49+
'updatedAt' => $resource->updated_at,
50+
];
51+
}
52+
53+
/**
54+
* @param Role $resource
55+
* @param bool $isPrimary
56+
* @param array $includeRelationships
57+
* @return array
58+
*/
59+
public function getRelationships($resource, $isPrimary, array $includeRelationships)
60+
{
61+
return [
62+
'users' => [
63+
self::SHOW_SELF => true,
64+
self::SHOW_RELATED => true,
65+
self::SHOW_DATA => false,
66+
],
67+
];
68+
}
69+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright 2020 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace DummyApp\JsonApi\Roles;
19+
20+
use CloudCreativity\LaravelJsonApi\Contracts\Validation\ValidatorInterface;
21+
use CloudCreativity\LaravelJsonApi\Validation\AbstractValidators;
22+
23+
class Validators extends AbstractValidators
24+
{
25+
26+
/**
27+
* @var array
28+
*/
29+
protected $allowedSortParameters = [
30+
'createdAt',
31+
'updatedAt',
32+
'name',
33+
];
34+
35+
/**
36+
* @var array
37+
*/
38+
protected $allowedIncludePaths = [];
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
protected function rules($record, array $data): array
44+
{
45+
// @TODO
46+
return [];
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*/
52+
protected function queryRules(): array
53+
{
54+
return [
55+
'filter.name' => 'filled|string',
56+
'page.number' => 'filled|integer|min:1',
57+
'page.size' => 'filled|integer|between:1,50',
58+
];
59+
}
60+
}

tests/dummy/app/JsonApi/Users/Adapter.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace DummyApp\JsonApi\Users;
1919

2020
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
21+
use CloudCreativity\LaravelJsonApi\Eloquent\HasMany;
2122
use CloudCreativity\LaravelJsonApi\Eloquent\HasOne;
2223
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
2324
use DummyApp\User;
@@ -42,21 +43,29 @@ public function __construct(StandardStrategy $paging)
4243
}
4344

4445
/**
45-
* @inheritDoc
46+
* @return HasOne
4647
*/
47-
protected function filter($query, Collection $filters)
48+
protected function phone(): HasOne
4849
{
49-
if ($name = $filters->get('name')) {
50-
$query->where('users.name', 'like', "%{$name}%");
51-
}
50+
return $this->hasOne();
5251
}
5352

5453
/**
55-
* @return HasOne
54+
* @return HasMany
5655
*/
57-
protected function phone()
56+
protected function roles(): HasMany
5857
{
59-
return $this->hasOne();
58+
return $this->hasMany();
59+
}
60+
61+
/**
62+
* @inheritDoc
63+
*/
64+
protected function filter($query, Collection $filters)
65+
{
66+
if ($name = $filters->get('name')) {
67+
$query->where('users.name', 'like', "%{$name}%");
68+
}
6069
}
6170

6271
/**

tests/dummy/app/JsonApi/Users/Schema.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ public function getRelationships($resource, $isPrimary, array $includeRelationsh
6868
return $resource->phone;
6969
},
7070
],
71+
'roles' => [
72+
self::SHOW_SELF => true,
73+
self::SHOW_RELATED => true,
74+
self::SHOW_DATA => isset($includeRelationships['roles']),
75+
self::DATA => function () use ($resource) {
76+
return $resource->roles;
77+
},
78+
],
7179
];
7280
}
7381
}

tests/dummy/app/JsonApi/Users/Validators.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Validators extends AbstractValidators
3838
*/
3939
protected $allowedIncludePaths = [
4040
'phone',
41+
'roles',
4142
];
4243

4344
/**

tests/dummy/app/Role.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/*
3+
* Copyright 2021 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace DummyApp;
21+
22+
use Illuminate\Database\Eloquent\Model;
23+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
24+
25+
class Role extends Model
26+
{
27+
28+
/**
29+
* @var string[]
30+
*/
31+
protected $fillable = ['name'];
32+
33+
/**
34+
* @return BelongsToMany
35+
*/
36+
public function users(): BelongsToMany
37+
{
38+
return $this->belongsToMany(User::class);
39+
}
40+
}

tests/dummy/app/User.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace DummyApp;
1919

2020
use Illuminate\Database\Eloquent\Relations\BelongsTo;
21+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2122
use Illuminate\Database\Eloquent\Relations\HasOne;
2223
use Illuminate\Database\Eloquent\Relations\MorphOne;
2324
use Illuminate\Foundation\Auth\User as Authenticatable;
@@ -90,6 +91,14 @@ public function phone()
9091
return $this->hasOne(Phone::class);
9192
}
9293

94+
/**
95+
* @return BelongsToMany
96+
*/
97+
public function roles(): BelongsToMany
98+
{
99+
return $this->belongsToMany(Role::class);
100+
}
101+
93102
/**
94103
* @return BelongsTo
95104
*/

tests/dummy/config/json-api-v1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
'images' => \DummyApp\Image::class,
7575
'phones' => \DummyApp\Phone::class,
7676
'posts' => \DummyApp\Post::class,
77+
'roles' => \DummyApp\Role::class,
7778
'sites' => \DummyApp\Entities\Site::class,
7879
'suppliers' => \DummyApp\Supplier::class,
7980
'tags' => \DummyApp\Tag::class,

tests/dummy/database/factories/ModelFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@
116116
];
117117
});
118118

119+
/** Role */
120+
$factory->define(DummyApp\Role::class, function (Faker $faker) {
121+
return ['name' => $faker->colorName];
122+
});
123+
119124
/** Tag */
120125
$factory->define(DummyApp\Tag::class, function (Faker $faker) {
121126
return [

0 commit comments

Comments
 (0)
0