Commit 1672646a authored by Mickaël Bourgier's avatar Mickaël Bourgier
Browse files

Create a StreamStub for tests

parent e05ce874
......@@ -5,8 +5,8 @@ namespace Tests\Stream;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Tests\Event\Stub\EventStub;
use Tests\Stream\Stub\StreamStub;
use Webf\ProjectionBundle\Stream\FilterStream;
use Webf\ProjectionBundle\Stream\StreamInterface;
/**
* @internal
......@@ -16,11 +16,7 @@ class FilterStreamTest extends TestCase
{
public function test_given_event_types_must_be_supported_by_inner_stream()
{
$innerStream = $this->createMock(StreamInterface::class);
$innerStream
->method('getEventTypes')
->willReturn(['a', 'b', 'c'])
;
$innerStream = new StreamStub(['a', 'b', 'c']);
$filteredEvents = ['d'];
$this->expectException(InvalidArgumentException::class);
......@@ -30,11 +26,7 @@ class FilterStreamTest extends TestCase
public function test_get_event_types_returns_given_event_types()
{
$innerStream = $this->createMock(StreamInterface::class);
$innerStream
->method('getEventTypes')
->willReturn(['a', 'b', 'c'])
;
$innerStream = new StreamStub(['a', 'b', 'c']);
$filteredEvents = ['c', 'b'];
$stream = new FilterStream($innerStream, $filteredEvents);
......@@ -47,13 +39,7 @@ class FilterStreamTest extends TestCase
$a = new EventStub('a');
$b = new EventStub('b');
$c = new EventStub('c');
$innerStream = $this->createMock(StreamInterface::class);
$innerStream
->method('getEventTypes')
->willReturn(['a', 'b', 'c'])
;
$innerStream->method('nextEvent')->willReturnOnConsecutiveCalls($a, $b, $c, $c, $a, $b, $a, $c, null);
$innerStream = StreamStub::fromEvents([$a, $b, $c, $c, $a, $b, $a, $c]);
$stream = new FilterStream($innerStream, ['a', 'b']);
......
<?php
namespace Tests\Stream\Stub;
use Webf\ProjectionBundle\Event\EventInterface;
use Webf\ProjectionBundle\Stream\StreamInterface;
class StreamStub implements StreamInterface
{
/** @var string[] */
private array $eventTypes;
/** @var EventInterface[] */
private array $events;
/**
* @param string[] $eventTypes
* @param EventInterface[] $events
*/
public function __construct(array $eventTypes = [], array $events = [])
{
$this->eventTypes = $eventTypes;
$this->events = $events;
}
/**
* @param EventInterface[] $events
*/
public static function fromEvents(array $events = []): self
{
$eventTypes = [];
foreach ($events as $event) {
$eventTypes[$event->getType()] = true;
}
return new self(array_keys($eventTypes), $events);
}
public function getEventTypes(): array
{
return $this->eventTypes;
}
public function nextEvent(): ?EventInterface
{
return array_shift($this->events);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment