|
10 | 10 | use Illuminate\Database\Grammar; |
11 | 11 | use Illuminate\Support\ItemNotFoundException; |
12 | 12 | use Workbench\Database\Entities\views\FooConnectionUserView; |
| 13 | +use Workbench\Database\Entities\views\NewUserView; |
13 | 14 | use Workbench\Database\Entities\views\UserView; |
14 | 15 |
|
15 | 16 | dataset('drivers', [ |
|
21 | 22 | ]); |
22 | 23 |
|
23 | 24 | dataset('typesAndConnections', [ |
24 | | - 'default args' => ['types' => null, 'connections' => null, 'times' => 2], |
| 25 | + 'default args' => ['types' => null, 'connections' => null, 'times' => 3], |
25 | 26 | 'single specific type' => ['types' => UserView::class, 'connections' => null, 'times' => 1], |
26 | | - 'single connection' => ['types' => null, 'connections' => 'default', 'times' => 1], |
27 | | - 'multiple connections' => ['types' => null, 'connections' => ['default', 'foo'], 'times' => 2], |
28 | | - 'single abstract type' => ['types' => View::class, 'connections' => null, 'times' => 2], |
| 27 | + 'single connection' => ['types' => null, 'connections' => 'default', 'times' => 2], |
| 28 | + 'multiple connections' => ['types' => null, 'connections' => ['default', 'foo'], 'times' => 3], |
| 29 | + 'single abstract type' => ['types' => View::class, 'connections' => null, 'times' => 3], |
29 | 30 | 'multiple types' => ['types' => [UserView::class, FooConnectionUserView::class], 'connections' => null, 'times' => 2], |
30 | 31 | ]); |
31 | 32 |
|
|
86 | 87 | $entity->shouldCreate = false; |
87 | 88 | test()->manager->create($entity); |
88 | 89 | }); |
| 90 | + |
| 91 | + it('skips already created entities', function () { |
| 92 | + test()->connection |
| 93 | + ->shouldReceive('getDriverName')->once()->andReturn('sqlite') |
| 94 | + ->shouldReceive('statement') |
| 95 | + ->once() |
| 96 | + ->withArgs(fn ($sql) => str_contains($sql, 'CREATE')); |
| 97 | + |
| 98 | + test()->manager->create(UserView::class); |
| 99 | + test()->manager->create(UserView::class); |
| 100 | + }); |
| 101 | + |
| 102 | + it('creates an entity\'s dependencies', function () { |
| 103 | + test()->connection |
| 104 | + ->shouldReceive('getDriverName')->times(2)->andReturn('sqlite') |
| 105 | + ->shouldReceive('statement') |
| 106 | + ->times(2) |
| 107 | + ->withArgs(fn ($sql) => str_contains($sql, 'CREATE')); |
| 108 | + |
| 109 | + test()->manager->create(NewUserView::class); |
| 110 | + }); |
89 | 111 | }); |
90 | 112 |
|
91 | 113 | describe('drop', function () { |
|
112 | 134 | $entity->shouldDrop = false; |
113 | 135 | test()->manager->drop($entity); |
114 | 136 | }); |
| 137 | + |
| 138 | + it('skips already dropped entities', function () { |
| 139 | + test()->connection |
| 140 | + ->shouldReceive('getDriverName')->once()->andReturn('sqlite') |
| 141 | + ->shouldReceive('statement') |
| 142 | + ->once() |
| 143 | + ->withArgs(fn ($sql) => str_contains($sql, 'DROP')); |
| 144 | + |
| 145 | + test()->manager->drop(UserView::class); |
| 146 | + test()->manager->drop(UserView::class); |
| 147 | + }); |
115 | 148 | }); |
116 | 149 |
|
117 | 150 | it('creates entities by type and connection', function (array|string|null $types, array|string|null $connections, int $times) { |
|
187 | 220 |
|
188 | 221 | test()->manager->create(new UserView()); |
189 | 222 | })->throws(InvalidArgumentException::class, 'Unsupported driver [unknown].'); |
| 223 | + |
| 224 | +it('flushes the instance', function () { |
| 225 | + test()->connection |
| 226 | + ->shouldReceive('getDriverName')->times(2)->andReturn('sqlite') |
| 227 | + ->shouldReceive('statement') |
| 228 | + ->times(2) |
| 229 | + ->withArgs(fn ($sql) => str_contains($sql, 'CREATE')); |
| 230 | + |
| 231 | + test()->manager->create(UserView::class); |
| 232 | + test()->manager->flush(); |
| 233 | + test()->manager->create(UserView::class); |
| 234 | +}); |
0 commit comments