Skip to content

Commit

Permalink
feat(Testing): Add Bus dispatcher assert / expectation
Browse files Browse the repository at this point in the history
  • Loading branch information
pionl committed Dec 9, 2022
1 parent 1088d59 commit 3b1984f
Show file tree
Hide file tree
Showing 10 changed files with 419 additions and 0 deletions.
161 changes: 161 additions & 0 deletions src/Testing/Laravel/Contracts/Bus/DispatcherAssert.php
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;
}
}
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,
) {
}
}
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,
) {
}
}
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,
) {
}
}
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,
) {
}
}
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 src/Testing/Laravel/Contracts/Bus/DispatcherMapExpectation.php
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
) {
}
}
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
) {
}
}
Loading

0 comments on commit 3b1984f

Please sign in to comment.