-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07c4a52
commit a385761
Showing
3 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Subscription\Engine; | ||
|
||
use Patchlevel\EventSourcing\Store\Store; | ||
use Patchlevel\EventSourcing\Subscription\Subscription; | ||
|
||
use const PHP_INT_MAX; | ||
|
||
final class CatchUpSubscriptionEngine implements SubscriptionEngine | ||
{ | ||
public function __construct( | ||
private readonly SubscriptionEngine $parent, | ||
private readonly Store $store, | ||
private readonly int $limit = PHP_INT_MAX, | ||
) { | ||
} | ||
|
||
public function setup(SubscriptionEngineCriteria|null $criteria = null, bool $skipBooting = false): void | ||
Check warning on line 21 in src/Subscription/Engine/CatchUpSubscriptionEngine.php GitHub Actions / Mutation tests on diff (locked, 8.3, ubuntu-latest)
|
||
{ | ||
$this->parent->setup($criteria, $skipBooting); | ||
} | ||
|
||
public function boot(SubscriptionEngineCriteria|null $criteria = null, int|null $limit = null): void | ||
{ | ||
for ($i = 0; $i < $this->limit; $i++) { | ||
$current = $this->store->count(); | ||
$this->parent->boot($criteria, $limit); | ||
if ($current === $this->store->count()) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public function run(SubscriptionEngineCriteria|null $criteria = null, int|null $limit = null): void | ||
{ | ||
for ($i = 0; $i < $this->limit; $i++) { | ||
Check warning on line 39 in src/Subscription/Engine/CatchUpSubscriptionEngine.php GitHub Actions / Mutation tests on diff (locked, 8.3, ubuntu-latest)
Check warning on line 39 in src/Subscription/Engine/CatchUpSubscriptionEngine.php GitHub Actions / Mutation tests on diff (locked, 8.3, ubuntu-latest)
Check warning on line 39 in src/Subscription/Engine/CatchUpSubscriptionEngine.php GitHub Actions / Mutation tests on diff (locked, 8.3, ubuntu-latest)
|
||
$current = $this->store->count(); | ||
$this->parent->run($criteria, $limit); | ||
if ($current === $this->store->count()) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public function teardown(SubscriptionEngineCriteria|null $criteria = null): void | ||
{ | ||
$this->parent->teardown($criteria); | ||
} | ||
|
||
public function remove(SubscriptionEngineCriteria|null $criteria = null): void | ||
{ | ||
$this->parent->remove($criteria); | ||
} | ||
|
||
public function reactivate(SubscriptionEngineCriteria|null $criteria = null): void | ||
{ | ||
$this->parent->reactivate($criteria); | ||
} | ||
|
||
public function pause(SubscriptionEngineCriteria|null $criteria = null): void | ||
{ | ||
$this->parent->pause($criteria); | ||
} | ||
|
||
/** @return list<Subscription> */ | ||
public function subscriptions(SubscriptionEngineCriteria|null $criteria = null): array | ||
{ | ||
return $this->parent->subscriptions($criteria); | ||
} | ||
} |
179 changes: 179 additions & 0 deletions
179
tests/Unit/Subscription/Engine/CatchUpSubscriptionEngineTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Tests\Unit\Subscription\Engine; | ||
|
||
use Patchlevel\EventSourcing\Store\Store; | ||
use Patchlevel\EventSourcing\Subscription\Engine\CatchUpSubscriptionEngine; | ||
use Patchlevel\EventSourcing\Subscription\Engine\SubscriptionEngine; | ||
use Patchlevel\EventSourcing\Subscription\Engine\SubscriptionEngineCriteria; | ||
use Patchlevel\EventSourcing\Subscription\Subscription; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
|
||
/** @covers \Patchlevel\EventSourcing\Subscription\Engine\CatchUpSubscriptionEngine */ | ||
final class CatchUpSubscriptionEngineTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
public function testSetup(): void | ||
{ | ||
$parent = $this->prophesize(SubscriptionEngine::class); | ||
$store = $this->prophesize(Store::class); | ||
|
||
$engine = new CatchUpSubscriptionEngine($parent->reveal(), $store->reveal()); | ||
$criteria = new SubscriptionEngineCriteria(); | ||
|
||
$parent->setup($criteria, true)->shouldBeCalled(); | ||
$engine->setup($criteria, true); | ||
} | ||
|
||
public function testBootFinished(): void | ||
{ | ||
$parent = $this->prophesize(SubscriptionEngine::class); | ||
$store = $this->prophesize(Store::class); | ||
|
||
$engine = new CatchUpSubscriptionEngine($parent->reveal(), $store->reveal()); | ||
$criteria = new SubscriptionEngineCriteria(); | ||
|
||
$store->count()->willReturn(42)->shouldBeCalledTimes(2); | ||
|
||
$parent->run($criteria, 42)->shouldBeCalledOnce(); | ||
$engine->run($criteria, 42); | ||
} | ||
|
||
public function testBootSecondTime(): void | ||
{ | ||
$parent = $this->prophesize(SubscriptionEngine::class); | ||
$store = $this->prophesize(Store::class); | ||
|
||
$engine = new CatchUpSubscriptionEngine($parent->reveal(), $store->reveal()); | ||
$criteria = new SubscriptionEngineCriteria(); | ||
|
||
$store->count()->willReturn(42, 43, 43); | ||
|
||
$parent->run($criteria, 42)->shouldBeCalledTimes(2); | ||
$engine->run($criteria, 42); | ||
} | ||
|
||
public function testBootLimit(): void | ||
{ | ||
$parent = $this->prophesize(SubscriptionEngine::class); | ||
$store = $this->prophesize(Store::class); | ||
|
||
$engine = new CatchUpSubscriptionEngine($parent->reveal(), $store->reveal(), 2); | ||
$criteria = new SubscriptionEngineCriteria(); | ||
|
||
$store->count()->willReturn(42, 43, 44); | ||
|
||
$parent->run($criteria, 42)->shouldBeCalledTimes(2); | ||
$engine->run($criteria, 42); | ||
} | ||
|
||
public function testRunFinished(): void | ||
{ | ||
$parent = $this->prophesize(SubscriptionEngine::class); | ||
$store = $this->prophesize(Store::class); | ||
|
||
$engine = new CatchUpSubscriptionEngine($parent->reveal(), $store->reveal()); | ||
$criteria = new SubscriptionEngineCriteria(); | ||
|
||
$store->count()->willReturn(42)->shouldBeCalledTimes(2); | ||
|
||
$parent->run($criteria, 42)->shouldBeCalledOnce(); | ||
$engine->run($criteria, 42); | ||
} | ||
|
||
public function testRunSecondTime(): void | ||
{ | ||
$parent = $this->prophesize(SubscriptionEngine::class); | ||
$store = $this->prophesize(Store::class); | ||
|
||
$engine = new CatchUpSubscriptionEngine($parent->reveal(), $store->reveal()); | ||
$criteria = new SubscriptionEngineCriteria(); | ||
|
||
$store->count()->willReturn(42, 43, 43); | ||
|
||
$parent->run($criteria, 42)->shouldBeCalledTimes(2); | ||
$engine->run($criteria, 42); | ||
} | ||
|
||
public function testRunLimit(): void | ||
{ | ||
$parent = $this->prophesize(SubscriptionEngine::class); | ||
$store = $this->prophesize(Store::class); | ||
|
||
$engine = new CatchUpSubscriptionEngine($parent->reveal(), $store->reveal(), 2); | ||
$criteria = new SubscriptionEngineCriteria(); | ||
|
||
$store->count()->willReturn(42, 43, 44); | ||
|
||
$parent->run($criteria, 42)->shouldBeCalledTimes(2); | ||
$engine->run($criteria, 42); | ||
} | ||
|
||
public function testTeardown(): void | ||
{ | ||
$parent = $this->prophesize(SubscriptionEngine::class); | ||
$store = $this->prophesize(Store::class); | ||
|
||
$engine = new CatchUpSubscriptionEngine($parent->reveal(), $store->reveal()); | ||
$criteria = new SubscriptionEngineCriteria(); | ||
|
||
$parent->teardown($criteria)->shouldBeCalled(); | ||
$engine->teardown($criteria); | ||
} | ||
|
||
public function testRemove(): void | ||
{ | ||
$parent = $this->prophesize(SubscriptionEngine::class); | ||
$store = $this->prophesize(Store::class); | ||
|
||
$engine = new CatchUpSubscriptionEngine($parent->reveal(), $store->reveal()); | ||
$criteria = new SubscriptionEngineCriteria(); | ||
|
||
$parent->remove($criteria)->shouldBeCalled(); | ||
$engine->remove($criteria); | ||
} | ||
|
||
public function testReactivate(): void | ||
{ | ||
$parent = $this->prophesize(SubscriptionEngine::class); | ||
$store = $this->prophesize(Store::class); | ||
|
||
$engine = new CatchUpSubscriptionEngine($parent->reveal(), $store->reveal()); | ||
$criteria = new SubscriptionEngineCriteria(); | ||
|
||
$parent->reactivate($criteria)->shouldBeCalled(); | ||
$engine->reactivate($criteria); | ||
} | ||
|
||
public function testPause(): void | ||
{ | ||
$parent = $this->prophesize(SubscriptionEngine::class); | ||
$store = $this->prophesize(Store::class); | ||
|
||
$engine = new CatchUpSubscriptionEngine($parent->reveal(), $store->reveal()); | ||
$criteria = new SubscriptionEngineCriteria(); | ||
|
||
$parent->pause($criteria)->shouldBeCalled(); | ||
$engine->pause($criteria); | ||
} | ||
|
||
public function testSubscriptions(): void | ||
{ | ||
$parent = $this->prophesize(SubscriptionEngine::class); | ||
$store = $this->prophesize(Store::class); | ||
|
||
$engine = new CatchUpSubscriptionEngine($parent->reveal(), $store->reveal()); | ||
$criteria = new SubscriptionEngineCriteria(); | ||
|
||
$expectedSubscriptions = [new Subscription('foo')]; | ||
|
||
$parent->subscriptions($criteria)->willReturn($expectedSubscriptions)->shouldBeCalled(); | ||
$subscriptions = $engine->subscriptions($criteria); | ||
|
||
self::assertEquals($expectedSubscriptions, $subscriptions); | ||
} | ||
} |