Skip to content

Commit f88ffd1

Browse files
authored
[9.x] Improves Pivot tests (#44626)
* [9.x] Improves accessing `$model->pivot->getKey()` doesn't throw an error on "strict mode" when pivot doesn't have an ID. ``` The attribute [id] either does not exist or was not retrieved for model [Illuminate\Database\Eloquent\Relations\Pivot]. {"userId":4,"exception":"[object] (Illuminate\\Database\\Eloquent\\MissingAttributeException(code: 0): The attribute [id] either does not exist or was not retrieved for model [Illuminate\\Database\\Eloquent\\Relations\\Pivot]. at /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:470) ```` Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent 6319837 commit f88ffd1

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Database;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\Pivot;
7+
use Illuminate\Database\Schema\Blueprint;
8+
use Illuminate\Support\Facades\Schema;
9+
10+
class EloquentPivotTest extends DatabaseTestCase
11+
{
12+
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
13+
{
14+
Schema::create('users', function (Blueprint $table) {
15+
$table->increments('id');
16+
$table->string('email');
17+
$table->timestamps();
18+
});
19+
20+
Schema::create('projects', function (Blueprint $table) {
21+
$table->increments('id');
22+
$table->string('name');
23+
$table->timestamps();
24+
});
25+
26+
Schema::create('collaborators', function (Blueprint $table) {
27+
$table->integer('user_id');
28+
$table->integer('project_id');
29+
$table->text('permissions')->nullable();
30+
});
31+
32+
Schema::create('contributors', function (Blueprint $table) {
33+
$table->id();
34+
$table->integer('user_id');
35+
$table->integer('project_id');
36+
$table->text('permissions')->nullable();
37+
});
38+
}
39+
40+
public function testPivotConvenientHelperReturnExpectedResult()
41+
{
42+
$user = PivotTestUser::forceCreate(['email' => '[email protected]']);
43+
$user2 = PivotTestUser::forceCreate(['email' => '[email protected]']);
44+
$project = PivotTestProject::forceCreate(['name' => 'Test Project']);
45+
46+
$project->contributors()->attach($user);
47+
$project->collaborators()->attach($user2);
48+
49+
tap($project->contributors->first()->pivot, function ($pivot) use ($project, $user) {
50+
$this->assertEquals(1, $pivot->getKey());
51+
$this->assertEquals(1, $pivot->getQueueableId());
52+
$this->assertSame('user_id', $pivot->getRelatedKey());
53+
$this->assertSame('project_id', $pivot->getForeignKey());
54+
});
55+
56+
tap($project->collaborators->first()->pivot, function ($pivot) use ($project, $user2) {
57+
$this->assertNull($pivot->getKey());
58+
$this->assertSame('project_id:1:user_id:2', $pivot->getQueueableId());
59+
$this->assertSame('user_id', $pivot->getRelatedKey());
60+
$this->assertSame('project_id', $pivot->getForeignKey());
61+
});
62+
}
63+
}
64+
65+
class PivotTestUser extends Model
66+
{
67+
public $table = 'users';
68+
}
69+
70+
class PivotTestProject extends Model
71+
{
72+
public $table = 'projects';
73+
74+
public function collaborators()
75+
{
76+
return $this->belongsToMany(
77+
PivotTestUser::class, 'collaborators', 'project_id', 'user_id'
78+
)->withPivot('permissions')
79+
->using(PivotTestCollaborator::class);
80+
}
81+
82+
public function contributors()
83+
{
84+
return $this->belongsToMany(PivotTestUser::class, 'contributors', 'project_id', 'user_id')
85+
->withPivot('id', 'permissions')
86+
->using(PivotTestContributor::class);
87+
}
88+
}
89+
90+
class PivotTestCollaborator extends Pivot
91+
{
92+
public $table = 'collaborators';
93+
94+
protected $casts = [
95+
'permissions' => 'json',
96+
];
97+
}
98+
99+
class PivotTestContributor extends Pivot
100+
{
101+
public $table = 'contributors';
102+
103+
public $incrementing = true;
104+
105+
protected $casts = [
106+
'permissions' => 'json',
107+
];
108+
}

0 commit comments

Comments
 (0)