forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseEloquentBelongsToManyOrFailTest.php
More file actions
191 lines (148 loc) · 4.68 KB
/
DatabaseEloquentBelongsToManyOrFailTest.php
File metadata and controls
191 lines (148 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace Illuminate\Tests\Database;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use PHPUnit\Framework\TestCase;
class DatabaseEloquentBelongsToManyOrFailTest extends TestCase
{
protected function setUp(): void
{
$db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->bootEloquent();
$db->setAsGlobal();
$this->createSchema();
}
public function createSchema()
{
$this->schema()->create('users', function ($table) {
$table->increments('id');
$table->string('email');
});
$this->schema()->create('roles', function ($table) {
$table->increments('id');
$table->string('name');
});
$this->schema()->create('role_user', function ($table) {
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->boolean('active')->default(false);
});
}
protected function tearDown(): void
{
$this->schema()->drop('users');
$this->schema()->drop('roles');
$this->schema()->drop('role_user');
}
protected function seedData()
{
OrFailUser::create(['id' => 1, 'email' => 'taylor@laravel.com']);
OrFailRole::insert([
['id' => 1, 'name' => 'Admin'],
['id' => 2, 'name' => 'Editor'],
['id' => 3, 'name' => 'Viewer'],
]);
}
public function testSyncOrFail()
{
$this->seedData();
$user = OrFailUser::find(1);
$result = $user->roles()->syncOrFail([1, 2]);
$this->assertEquals([1, 2], $result['attached']);
$this->assertEmpty($result['detached']);
$this->assertEmpty($result['updated']);
$this->assertCount(2, $user->roles);
}
public function testSyncWithoutDetachingOrFail()
{
$this->seedData();
$user = OrFailUser::find(1);
$user->roles()->attach([1]);
$result = $user->roles()->syncWithoutDetachingOrFail([2, 3]);
$this->assertEquals([2, 3], $result['attached']);
$this->assertEmpty($result['detached']);
$this->assertCount(3, $user->roles()->get());
}
public function testAttachOrFail()
{
$this->seedData();
$user = OrFailUser::find(1);
$user->roles()->attachOrFail(1);
$this->assertCount(1, $user->roles);
}
public function testAttachOrFailWithAttributes()
{
$this->seedData();
$user = OrFailUser::find(1);
$user->roles()->attachOrFail(1, ['active' => true]);
$pivot = DB::table('role_user')->where('user_id', 1)->where('role_id', 1)->first();
$this->assertEquals(1, $pivot->active);
}
public function testDetachOrFail()
{
$this->seedData();
$user = OrFailUser::find(1);
$user->roles()->attach([1, 2, 3]);
$result = $user->roles()->detachOrFail([1, 2]);
$this->assertEquals(2, $result);
$this->assertCount(1, $user->roles()->get());
}
public function testDetachOrFailAll()
{
$this->seedData();
$user = OrFailUser::find(1);
$user->roles()->attach([1, 2, 3]);
$result = $user->roles()->detachOrFail();
$this->assertEquals(3, $result);
$this->assertCount(0, $user->roles()->get());
}
public function testToggleOrFail()
{
$this->seedData();
$user = OrFailUser::find(1);
$user->roles()->attach([1]);
$result = $user->roles()->toggleOrFail([1, 2]);
$this->assertEquals([1], $result['detached']);
$this->assertEquals([2], $result['attached']);
$this->assertCount(1, $user->roles()->get());
}
/**
* Get a database connection instance.
*
* @return \Illuminate\Database\Connection
*/
protected function connection()
{
return Eloquent::getConnectionResolver()->connection();
}
/**
* Get a schema builder instance.
*
* @return \Illuminate\Database\Schema\Builder
*/
protected function schema()
{
return $this->connection()->getSchemaBuilder();
}
}
class OrFailUser extends Eloquent
{
protected $table = 'users';
protected $guarded = [];
public $timestamps = false;
public function roles(): BelongsToMany
{
return $this->belongsToMany(OrFailRole::class, 'role_user', 'user_id', 'role_id');
}
}
class OrFailRole extends Eloquent
{
protected $table = 'roles';
protected $guarded = [];
public $timestamps = false;
}