8000 [Tests] Add tests for duplicate belongs-to-many inserts · positivegroup/laravel-json-api@2e16c44 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 2e16c44

Browse files
committed
[Tests] Add tests for duplicate belongs-to-many inserts
See cloudcreativity#580
1 parent 1f0698b commit 2e16c44

File tree

5 files changed

+91
-6
lines changed

5 files changed

+91
-6
lines changed

tests/dummy/app/Role.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ class Role extends Model
3535
*/
3636
public function users(): BelongsToMany
3737
{
38-
return $this->belongsToMany(User::class);
38+
return $this->belongsToMany(User::class)->using(RoleUser::class);
3939
}
4040
}

tests/dummy/app/RoleUser.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Relations\Pivot;
23+
24+
class RoleUser extends Pivot
25+
{
26+
27+
/**
28+
* Indicates if the IDs are auto-incrementing.
29+
*
30+
* @var bool
31+
*/
32+
public $incrementing = true;
33+
}

tests/dummy/app/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function phone()
9696
*/
9797
public function roles(): BelongsToMany
9898
{
99-
return $this->belongsToMany(Role::class);
99+
return $this->belongsToMany(Role::class)->using(RoleUser::class);
100100
}
101101

102102
/**

tests/dummy/database/migrations/2018_02_11_1648_create_tables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public function up()
4747
});
4848

4949
Schema::create('role_user', function (Blueprint $table) {
50+
$table->increments('id');
5051
$table->unsignedInteger('user_id');
5152
$table->unsignedInteger('role_id');
52-
$table->primary(['user_id', 'role_id']);
5353
});
5454

5555
Schema::create('avatars', function (Blueprint $table) {

tests/lib/Integration/Eloquent/BelongsToManyTest.php

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,49 @@ public function testUpdateChangesRelatedResources(): void
279279
}
280280
}
281281

282+
/**
283+
* In this test we keep one existing role, and add two new ones.
284+
*/
285+
public function testUpdateSyncsRelatedResources(): void
286+
{
287+
/** @var User $user */
288+
$user = factory(User::class)->create();
289+
$user->roles()->saveMany($existing = factory(Role::class, 2)->create());
290+
291+
$roles = factory(Role::class, 2)->create();
292+
293+
$expected = $roles->merge([$existing[0]]);
294+
295+
$data = [
296+
'type' => 'users',
297+
'id' => (string) $user->getRouteKey(),
298+
'relationships' => [
299+
'roles' => [
300+
'data' => $expected->map(function (Role $role) {
301+
return ['type' => 'roles', 'id' => (string) $role->getRouteKey()];
302+
})->sortBy('id')->values()->all(),
303+
],
304+
],
305+
];
306+
307+
$response = $this
308+
->jsonApi()
309+
->includePaths('roles')
310+
->withData($data)
311+
->patch(url('/api/v1/users', $user));
312+
313+
$response->assertFetchedOne($data);
314+
315+
$this->assertDatabaseCount('role_user', count($expected));
316+
317+
foreach ($expected as $role) {
318+
$this->assertDatabaseHas('role_user', [
319+
'user_id' => $user->getKey(),
320+
'role_id' => $role->getKey(),
321+
]);
322+
}
323+
}
324+
282325
public function testReadRelated(): void
283326
{
284327
/** @var User $user */
@@ -420,9 +463,12 @@ public function testReplaceRelationshipWithDifferentResources(): void
420463
$user->roles()->saveMany(factory(Role::class, 2)->create());
421464
$roles = factory(Role::class, 3)->create();
422465

423-
$data = $roles->map(function (Role $user) {
424-
return ['type' => 'roles', 'id' => (string) $user->getRouteKey()];
425-
})->all();
466+
$data = $roles->map(function (Role $role) {
467+
return ['type' => 'roles', 'id' => (string) $role->getRouteKey()];
468+
});
469+
470+
/** Add a duplicate - expecting that resource to only be added once. */
471+
$data->push(['type' => 'roles', 'id' => (string) $roles[1]->getRouteKey()]);
426472

427473
$response = $this
428474
->jsonApi()
@@ -452,6 +498,12 @@ public function testAddToRelationship(): void
452498
return ['type' => 'roles', 'id' => (string) $role->getRouteKey()];
453499
});
454500

501+
/** Add an existing role: this should not be added twice */
502+
$data->push(['type' => 'roles', 'id' => (string) $existing[1]->getRouteKey()]);
503+
504+
/** Add a duplicate to add: this should only be added once. */
505+
$data->push(['type' => 'roles', 'id' => (string) $add[0]->getRouteKey()]);
506+
455507
$response = $this
456508
->jsonApi()
457509
->expects('roles')

0 commit comments

Comments
 (0)
0