Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Commit

Permalink
improve test methods
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jul 19, 2019
1 parent 7c2eacc commit bfa2b4c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-event-projector` will be documented in this file

## 2.8.1 - 2019-07-19

- improve test methods

## 2.8.0 - 2019-07-19

- added aggregate test methods
Expand Down
12 changes: 10 additions & 2 deletions src/AggregateRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\EventProjector;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Spatie\EventProjector\Models\StoredEvent;

Expand Down Expand Up @@ -83,8 +84,15 @@ private function apply(ShouldBeStored $event): void
}
}

public static function fake(array $givenEvents = []): FakeAggregateRoot
/**
* @param \Spatie\EventProjector\ShouldBeStored|\Spatie\EventProjector\ShouldBeStored[] $events
*
* @return $this
*/
public static function fake($events = []): FakeAggregateRoot
{
return (new FakeAggregateRoot(app(static::class)))->given($givenEvents);
$events = Arr::wrap($events);

return (new FakeAggregateRoot(app(static::class)))->given($events);
}
}
18 changes: 16 additions & 2 deletions src/FakeAggregateRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ public function __construct(AggregateRoot $aggregateRoot)
$this->aggregateRoot = $aggregateRoot;
}

public function given(array $events)
/**
* @param \Spatie\EventProjector\ShouldBeStored|\Spatie\EventProjector\ShouldBeStored[] $events
*
* @return $this
*/
public function given($events)
{
$events = Arr::wrap($events);

foreach ($events as $event) {
$this->aggregateRoot->recordThat($event);
}
Expand All @@ -40,8 +47,15 @@ public function assertNothingRecorded()
return $this;
}

public function assertRecorded(array $expectedEvents)
/**
* @param \Spatie\EventProjector\ShouldBeStored|\Spatie\EventProjector\ShouldBeStored[] $expectedEvents
*
* @return $this
*/
public function assertRecorded($expectedEvents)
{
$expectedEvents = Arr::wrap($expectedEvents);

Assert::assertEquals($expectedEvents, $this->aggregateRoot->getRecordedEvents());

return $this;
Expand Down
17 changes: 17 additions & 0 deletions tests/FakeAggregateRootTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ public function the_fake_method_can_accept_given_events()
]);
}

/** @test */
public function fake_can_accept_a_single_given_event()
{
DummyAggregateRoot::fake(new DummyEvent(1))
->when(function(DummyAggregateRoot $dummyAggregateRoot) {
$dummyAggregateRoot->dummy();
})
->assertRecorded(new DummyEvent(2));
}

/** @test */
public function assertNotRecorded_can_accept_a_single_given_event()
{
DummyAggregateRoot::fake(new DummyEvent(1))
->assertNotRecorded(DummyEvent::class);
}

/** @test */
public function it_can_assert_that_an_event_is_not_recorded()
{
Expand Down

0 comments on commit bfa2b4c

Please sign in to comment.