-
-
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 Bus dispatcher assert / expectation
- Loading branch information
Showing
10 changed files
with
419 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,161 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Bus; | ||
|
||
use Illuminate\Contracts\Bus\Dispatcher; | ||
use LaraStrict\Testing\AbstractExpectationCallsMap; | ||
use PHPUnit\Framework\Assert; | ||
|
||
class DispatcherAssert extends AbstractExpectationCallsMap implements Dispatcher | ||
{ | ||
/** | ||
* @param array<DispatcherDispatchExpectation> $dispatch | ||
* @param array<DispatcherDispatchSyncExpectation> $dispatchSync | ||
* @param array<DispatcherDispatchNowExpectation> $dispatchNow | ||
* @param array<DispatcherHasCommandHandlerExpectation> $hasCommandHandler | ||
* @param array<DispatcherGetCommandHandlerExpectation> $getCommandHandler | ||
* @param array<DispatcherPipeThroughExpectation> $pipeThrough | ||
* @param array<DispatcherMapExpectation> $map | ||
*/ | ||
public function __construct( | ||
array $dispatch = [], | ||
array $dispatchSync = [], | ||
array $dispatchNow = [], | ||
array $hasCommandHandler = [], | ||
array $getCommandHandler = [], | ||
array $pipeThrough = [], | ||
array $map = [], | ||
) { | ||
$this->setExpectations(DispatcherDispatchExpectation::class, array_values(array_filter($dispatch))); | ||
$this->setExpectations(DispatcherDispatchSyncExpectation::class, array_values(array_filter($dispatchSync))); | ||
$this->setExpectations(DispatcherDispatchNowExpectation::class, array_values(array_filter($dispatchNow))); | ||
$this->setExpectations( | ||
DispatcherHasCommandHandlerExpectation::class, | ||
array_values(array_filter($hasCommandHandler)) | ||
); | ||
$this->setExpectations( | ||
DispatcherGetCommandHandlerExpectation::class, | ||
array_values(array_filter($getCommandHandler)) | ||
); | ||
$this->setExpectations(DispatcherPipeThroughExpectation::class, array_values(array_filter($pipeThrough))); | ||
$this->setExpectations(DispatcherMapExpectation::class, array_values(array_filter($map))); | ||
} | ||
|
||
/** | ||
* Dispatch a command to its appropriate handler. | ||
* | ||
* @param mixed $command | ||
* @return mixed | ||
*/ | ||
public function dispatch($command) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherDispatchExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->command, $command, $message); | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Dispatch a command to its appropriate handler in the current process. | ||
* | ||
* Queueable jobs will be dispatched to the "sync" queue. | ||
* | ||
* @param mixed $command | ||
* @param mixed $handler | ||
* @return mixed | ||
*/ | ||
public function dispatchSync($command, $handler = null) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherDispatchSyncExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->command, $command, $message); | ||
Assert::assertEquals($expectation->handler, $handler, $message); | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Dispatch a command to its appropriate handler in the current process. | ||
* | ||
* @param mixed $command | ||
* @param mixed $handler | ||
* @return mixed | ||
*/ | ||
public function dispatchNow($command, $handler = null) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherDispatchNowExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->command, $command, $message); | ||
Assert::assertEquals($expectation->handler, $handler, $message); | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Determine if the given command has a handler. | ||
* | ||
* @param mixed $command | ||
* @return bool | ||
*/ | ||
public function hasCommandHandler($command) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherHasCommandHandlerExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->command, $command, $message); | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Retrieve the handler for a command. | ||
* | ||
* @param mixed $command | ||
* @return bool|mixed | ||
*/ | ||
public function getCommandHandler($command) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherGetCommandHandlerExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->command, $command, $message); | ||
|
||
return $expectation->return; | ||
} | ||
|
||
/** | ||
* Set the pipes commands should be piped through before dispatching. | ||
* | ||
* @return $this | ||
*/ | ||
public function pipeThrough(array $pipes) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherPipeThroughExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->pipes, $pipes, $message); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Map a command to a handler. | ||
* | ||
* @return $this | ||
*/ | ||
public function map(array $map) | ||
{ | ||
$expectation = $this->getExpectation(DispatcherMapExpectation::class); | ||
$message = $this->getDebugMessage(); | ||
|
||
Assert::assertEquals($expectation->map, $map, $message); | ||
|
||
return $this; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Testing/Laravel/Contracts/Bus/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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Bus; | ||
|
||
final class DispatcherDispatchExpectation | ||
{ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly mixed $command, | ||
) { | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Testing/Laravel/Contracts/Bus/DispatcherDispatchNowExpectation.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\Bus; | ||
|
||
final class DispatcherDispatchNowExpectation | ||
{ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly mixed $command, | ||
public readonly mixed $handler = null, | ||
) { | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Testing/Laravel/Contracts/Bus/DispatcherDispatchSyncExpectation.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\Bus; | ||
|
||
final class DispatcherDispatchSyncExpectation | ||
{ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly mixed $command, | ||
public readonly mixed $handler = null, | ||
) { | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Testing/Laravel/Contracts/Bus/DispatcherGetCommandHandlerExpectation.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\Bus; | ||
|
||
final class DispatcherGetCommandHandlerExpectation | ||
{ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly mixed $command, | ||
) { | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Testing/Laravel/Contracts/Bus/DispatcherHasCommandHandlerExpectation.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\Bus; | ||
|
||
final class DispatcherHasCommandHandlerExpectation | ||
{ | ||
public function __construct( | ||
public readonly mixed $return, | ||
public readonly mixed $command, | ||
) { | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Testing/Laravel/Contracts/Bus/DispatcherMapExpectation.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\Bus; | ||
|
||
final class DispatcherMapExpectation | ||
{ | ||
public function __construct( | ||
public readonly array $map | ||
) { | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Testing/Laravel/Contracts/Bus/DispatcherPipeThroughExpectation.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\Bus; | ||
|
||
final class DispatcherPipeThroughExpectation | ||
{ | ||
public function __construct( | ||
public readonly array $pipes | ||
) { | ||
} | ||
} |
Oops, something went wrong.