-
-
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 support for multiple listeners in assertEventListe…
…ners BREAKING CHANGE: assertEventListeners accepts list of contracts and their assert listener
- Loading branch information
Showing
7 changed files
with
157 additions
and
12 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
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
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Testing\Concerns; | ||
|
||
use PHPUnit\Framework\Assert; | ||
|
||
class TestCallsListener implements TestListenerCallsContract | ||
{ | ||
public function handle(TestEvent $event): void | ||
{ | ||
Assert::fail('Listener should not be called'); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
Assert::fail('Test should not be called'); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Feature/Testing/Concerns/TestListenerCallsContract.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Testing\Concerns; | ||
|
||
interface TestListenerCallsContract | ||
{ | ||
public function handle(TestEvent $event): void; | ||
|
||
public function test(): void; | ||
} |
42 changes: 42 additions & 0 deletions
42
tests/Feature/Testing/Concerns/TestListenerCallsContractAssert.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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Testing\Concerns; | ||
|
||
use LaraStrict\Testing\AbstractExpectationCallsMap; | ||
use PHPUnit\Framework\Assert; | ||
|
||
class TestListenerCallsContractAssert extends AbstractExpectationCallsMap implements TestListenerCallsContract | ||
{ | ||
/** | ||
* @param array<TestListenerCallsContractHandleExpectation> $handle | ||
* @param array<TestListenerCallsContractTestExpectation> $test | ||
*/ | ||
public function __construct(array $handle = [], array $test = []) | ||
{ | ||
$this->setExpectations(TestListenerCallsContractHandleExpectation::class, array_values(array_filter($handle))); | ||
$this->setExpectations(TestListenerCallsContractTestExpectation::class, array_values(array_filter($test))); | ||
} | ||
|
||
public function handle(TestEvent $event): void | ||
{ | ||
$expectation = $this->getExpectation(TestListenerCallsContractHandleExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->event, $event, $message); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $event, $expectation); | ||
} | ||
} | ||
|
||
public function test(): void | ||
{ | ||
$expectation = $this->getExpectation(TestListenerCallsContractTestExpectation::class); | ||
|
||
if (is_callable($expectation->hook)) { | ||
call_user_func($expectation->hook, $expectation); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/Feature/Testing/Concerns/TestListenerCallsContractHandleExpectation.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 TestListenerCallsContractHandleExpectation | ||
{ | ||
/** | ||
* @param Closure(TestEvent,self):void|null $hook | ||
*/ | ||
public function __construct( | ||
public readonly TestEvent $event, | ||
public readonly ?Closure $hook = null, | ||
) { | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/Feature/Testing/Concerns/TestListenerCallsContractTestExpectation.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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Testing\Concerns; | ||
|
||
use Closure; | ||
|
||
final class TestListenerCallsContractTestExpectation | ||
{ | ||
/** | ||
* @param Closure(self):void|null $hook | ||
*/ | ||
public function __construct( | ||
public readonly ?Closure $hook = null | ||
) { | ||
} | ||
} |