|
3 | 3 | namespace Illuminate\Tests\Cache;
|
4 | 4 |
|
5 | 5 | use Illuminate\Cache\ArrayStore;
|
| 6 | +use Illuminate\Cache\Events\CacheFlushed; |
| 7 | +use Illuminate\Cache\Events\CacheFlushing; |
6 | 8 | use Illuminate\Cache\Events\CacheHit;
|
7 | 9 | use Illuminate\Cache\Events\CacheMissed;
|
8 | 10 | use Illuminate\Cache\Events\ForgettingKey;
|
@@ -221,6 +223,44 @@ public function testForgetDoesTriggerFailedEventOnFailure()
|
221 | 223 | $this->assertFalse($repository->forget('baz'));
|
222 | 224 | }
|
223 | 225 |
|
| 226 | + public function testFlushTriggersEvents() |
| 227 | + { |
| 228 | + $dispatcher = $this->getDispatcher(); |
| 229 | + $repository = $this->getRepository($dispatcher); |
| 230 | + |
| 231 | + $dispatcher->shouldReceive('dispatch')->once()->with( |
| 232 | + $this->assertEventMatches(CacheFlushing::class, [ |
| 233 | + 'storeName' => 'array', |
| 234 | + ]) |
| 235 | + ); |
| 236 | + |
| 237 | + $dispatcher->shouldReceive('dispatch')->once()->with( |
| 238 | + $this->assertEventMatches(CacheFlushed::class, [ |
| 239 | + 'storeName' => 'array', |
| 240 | + ]) |
| 241 | + ); |
| 242 | + $this->assertTrue($repository->clear()); |
| 243 | + } |
| 244 | + |
| 245 | + public function testFlushFailureDoesNotDispatchEvent() |
| 246 | + { |
| 247 | + $dispatcher = $this->getDispatcher(); |
| 248 | + |
| 249 | + // Create a store that fails to flush |
| 250 | + $failingStore = m::mock(Store::class); |
| 251 | + $failingStore->shouldReceive('flush')->andReturn(false); |
| 252 | + |
| 253 | + $repository = new Repository($failingStore, ['store' => 'array']); |
| 254 | + $repository->setEventDispatcher($dispatcher); |
| 255 | + |
| 256 | + $dispatcher->shouldReceive('dispatch')->once()->with( |
| 257 | + $this->assertEventMatches(CacheFlushing::class, [ |
| 258 | + 'storeName' => 'array', |
| 259 | + ]) |
| 260 | + ); |
| 261 | + $this->assertFalse($repository->clear()); |
| 262 | + } |
| 263 | + |
224 | 264 | protected function assertEventMatches($eventClass, $properties = [])
|
225 | 265 | {
|
226 | 266 | return m::on(function ($event) use ($eventClass, $properties) {
|
|
0 commit comments