Skip to content

Commit b449628

Browse files
committed
chore: update existing tests
1 parent c548b14 commit b449628

File tree

6 files changed

+93
-10
lines changed

6 files changed

+93
-10
lines changed

tests/ProjectionistTest.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Contracts\Container\BindingResolutionException;
77
use Illuminate\Support\Facades\Queue;
88

9+
use Spatie\EventSourcing\Tests\TestClasses\Projectors\ProjectorWithDynamicWeight;
910
use function PHPUnit\Framework\assertCount;
1011
use function PHPUnit\Framework\assertEquals;
1112
use function PHPUnit\Framework\assertInstanceOf;
@@ -66,30 +67,42 @@
6667
assertEquals(1, ProjectorThatThrowsAnException::$exceptionsHandled);
6768
});
6869

69-
it('will call projectors ordered by weight', function () {
70+
it('will call projectors ordered by weight', function (string $eventClass, array $sortedProjectors) {
7071
app()->singleton(ProjectorWithWeightTestHelper::class);
7172

73+
Projectionist::addProjector(ProjectorWithDynamicWeight::class);
7274
Projectionist::addProjector(ProjectorWithHighWeight::class);
7375
Projectionist::addProjector(ProjectorWithoutWeight::class);
7476
Projectionist::addProjector(ProjectorWithNegativeWeight::class);
7577
Projectionist::addProjector(ProjectorWithLowWeight::class);
7678

77-
event(new MoneyAddedEvent($this->account, 1000));
79+
event(new $eventClass($this->account, 1000));
7880

79-
assertSame([
81+
assertSame($sortedProjectors, app(ProjectorWithWeightTestHelper::class)->calledBy);
82+
})->with([
83+
[MoneyAddedEvent::class, [
8084
ProjectorWithNegativeWeight::class,
8185
ProjectorWithoutWeight::class,
8286
ProjectorWithLowWeight::class,
87+
ProjectorWithDynamicWeight::class,
8388
ProjectorWithHighWeight::class,
84-
], app(ProjectorWithWeightTestHelper::class)->calledBy);
85-
});
89+
]],
90+
[MoneySubtractedEvent::class, [
91+
ProjectorWithNegativeWeight::class,
92+
ProjectorWithDynamicWeight::class,
93+
ProjectorWithoutWeight::class,
94+
ProjectorWithLowWeight::class,
95+
ProjectorWithHighWeight::class,
96+
]],
97+
]);
8698

87-
it('will replay projectors ordered by weight', function () {
99+
it('will replay projectors ordered by weight', function (string $eventClass, array $sortedProjectors) {
88100
app()->singleton(ProjectorWithWeightTestHelper::class);
89101

90-
event(new MoneyAddedEvent($this->account, 1000));
102+
event(new $eventClass($this->account, 1000));
91103

92104
Projectionist::addProjectors([
105+
ProjectorWithDynamicWeight::class,
93106
ProjectorWithHighWeight::class,
94107
ProjectorWithoutWeight::class,
95108
ProjectorWithNegativeWeight::class,
@@ -98,13 +111,23 @@
98111

99112
Projectionist::replay(Projectionist::getProjectors());
100113

101-
assertSame([
114+
assertSame($sortedProjectors, app(ProjectorWithWeightTestHelper::class)->calledBy);
115+
})->with([
116+
[MoneyAddedEvent::class, [
102117
ProjectorWithNegativeWeight::class,
103118
ProjectorWithoutWeight::class,
104119
ProjectorWithLowWeight::class,
120+
ProjectorWithDynamicWeight::class,
105121
ProjectorWithHighWeight::class,
106-
], app(ProjectorWithWeightTestHelper::class)->calledBy);
107-
});
122+
]],
123+
[MoneySubtractedEvent::class, [
124+
ProjectorWithNegativeWeight::class,
125+
ProjectorWithDynamicWeight::class,
126+
ProjectorWithoutWeight::class,
127+
ProjectorWithLowWeight::class,
128+
ProjectorWithHighWeight::class,
129+
]],
130+
]);
108131

109132
it('can catch exceptions and still continue calling other projectors', function () {
110133
$this->setConfig('event-sourcing.catch_exceptions', true);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Spatie\EventSourcing\Tests\TestClasses\Projectors;
4+
5+
use Spatie\EventSourcing\EventHandlers\Projectors\Projector;
6+
use Spatie\EventSourcing\StoredEvents\StoredEvent;
7+
use Spatie\EventSourcing\Tests\TestClasses\Events\MoneyAddedEvent;
8+
use Spatie\EventSourcing\Tests\TestClasses\Events\MoneySubtractedEvent;
9+
use Spatie\EventSourcing\Tests\TestClasses\ProjectorWithWeightTestHelper;
10+
11+
class ProjectorWithDynamicWeight extends Projector
12+
{
13+
public function __construct(
14+
private ProjectorWithWeightTestHelper $projectorWithWeightTestHelper,
15+
) {
16+
}
17+
18+
public function getWeight(?StoredEvent $event): int
19+
{
20+
return match ($event?->event_class) {
21+
MoneyAddedEvent::class => 2,
22+
MoneySubtractedEvent::class => -2,
23+
default => 0,
24+
};
25+
}
26+
27+
public function onMoneyAdded(MoneyAddedEvent $event): void
28+
{
29+
$this->projectorWithWeightTestHelper->calledBy(static::class);
30+
}
31+
32+
public function onMoneySubtracted(MoneySubtractedEvent $event): void
33+
{
34+
$this->projectorWithWeightTestHelper->calledBy(static::class);
35+
}
36+
}

tests/TestClasses/Projectors/ProjectorWithHighWeight.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Spatie\EventSourcing\EventHandlers\Projectors\Projector;
66
use Spatie\EventSourcing\Tests\TestClasses\Events\MoneyAddedEvent;
7+
use Spatie\EventSourcing\Tests\TestClasses\Events\MoneySubtractedEvent;
78
use Spatie\EventSourcing\Tests\TestClasses\ProjectorWithWeightTestHelper;
89

910
class ProjectorWithHighWeight extends Projector
@@ -19,4 +20,9 @@ public function onMoneyAdded(MoneyAddedEvent $event): void
1920
{
2021
$this->projectorWithWeightTestHelper->calledBy(static::class);
2122
}
23+
24+
public function onMoneySubtracted(MoneySubtractedEvent $event): void
25+
{
26+
$this->projectorWithWeightTestHelper->calledBy(static::class);
27+
}
2228
}

tests/TestClasses/Projectors/ProjectorWithLowWeight.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Spatie\EventSourcing\EventHandlers\Projectors\Projector;
66
use Spatie\EventSourcing\Tests\TestClasses\Events\MoneyAddedEvent;
7+
use Spatie\EventSourcing\Tests\TestClasses\Events\MoneySubtractedEvent;
78
use Spatie\EventSourcing\Tests\TestClasses\ProjectorWithWeightTestHelper;
89

910
class ProjectorWithLowWeight extends Projector
@@ -19,4 +20,9 @@ public function onMoneyAdded(MoneyAddedEvent $event): void
1920
{
2021
$this->projectorWithWeightTestHelper->calledBy(static::class);
2122
}
23+
24+
public function onMoneySubtracted(MoneySubtractedEvent $event): void
25+
{
26+
$this->projectorWithWeightTestHelper->calledBy(static::class);
27+
}
2228
}

tests/TestClasses/Projectors/ProjectorWithNegativeWeight.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Spatie\EventSourcing\EventHandlers\Projectors\Projector;
66
use Spatie\EventSourcing\Tests\TestClasses\Events\MoneyAddedEvent;
7+
use Spatie\EventSourcing\Tests\TestClasses\Events\MoneySubtractedEvent;
78
use Spatie\EventSourcing\Tests\TestClasses\ProjectorWithWeightTestHelper;
89

910
class ProjectorWithNegativeWeight extends Projector
@@ -19,4 +20,9 @@ public function onMoneyAdded(MoneyAddedEvent $event): void
1920
{
2021
$this->projectorWithWeightTestHelper->calledBy(static::class);
2122
}
23+
24+
public function onMoneySubtracted(MoneySubtractedEvent $event): void
25+
{
26+
$this->projectorWithWeightTestHelper->calledBy(static::class);
27+
}
2228
}

tests/TestClasses/Projectors/ProjectorWithoutWeight.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Spatie\EventSourcing\EventHandlers\Projectors\Projector;
66
use Spatie\EventSourcing\Tests\TestClasses\Events\MoneyAddedEvent;
7+
use Spatie\EventSourcing\Tests\TestClasses\Events\MoneySubtractedEvent;
78
use Spatie\EventSourcing\Tests\TestClasses\ProjectorWithWeightTestHelper;
89

910
class ProjectorWithoutWeight extends Projector
@@ -17,4 +18,9 @@ public function onMoneyAdded(MoneyAddedEvent $event): void
1718
{
1819
$this->projectorWithWeightTestHelper->calledBy(static::class);
1920
}
21+
22+
public function onMoneySubtracted(MoneySubtractedEvent $event): void
23+
{
24+
$this->projectorWithWeightTestHelper->calledBy(static::class);
25+
}
2026
}

0 commit comments

Comments
 (0)