|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cino\LaravelChronos\Tests\Database; |
|
4
|
|
|
|
|
5
|
|
|
use Cake\Chronos\Chronos; |
|
6
|
|
|
use Cake\Chronos\ChronosInterface; |
|
7
|
|
|
use Cino\LaravelChronos\Eloquent\Model; |
|
8
|
|
|
use Cino\LaravelChronos\Eloquent\Relations\BelongsToMany; |
|
9
|
|
|
use Cino\LaravelChronos\Eloquent\Relations\Pivot; |
|
10
|
|
|
use Illuminate\Database\Schema\Blueprint; |
|
11
|
|
|
|
|
12
|
|
|
class EloquentBelongsToManyTest extends EloquentTestCase |
|
13
|
|
|
{ |
|
14
|
|
|
protected function createSchema(): void |
|
15
|
|
|
{ |
|
16
|
|
|
$this->schema()->create('users', function (Blueprint $table) { |
|
17
|
|
|
$table->increments('id'); |
|
18
|
|
|
}); |
|
19
|
|
|
|
|
20
|
|
|
$this->schema()->create('roles', function (Blueprint $table) { |
|
21
|
|
|
$table->increments('id'); |
|
22
|
|
|
$table->date('date'); |
|
23
|
|
|
$table->timestamps(); |
|
24
|
|
|
}); |
|
25
|
|
|
|
|
26
|
|
|
$this->schema()->create('role_user', function (Blueprint $table) { |
|
27
|
|
|
$table->unsignedBigInteger('user_id'); |
|
28
|
|
|
$table->unsignedBigInteger('role_id'); |
|
29
|
|
|
|
|
30
|
|
|
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); |
|
31
|
|
|
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade'); |
|
32
|
|
|
}); |
|
33
|
|
|
|
|
34
|
|
|
$this->schema()->create('role_user_using', function (Blueprint $table) { |
|
35
|
|
|
$table->unsignedBigInteger('user_id'); |
|
36
|
|
|
$table->unsignedBigInteger('role_id'); |
|
37
|
|
|
|
|
38
|
|
|
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); |
|
39
|
|
|
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade'); |
|
40
|
|
|
}); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function seedData(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$user = EloquentBelongsToManyUser::query()->create(['id' => 1]); |
|
46
|
|
|
EloquentBelongsToManyRole::query()->create(['date' => Chronos::now()]); |
|
47
|
|
|
EloquentBelongsToManyRole::query()->create(['date' => Chronos::now()]); |
|
48
|
|
|
EloquentBelongsToManyRole::query()->create(['date' => Chronos::now()]); |
|
49
|
|
|
|
|
50
|
|
|
$user->roles()->sync([3, 1, 2]); |
|
51
|
|
|
$user->rolesUsing()->sync([3, 1, 2]); |
|
52
|
|
|
$user->save(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function tearDown(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->schema()->drop('role_user'); |
|
58
|
|
|
$this->schema()->drop('roles'); |
|
59
|
|
|
$this->schema()->drop('users'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testDateFieldsReturnChronos(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->seedData(); |
|
65
|
|
|
|
|
66
|
|
|
$user = EloquentBelongsToManyUser::query()->first(); |
|
67
|
|
|
|
|
68
|
|
|
// Trigger refresh to ensure relations are still working after. |
|
69
|
|
|
$user = $user->refresh(); |
|
70
|
|
|
|
|
71
|
|
|
foreach ($user->roles as $role) { |
|
72
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $role->created_at); |
|
73
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $role->date); |
|
74
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $role->updated_at); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
foreach ($user->rolesUsing as $role) { |
|
78
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $role->created_at); |
|
79
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $role->date); |
|
80
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $role->updated_at); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->assertSame(Pivot::class, $user->roles()->getPivotClass()); |
|
84
|
|
|
$this->assertSame(EloquentBelongsToManyRoleUsing::class, $user->rolesUsing()->getPivotClass()); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
class EloquentBelongsToManyUser extends Model |
|
89
|
|
|
{ |
|
90
|
|
|
protected $fillable = ['id']; |
|
91
|
|
|
|
|
92
|
|
|
protected $table = 'users'; |
|
93
|
|
|
|
|
94
|
|
|
public $timestamps = false; |
|
95
|
|
|
|
|
96
|
|
|
public function roles(): BelongsToMany |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->belongsToMany(EloquentBelongsToManyRole::class, 'role_user', 'role_id', 'user_id'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function rolesUsing(): BelongsToMany |
|
102
|
|
|
{ |
|
103
|
|
|
return $this |
|
104
|
|
|
->belongsToMany(EloquentBelongsToManyRole::class, 'role_user', 'role_id', 'user_id') |
|
105
|
|
|
->using(EloquentBelongsToManyRoleUsing::class); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
class EloquentBelongsToManyRole extends Model |
|
110
|
|
|
{ |
|
111
|
|
|
protected $dates = ['date']; |
|
112
|
|
|
|
|
113
|
|
|
protected $fillable = ['date', 'id', 'user_id']; |
|
114
|
|
|
|
|
115
|
|
|
protected $table = 'roles'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
class EloquentBelongsToManyRoleUsing extends Pivot |
|
119
|
|
|
{ |
|
120
|
|
|
protected $table = 'role_user_using'; |
|
121
|
|
|
} |
|
122
|
|
|
|