|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Database\Events\QueryExecuted; |
| 4 | +use Illuminate\Support\Facades\DB; |
| 5 | +use PHPUnit\Framework\ExpectationFailedException; |
| 6 | + |
| 7 | +it('can register an event listener', function () { |
| 8 | + $eventDispatcher = DB::getEventDispatcher(); |
| 9 | + |
| 10 | + expect($eventDispatcher->hasListeners(QueryExecuted::class))->toBeFalse(); |
| 11 | + |
| 12 | + $this->startCountingQueries(); |
| 13 | + |
| 14 | + expect($eventDispatcher->hasListeners(QueryExecuted::class))->toBeTrue(); |
| 15 | +}); |
| 16 | + |
| 17 | +it('can count an event dispatch', function () { |
| 18 | + $eventDispatcher = DB::getEventDispatcher(); |
| 19 | + |
| 20 | + $this->startCountingQueries(); |
| 21 | + |
| 22 | + expect($this->getQueryCount())->toBe(0); |
| 23 | + |
| 24 | + $eventDispatcher->dispatch(QueryExecuted::class); |
| 25 | + expect($this->getQueryCount())->toBe(1); |
| 26 | +}); |
| 27 | + |
| 28 | +it('can stop counting event dispatches', function () { |
| 29 | + $eventDispatcher = DB::getEventDispatcher(); |
| 30 | + |
| 31 | + $this->startCountingQueries(); |
| 32 | + |
| 33 | + expect($this->getQueryCount())->toBe(0); |
| 34 | + |
| 35 | + $eventDispatcher->dispatch(QueryExecuted::class); |
| 36 | + expect($this->getQueryCount())->toBe(1); |
| 37 | + |
| 38 | + $this->stopCountingQueries(); |
| 39 | + |
| 40 | + $eventDispatcher->dispatch(QueryExecuted::class); |
| 41 | + expect($this->getQueryCount())->toBe(1); |
| 42 | +}); |
| 43 | + |
| 44 | +it('can assert about event dispatches', function () { |
| 45 | + $eventDispatcher = DB::getEventDispatcher(); |
| 46 | + |
| 47 | + $this->startCountingQueries(); |
| 48 | + |
| 49 | + $this->assertDatabaseQueriesCount(0); |
| 50 | + |
| 51 | + $eventDispatcher->dispatch(QueryExecuted::class); |
| 52 | + $this->assertDatabaseQueriesCount(1); |
| 53 | + |
| 54 | + $this->assertDatabaseQueriesCount(2); |
| 55 | +})->throws(ExpectationFailedException::class, 'Failed asserting that 1 matches expected 2.'); |
| 56 | + |
| 57 | +it('can count an event dispatch with a callback and return its result', function () { |
| 58 | + $eventDispatcher = DB::getEventDispatcher(); |
| 59 | + |
| 60 | + expect($this->getQueryCount())->toBe(0); |
| 61 | + |
| 62 | + $receivedResult = $this->whileCountingQueries(function () use ($eventDispatcher) { |
| 63 | + $eventDispatcher->dispatch(QueryExecuted::class); |
| 64 | + |
| 65 | + return true; |
| 66 | + }); |
| 67 | + |
| 68 | + expect($this->getQueryCount())->toBe(1); |
| 69 | + expect($receivedResult)->toBeTrue(); |
| 70 | +}); |
| 71 | + |
| 72 | +it('stops counting event dispatches after run the given callback', function () { |
| 73 | + $eventDispatcher = DB::getEventDispatcher(); |
| 74 | + |
| 75 | + expect($this->getQueryCount())->toBe(0); |
| 76 | + |
| 77 | + $this->whileCountingQueries(fn () => $eventDispatcher->dispatch(QueryExecuted::class)); |
| 78 | + |
| 79 | + expect($this->getQueryCount())->toBe(1); |
| 80 | + |
| 81 | + $eventDispatcher->dispatch(QueryExecuted::class); |
| 82 | + expect($this->getQueryCount())->toBe(1); |
| 83 | +}); |
0 commit comments