Skip to content

Commit

Permalink
feat(Testing): Add assert Illuminate\Contracts\Events\Dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
pionl committed Nov 22, 2022
1 parent 3bcb0c5 commit 8b2e816
Show file tree
Hide file tree
Showing 13 changed files with 491 additions and 0 deletions.
177 changes: 177 additions & 0 deletions src/Testing/Laravel/Contracts/Events/DispatcherAssert.php
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);
}
}
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,
) {
}
}
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
) {
}
}
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
) {
}
}
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()
{
}
}
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,
) {
}
}
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 src/Testing/Laravel/Contracts/Events/DispatcherPushExpectation.php
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 = [],
) {
}
}
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
) {
}
}
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 = [],
) {
}
}
Loading

0 comments on commit 8b2e816

Please sign in to comment.