-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Testing): Add ability to assert event listeners with assert class
- Loading branch information
Showing
7 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Concerns; | ||
|
||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Events\Dispatcher; | ||
use PHPUnit\Framework\Assert; | ||
|
||
trait AssertEventListeners | ||
{ | ||
/** | ||
* @param bool $disableWildcard You can receive un-wanted listener responses (like laravel-ray). By default, we will remove any wildcard event. | ||
*/ | ||
public function assertEventListeners( | ||
Application $app, | ||
object $event, | ||
string $contract, | ||
object $assert, | ||
array $expectedListenerResults = [null], | ||
bool $disableWildcard = true | ||
): void { | ||
$app->bind(abstract: $contract, concrete: static fn () => $assert); | ||
|
||
/** @var Dispatcher $events */ | ||
$events = $app->make(Dispatcher::class); | ||
|
||
if ($disableWildcard) { | ||
$events->forget('*'); | ||
} | ||
|
||
$results = $events->dispatch($event); | ||
|
||
Assert::assertEquals($expectedListenerResults, $results); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
tests/Feature/Testing/Concerns/AssertEventListenersTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Testing\Concerns; | ||
|
||
use Illuminate\Events\Dispatcher; | ||
use LaraStrict\Testing\Concerns\AssertEventListeners; | ||
use Tests\LaraStrict\Feature\TestCase; | ||
|
||
class AssertEventListenersTest extends TestCase | ||
{ | ||
use AssertEventListeners; | ||
|
||
public function testAssertEventListeners(): void | ||
{ | ||
// Set the event as provided would | ||
/** @var Dispatcher $events */ | ||
$events = $this->app() | ||
->get(Dispatcher::class); | ||
$this->app() | ||
->bind(TestListenerContract::class, TestListener::class); | ||
|
||
$events->listen(TestEvent::class, TestListenerContract::class); | ||
|
||
$event = new TestEvent(); | ||
$this->assertEventListeners( | ||
app: $this->app(), | ||
event: $event, | ||
contract: TestListenerContract::class, | ||
assert: new TestListenerContractAssert([new TestListenerContractExpectation(event: $event)]) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Testing\Concerns; | ||
|
||
class TestEvent | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Testing\Concerns; | ||
|
||
use PHPUnit\Framework\Assert; | ||
|
||
class TestListener implements TestListenerContract | ||
{ | ||
public function handle(TestEvent $event): void | ||
{ | ||
Assert::fail('Listener should not be called'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Testing\Concerns; | ||
|
||
interface TestListenerContract | ||
{ | ||
public function handle(TestEvent $event): void; | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/Feature/Testing/Concerns/TestListenerContractAssert.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Testing\Concerns; | ||
|
||
use LaraStrict\Testing\AbstractExpectationCallMap; | ||
use PHPUnit\Framework\Assert; | ||
|
||
/** | ||
* @extends AbstractExpectationCallMap<TestListenerContractExpectation> | ||
*/ | ||
class TestListenerContractAssert extends AbstractExpectationCallMap implements TestListenerContract | ||
{ | ||
public function handle(TestEvent $event): void | ||
{ | ||
$expectation = $this->getExpectation(); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->event, $event, $message); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $event, $expectation); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/Feature/Testing/Concerns/TestListenerContractExpectation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Testing\Concerns; | ||
|
||
use Closure; | ||
|
||
final class TestListenerContractExpectation | ||
{ | ||
/** | ||
* @param Closure(TestEvent,self):void|null $hook | ||
*/ | ||
public function __construct( | ||
public readonly TestEvent $event, | ||
public readonly ?Closure $hook = null, | ||
) { | ||
} | ||
} |