-
-
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 assert Illuminate\Contracts\Events\Dispatcher
- Loading branch information
Showing
13 changed files
with
491 additions
and
0 deletions.
There are no files selected for viewing
177 changes: 177 additions & 0 deletions
177
src/Testing/Laravel/Contracts/Events/DispatcherAssert.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,177 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Events; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Events\Dispatcher; | ||
use LaraStrict\Testing\AbstractExpectationCallsMap; | ||
use PHPUnit\Framework\Assert; | ||
|
||
class DispatcherAssert extends AbstractExpectationCallsMap implements Dispatcher | ||
{ | ||
/** | ||
* @param array<DispatcherListenExpectation> $listen | ||
* @param array<DispatcherHasListenersExpectation> $hasListeners | ||
* @param array<DispatcherSubscribeExpectation> $subscribe | ||
* @param array<DispatcherUntilExpectation> $until | ||
* @param array<DispatcherDispatchExpectation> $dispatch | ||
* @param array<DispatcherPushExpectation> $push | ||
* @param array<DispatcherFlushExpectation> $flush | ||
* @param array<DispatcherForgetExpectation> $forget | ||
* @param array<DispatcherForgetPushedExpectation> $forgetPushed | ||
*/ | ||
public function __construct( | ||
array $listen = [], | ||
array $hasListeners = [], | ||
array $subscribe = [], | ||
array $until = [], | ||
array $dispatch = [], | ||
array $push = [], | ||
array $flush = [], | ||
array $forget = [], | ||
array $forgetPushed = [], | ||
) { | ||
$this->setExpectations(DispatcherListenExpectation::class, array_values(array_filter($listen))); | ||
$this->setExpectations(DispatcherHasListenersExpectation::class, array_values(array_filter($hasListeners))); | ||
$this->setExpectations(DispatcherSubscribeExpectation::class, array_values(array_filter($subscribe))); | ||
$this->setExpectations(DispatcherUntilExpectation::class, array_values(array_filter($until))); | ||
$this->setExpectations(DispatcherDispatchExpectation::class, array_values(array_filter($dispatch))); | ||
$this->setExpectations(DispatcherPushExpectation::class, array_values(array_filter($push))); | ||
$this->setExpectations(DispatcherFlushExpectation::class, array_values(array_filter($flush))); | ||
$this->setExpectations(DispatcherForgetExpectation::class, array_values(array_filter($forget))); | ||
$this->setExpectations(DispatcherForgetPushedExpectation::class, array_values(array_filter($forgetPushed))); | ||
} | ||
|
||
/** | ||
* Register an event listener with the dispatcher. | ||
* | ||
* @param Closure|string|array $events | ||
* @param Closure|string|array|null $listener | ||
*/ | ||
public function listen($events, $listener = null) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherListenExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->events, $events, $message); | ||
Assert::assertEquals($expectation->listener, $listener, $message); | ||
} | ||
|
||
/** | ||
* Determine if a given event has listeners. | ||
* | ||
* @param string $eventName | ||
* @return bool | ||
*/ | ||
public function hasListeners($eventName) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherHasListenersExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->eventName, $eventName, $message); | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Register an event subscriber with the dispatcher. | ||
* | ||
* @param object|string $subscriber | ||
*/ | ||
public function subscribe($subscriber) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherSubscribeExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->subscriber, $subscriber, $message); | ||
} | ||
|
||
/** | ||
* Dispatch an event until the first non-null response is returned. | ||
* | ||
* @param string|object $event | ||
* @param mixed $payload | ||
* @return array|null | ||
*/ | ||
public function until($event, $payload = []) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherUntilExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->event, $event, $message); | ||
Assert::assertEquals($expectation->payload, $payload, $message); | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Dispatch an event and call the listeners. | ||
* | ||
* @param string|object $event | ||
* @param mixed $payload | ||
* @param bool $halt | ||
* @return array|null | ||
*/ | ||
public function dispatch($event, $payload = [], $halt = false) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherDispatchExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->event, $event, $message); | ||
Assert::assertEquals($expectation->payload, $payload, $message); | ||
Assert::assertEquals($expectation->halt, $halt, $message); | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Register an event and payload to be fired later. | ||
* | ||
* @param string $event | ||
* @param array $payload | ||
*/ | ||
public function push($event, $payload = []) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherPushExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->event, $event, $message); | ||
Assert::assertEquals($expectation->payload, $payload, $message); | ||
} | ||
|
||
/** | ||
* Flush a set of pushed events. | ||
* | ||
* @param string $event | ||
*/ | ||
public function flush($event) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherFlushExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->event, $event, $message); | ||
} | ||
|
||
/** | ||
* Remove a set of listeners from the dispatcher. | ||
* | ||
* @param string $event | ||
*/ | ||
public function forget($event) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherForgetExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->event, $event, $message); | ||
} | ||
|
||
/** | ||
* Forget all of the queued listeners. | ||
*/ | ||
public function forgetPushed() | ||
{ | ||
$expectation = $this->getExpectation(DispatcherForgetPushedExpectation::class); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Testing/Laravel/Contracts/Events/DispatcherDispatchExpectation.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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Events; | ||
|
||
final class DispatcherDispatchExpectation | ||
{ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly mixed $event, | ||
public readonly mixed $payload = [], | ||
public readonly mixed $halt = false, | ||
) { | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Testing/Laravel/Contracts/Events/DispatcherFlushExpectation.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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Events; | ||
|
||
final class DispatcherFlushExpectation | ||
{ | ||
public function __construct( | ||
public readonly mixed $event | ||
) { | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Testing/Laravel/Contracts/Events/DispatcherForgetExpectation.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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Events; | ||
|
||
final class DispatcherForgetExpectation | ||
{ | ||
public function __construct( | ||
public readonly mixed $event | ||
) { | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Testing/Laravel/Contracts/Events/DispatcherForgetPushedExpectation.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 LaraStrict\Testing\Laravel\Contracts\Events; | ||
|
||
final class DispatcherForgetPushedExpectation | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Testing/Laravel/Contracts/Events/DispatcherHasListenersExpectation.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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Events; | ||
|
||
final class DispatcherHasListenersExpectation | ||
{ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly mixed $eventName, | ||
) { | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Testing/Laravel/Contracts/Events/DispatcherListenExpectation.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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Events; | ||
|
||
final class DispatcherListenExpectation | ||
{ | ||
public function __construct( | ||
public readonly mixed $events, | ||
public readonly mixed $listener = null, | ||
) { | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Testing/Laravel/Contracts/Events/DispatcherPushExpectation.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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Events; | ||
|
||
final class DispatcherPushExpectation | ||
{ | ||
public function __construct( | ||
public readonly mixed $event, | ||
public readonly mixed $payload = [], | ||
) { | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Testing/Laravel/Contracts/Events/DispatcherSubscribeExpectation.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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Events; | ||
|
||
final class DispatcherSubscribeExpectation | ||
{ | ||
public function __construct( | ||
public readonly mixed $subscriber | ||
) { | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Testing/Laravel/Contracts/Events/DispatcherUntilExpectation.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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Events; | ||
|
||
final class DispatcherUntilExpectation | ||
{ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly mixed $event, | ||
public readonly mixed $payload = [], | ||
) { | ||
} | ||
} |
Oops, something went wrong.