Skip to content

Commit 3b26145

Browse files
committed
rename classes, add more tests & improve dbal cleanup task handler
1 parent 133322d commit 3b26145

10 files changed

Lines changed: 331 additions & 51 deletions

File tree

docs/pages/subscription.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,9 +1026,9 @@ The handler must implement the `CleanupHandler` interface.
10261026

10271027
```php
10281028
use MongoDb\Database;
1029-
use Patchlevel\EventSourcing\Subscription\Cleanup\CleanupHandler;
1029+
use Patchlevel\EventSourcing\Subscription\Cleanup\CleanupTaskHandler;
10301030

1031-
final class MongodbCleanupHandler implements CleanupHandler
1031+
final class MongodbCleanupTaskHandler implements CleanupTaskHandler
10321032
{
10331033
public function __construct(
10341034
private readonly Database $database,
@@ -1054,11 +1054,12 @@ Lastly, we have to add the new handler to `DefaultCleaner`,
10541054
which is responsible for cleaning up subscriptions.
10551055

10561056
```php
1057+
use Patchlevel\EventSourcing\Subscription\Cleanup\Dbal\DbalCleanupTaskHandler;
10571058
use Patchlevel\EventSourcing\Subscription\Cleanup\DefaultCleaner;
10581059

10591060
$cleaner = new DefaultCleaner([
1060-
new DbalCleanupHandler($projectionConnection),
1061-
new MongodbCleanupHandler($mongodbDatabase),
1061+
new DbalCleanupTaskHandler($projectionConnection),
1062+
new MongodbCleanupTaskHandler($mongodbDatabase),
10621063
]);
10631064
```
10641065
!!! warning
@@ -1093,7 +1094,7 @@ Finally, if we want to use the cleanup feature, we need to pass the cleanup hand
10931094

10941095
```php
10951096
use Doctrine\DBAL\Connection;
1096-
use Patchlevel\EventSourcing\Subscription\Cleanup\Dbal\DbalCleanupHandler;
1097+
use Patchlevel\EventSourcing\Subscription\Cleanup\Dbal\DbalCleanupTaskHandler;
10971098
use Patchlevel\EventSourcing\Subscription\Cleanup\DefaultCleaner;
10981099
use Patchlevel\EventSourcing\Subscription\Engine\DefaultSubscriptionEngine;
10991100
use Patchlevel\EventSourcing\Subscription\Engine\MessageLoader;
@@ -1115,7 +1116,7 @@ $subscriptionEngine = new DefaultSubscriptionEngine(
11151116
$subscriberAccessorRepository,
11161117
$retryStrategyRepository, // optional, if not set the default retry strategy is used
11171118
$logger, // optional
1118-
new DefaultCleaner([new DbalCleanupHandler($projectionConnection)]), // optional but required if you want to use the cleanup feature
1119+
new DefaultCleaner([new DbalCleanupTaskHandler($projectionConnection)]), // optional but required if you want to use the cleanup feature
11191120
);
11201121
```
11211122
### Catch up Subscription Engine

src/Subscription/Cleanup/CleanupHandler.php renamed to src/Subscription/Cleanup/CleanupTaskHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Patchlevel\EventSourcing\Subscription\Cleanup;
66

7-
interface CleanupHandler
7+
interface CleanupTaskHandler
88
{
99
public function __invoke(object $task): void;
1010

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcing\Subscription\Cleanup;
6+
7+
use RuntimeException;
8+
9+
use function sprintf;
10+
11+
final class CleanupTaskNotSupported extends RuntimeException
12+
{
13+
/** @param class-string<CleanupTaskHandler> $handler */
14+
public function __construct(
15+
object $task,
16+
string $handler,
17+
) {
18+
parent::__construct(
19+
sprintf('Task "%s" is not supported by handler "%s"', $task::class, $handler),
20+
);
21+
}
22+
}

src/Subscription/Cleanup/Dbal/DbalCleanupHandler.php

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcing\Subscription\Cleanup\Dbal;
6+
7+
use Doctrine\DBAL\Connection;
8+
use Patchlevel\EventSourcing\Subscription\Cleanup\CleanupTaskHandler;
9+
use Patchlevel\EventSourcing\Subscription\Cleanup\CleanupTaskNotSupported;
10+
11+
final class DbalCleanupTaskHandler implements CleanupTaskHandler
12+
{
13+
public function __construct(
14+
private readonly Connection $connection,
15+
) {
16+
}
17+
18+
public function __invoke(object $task): void
19+
{
20+
if ($task instanceof DropTableTask) {
21+
$this->connection->createSchemaManager()->dropTable($task->table);
22+
23+
return;
24+
}
25+
26+
if ($task instanceof DropIndexTask) {
27+
$this->connection->createSchemaManager()->dropIndex($task->index, $task->table);
28+
29+
return;
30+
}
31+
32+
throw new CleanupTaskNotSupported($task, self::class);
33+
}
34+
35+
public function supports(object $task): bool
36+
{
37+
return $task instanceof DropTableTask || $task instanceof DropIndexTask;
38+
}
39+
}

src/Subscription/Cleanup/DefaultCleaner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
final class DefaultCleaner implements Cleaner
1111
{
12-
/** @param iterable<CleanupHandler> $handlers */
12+
/** @param iterable<CleanupTaskHandler> $handlers */
1313
public function __construct(
1414
private readonly iterable $handlers = [],
1515
) {

tests/Integration/Subscription/SubscriptionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use Patchlevel\EventSourcing\Serializer\DefaultEventSerializer;
2222
use Patchlevel\EventSourcing\Store\DoctrineDbalStore;
2323
use Patchlevel\EventSourcing\Store\StreamDoctrineDbalStore;
24-
use Patchlevel\EventSourcing\Subscription\Cleanup\Dbal\DbalCleanupHandler;
24+
use Patchlevel\EventSourcing\Subscription\Cleanup\Dbal\DbalCleanupTaskHandler;
2525
use Patchlevel\EventSourcing\Subscription\Cleanup\Dbal\DropTableTask;
2626
use Patchlevel\EventSourcing\Subscription\Cleanup\DefaultCleaner;
2727
use Patchlevel\EventSourcing\Subscription\Engine\CatchUpSubscriptionEngine;
@@ -1186,7 +1186,7 @@ public function testCleanup(): void
11861186
// Test Setup
11871187

11881188
$cleaner = new DefaultCleaner([
1189-
new DbalCleanupHandler(
1189+
new DbalCleanupTaskHandler(
11901190
$this->projectionConnection,
11911191
),
11921192
]);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcing\Tests\Unit\Subscription\Cleanup\Dbal;
6+
7+
use Doctrine\DBAL\Connection;
8+
use Doctrine\DBAL\Schema\AbstractSchemaManager;
9+
use Patchlevel\EventSourcing\Subscription\Cleanup\CleanupTaskNotSupported;
10+
use Patchlevel\EventSourcing\Subscription\Cleanup\Dbal\DbalCleanupTaskHandler;
11+
use Patchlevel\EventSourcing\Subscription\Cleanup\Dbal\DropIndexTask;
12+
use Patchlevel\EventSourcing\Subscription\Cleanup\Dbal\DropTableTask;
13+
use PHPUnit\Framework\Attributes\CoversClass;
14+
use PHPUnit\Framework\TestCase;
15+
use stdClass;
16+
17+
#[CoversClass(DbalCleanupTaskHandler::class)]
18+
final class DbalCleanupTaskHandlerTest extends TestCase
19+
{
20+
public function testNoSupport(): void
21+
{
22+
$handler = new DbalCleanupTaskHandler(
23+
$this->createMock(Connection::class),
24+
);
25+
26+
self::assertFalse($handler->supports(new stdClass()));
27+
}
28+
29+
public function testSupports(): void
30+
{
31+
$handler = new DbalCleanupTaskHandler(
32+
$this->createMock(Connection::class),
33+
);
34+
35+
self::assertTrue($handler->supports(new DropTableTask('test')));
36+
self::assertTrue($handler->supports(new DropIndexTask('test', 'test')));
37+
}
38+
39+
public function testHandleNoSupportedTask(): void
40+
{
41+
$handler = new DbalCleanupTaskHandler(
42+
$this->createMock(Connection::class),
43+
);
44+
45+
$this->expectException(CleanupTaskNotSupported::class);
46+
47+
$handler(new stdClass());
48+
}
49+
50+
public function testHandleDropTable(): void
51+
{
52+
$schemaManager = $this->createMock(AbstractSchemaManager::class);
53+
$schemaManager->expects($this->once())->method('dropTable')->with('test');
54+
55+
$connection = $this->createMock(Connection::class);
56+
$connection
57+
->expects($this->once())
58+
->method('createSchemaManager')
59+
->willReturn($schemaManager);
60+
61+
$handler = new DbalCleanupTaskHandler($connection);
62+
63+
$handler(new DropTableTask('test'));
64+
}
65+
66+
public function testHandleDropIndex(): void
67+
{
68+
$schemaManager = $this->createMock(AbstractSchemaManager::class);
69+
$schemaManager->expects($this->once())->method('dropIndex')->with('foo', 'bar');
70+
71+
$connection = $this->createMock(Connection::class);
72+
$connection
73+
->expects($this->once())
74+
->method('createSchemaManager')
75+
->willReturn($schemaManager);
76+
77+
$handler = new DbalCleanupTaskHandler($connection);
78+
79+
$handler(new DropIndexTask('foo', 'bar'));
80+
}
81+
}

tests/Unit/Subscription/Cleanup/DefaultCleanerTest.php

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

55
namespace Patchlevel\EventSourcing\Tests\Unit\Subscription\Cleanup;
66

7-
use Patchlevel\EventSourcing\Subscription\Cleanup\CleanupHandler;
7+
use Patchlevel\EventSourcing\Subscription\Cleanup\CleanupTaskHandler;
88
use Patchlevel\EventSourcing\Subscription\Cleanup\Dbal\DropTableTask;
99
use Patchlevel\EventSourcing\Subscription\Cleanup\DefaultCleaner;
1010
use Patchlevel\EventSourcing\Subscription\Cleanup\NoHandlerForCleanupTask;
@@ -18,7 +18,7 @@ final class DefaultCleanerTest extends TestCase
1818
{
1919
public function testClean(): void
2020
{
21-
$handler = new class implements CleanupHandler {
21+
$handler = new class implements CleanupTaskHandler {
2222
public bool $called = false;
2323

2424
public function __invoke(object $task): void
@@ -42,7 +42,7 @@ public function supports(object $task): bool
4242

4343
public function testCleanupFailed(): void
4444
{
45-
$handler = new class implements CleanupHandler {
45+
$handler = new class implements CleanupTaskHandler {
4646
public function __invoke(object $task): void
4747
{
4848
throw new RuntimeException('Failed to cleanup');
@@ -64,7 +64,7 @@ public function supports(object $task): bool
6464

6565
public function testNoTasks(): void
6666
{
67-
$handler = new class implements CleanupHandler {
67+
$handler = new class implements CleanupTaskHandler {
6868
public bool $called = false;
6969

7070
public function __invoke(object $task): void

0 commit comments

Comments
 (0)