Skip to content

Commit 45c60f4

Browse files
committed
[Tests] Add failing update to-one tests
1 parent 3248c0d commit 45c60f4

File tree

14 files changed

+450
-13
lines changed

14 files changed

+450
-13
lines changed

Diff for: tests/dummy/app/Http/Controllers/Api/V1/UserController.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626

2727
class UserController extends Controller
2828
{
29-
3029
use Actions\FetchOne;
30+
use Actions\FetchRelated;
31+
use Actions\FetchRelationship;
32+
use Actions\UpdateRelationship;
3133

3234
/**
3335
* Return the current user.

Diff for: tests/dummy/app/JsonApi/V1/Phones/PhoneSchema.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/*
3+
* Copyright 2022 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 App\JsonApi\V1\Phones;
21+
22+
use App\Models\Phone;
23+
use LaravelJsonApi\Eloquent\Fields\DateTime;
24+
use LaravelJsonApi\Eloquent\Fields\ID;
25+
use LaravelJsonApi\Eloquent\Fields\Str;
26+
use LaravelJsonApi\Eloquent\Schema;
27+
28+
class PhoneSchema extends Schema
29+
{
30+
/**
31+
* The model the schema corresponds to.
32+
*
33+
* @var string
34+
*/
35+
public static string $model = Phone::class;
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function fields(): iterable
41+
{
42+
return [
43+
ID::make(),
44+
DateTime::make('createdAt')->readOnly(),
45+
Str::make('number'),
46+
DateTime::make('updatedAt')->readOnly(),
47+
];
48+
}
49+
}

Diff for: tests/dummy/app/JsonApi/V1/Server.php

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ protected function allSchemas(): array
6565
return [
6666
Comments\CommentSchema::class,
6767
Images\ImageSchema::class,
68+
Phones\PhoneSchema::class,
6869
Posts\PostSchema::class,
6970
Tags\TagSchema::class,
7071
Users\UserSchema::class,

Diff for: tests/dummy/app/JsonApi/V1/Users/UserRequest.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/*
3+
* Copyright 2022 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 App\JsonApi\V1\Users;
21+
22+
use LaravelJsonApi\Laravel\Http\Requests\ResourceRequest;
23+
use LaravelJsonApi\Validation\Rule as JsonApiRule;
24+
25+
class UserRequest extends ResourceRequest
26+
{
27+
/**
28+
* @return array
29+
*/
30+
public function rules(): array
31+
{
32+
return [
33+
'phone' => JsonApiRule::toOne(),
34+
];
35+
}
36+
}

Diff for: tests/dummy/app/JsonApi/V1/Users/UserSchema.php

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use App\Models\User;
2323
use LaravelJsonApi\Eloquent\Fields\DateTime;
2424
use LaravelJsonApi\Eloquent\Fields\ID;
25+
use LaravelJsonApi\Eloquent\Fields\Relations\HasOne;
2526
use LaravelJsonApi\Eloquent\Fields\Str;
2627
use LaravelJsonApi\Eloquent\Filters\Where;
2728
use LaravelJsonApi\Eloquent\Filters\WhereIdIn;
@@ -47,6 +48,7 @@ public function fields(): array
4748
ID::make(),
4849
DateTime::make('createdAt')->readOnly(),
4950
Str::make('name'),
51+
HasOne::make('phone')->deleteDetachedModel(),
5052
DateTime::make('updatedAt')->readOnly(),
5153
];
5254
}

Diff for: tests/dummy/app/Models/Phone.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/*
3+
* Copyright 2022 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 App\Models;
21+
22+
use Illuminate\Database\Eloquent\Factories\HasFactory;
23+
use Illuminate\Database\Eloquent\Model;
24+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
25+
26+
class Phone extends Model
27+
{
28+
use HasFactory;
29+
30+
/**
31+
* @var string[]
32+
*/
33+
protected $fillable = [
34+
'number',
35+
];
36+
37+
/**
38+
* @return BelongsTo
39+
*/
40+
public function user(): BelongsTo
41+
{
42+
return $this->belongsTo(User::class);
43+
}
44+
}

Diff for: tests/dummy/app/Models/User.php

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace App\Models;
1919

2020
use Illuminate\Database\Eloquent\Factories\HasFactory;
21+
use Illuminate\Database\Eloquent\Relations\HasOne;
2122
use Illuminate\Foundation\Auth\User as Authenticatable;
2223
use Illuminate\Notifications\Notifiable;
2324

@@ -60,4 +61,12 @@ public function isAdmin(): bool
6061
{
6162
return '[email protected]' === $this->email;
6263
}
64+
65+
/**
66+
* @return HasOne
67+
*/
68+
public function phone(): HasOne
69+
{
70+
return $this->hasOne(Phone::class);
71+
}
6372
}

Diff for: tests/dummy/app/Policies/UserPolicy.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
class UserPolicy
2525
{
26-
2726
/**
2827
* Determine if the user can view the other user.
2928
*
@@ -35,4 +34,28 @@ public function view(User $user, User $other): bool
3534
{
3635
return true;
3736
}
37+
38+
/**
39+
* Determine if the user can view the other user's phone.
40+
*
41+
* @param User $user
42+
* @param User $other
43+
* @return bool
44+
*/
45+
public function viewPhone(User $user, User $other): bool
46+
{
47+
return $user->is($other);
48+
}
49+
50+
/**
51+
* Determine if the user can update the other user's phone.
52+
*
53+
* @param User $user
54+
* @param User $other
55+
* @return bool
56+
*/
57+
public function updatePhone(User $user, User $other): bool
58+
{
59+
return $user->is($other);
60+
}
3861
}

Diff for: tests/dummy/database/factories/PhoneFactory.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/*
3+
* Copyright 2022 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 Database\Factories;
21+
22+
use App\Models\Phone;
23+
use App\Models\User;
24+
use Illuminate\Database\Eloquent\Factories\Factory;
25+
26+
class PhoneFactory extends Factory
27+
{
28+
/**
29+
* The name of the factory's corresponding model.
30+
*
31+
* @var string
32+
*/
33+
protected $model = Phone::class;
34+
35+
/**
36+
* Define the model's default state.
37+
*
38+
* @return array
39+
*/
40+
public function definition(): array
41+
{
42+
return [
43+
'number' => $this->faker->phoneNumber(),
44+
'user_id' => User::factory(),
45+
];
46+
}
47+
}

Diff for: tests/dummy/database/migrations/2020_06_13_143800_create_post_and_video_tables.php

+12-10
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
* limitations under the License.
1616
*/
1717

18+
declare(strict_types=1);
19+
1820
use Illuminate\Database\Migrations\Migration;
1921
use Illuminate\Database\Schema\Blueprint;
2022
use Illuminate\Support\Facades\Schema;
2123

22-
class CreatePostAndVideoTables extends Migration
24+
return new class extends Migration
2325
{
2426

2527
/**
@@ -29,7 +31,7 @@ class CreatePostAndVideoTables extends Migration
2931
*/
3032
public function up(): void
3133
{
32-
Schema::create('posts', function (Blueprint $table) {
34+
Schema::create('posts', function (Blueprint $table): void {
3335
$table->id();
3436
$table->timestamps();
3537
$table->softDeletes();
@@ -47,13 +49,13 @@ public function up(): void
4749
->onUpdate('cascade');
4850
});
4951

50-
Schema::create('images', function (Blueprint $table) {
52+
Schema::create('images', function (Blueprint $table): void {
5153
$table->uuid('uuid')->primary();
5254
$table->timestamps();
5355
$table->string('url');
5456
});
5557

56-
Schema::create('videos', function (Blueprint $table) {
58+
Schema::create('videos', function (Blueprint $table): void {
5759
$table->uuid('uuid')->primary();
5860
$table->timestamps();
5961
$table->unsignedBigInteger('owner_id');
@@ -67,7 +69,7 @@ public function up(): void
6769
->onUpdate('cascade');
6870
});
6971

70-
Schema::create('comments', function (Blueprint $table) {
72+
Schema::create('comments', function (Blueprint $table): void {
7173
$table->id();
7274
$table->timestamps();
7375
$table->unsignedBigInteger('post_id');
@@ -87,13 +89,13 @@ public function up(): void
8789
->cascadeOnUpdate();
8890
});
8991

90-
Schema::create('tags', function (Blueprint $table) {
92+
Schema::create('tags', function (Blueprint $table): void {
9193
$table->id();
9294
$table->timestamps();
9395
$table->string('name');
9496
});
9597

96-
Schema::create('taggables', function (Blueprint $table) {
98+
Schema::create('taggables', function (Blueprint $table): void {
9799
$table->unsignedBigInteger('tag_id');
98100
$table->morphs('taggable');
99101
$table->primary([
@@ -109,7 +111,7 @@ public function up(): void
109111
->cascadeOnDelete();
110112
});
111113

112-
Schema::create('image_post', function (Blueprint $table) {
114+
Schema::create('image_post', function (Blueprint $table): void {
113115
$table->uuid('image_uuid');
114116
$table->unsignedBigInteger('post_id');
115117
$table->primary(['image_uuid', 'post_id']);
@@ -127,7 +129,7 @@ public function up(): void
127129
->cascadeOnUpdate();
128130
});
129131

130-
Schema::create('post_video', function (Blueprint $table) {
132+
Schema::create('post_video', function (Blueprint $table): void {
131133
$table->unsignedBigInteger('post_id');
132134
$table->uuid('video_uuid');
133135
$table->primary(['post_id', 'video_uuid']);
@@ -159,4 +161,4 @@ public function down(): void
159161
Schema::dropIfExists('videos');
160162
Schema::dropIfExists('posts');
161163
}
162-
}
164+
};

0 commit comments

Comments
 (0)