Skip to content

Commit e064a5d

Browse files
committed
phpstan green
1 parent 2d37ca7 commit e064a5d

File tree

4 files changed

+73
-52
lines changed

4 files changed

+73
-52
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor
22
/composer.lock
3+
/.phpunit.cache

src/LaravelEventStore.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
declare(strict_types=1);
3-
4-
namespace Neos\EventStore\DoctrineAdapter;
3+
namespace Sandstorm\EventStore\LaravelAdapter;
54

65
use DateTimeImmutable;
76
use Illuminate\Database\Connection as DbConnection;
@@ -26,7 +25,6 @@
2625
use Psr\Clock\ClockInterface;
2726
use RuntimeException;
2827
use Illuminate\Database\QueryException;
29-
use Sandstorm\EventStore\EloquentAdapter\LaravelEventStream as EloquentStream;
3028

3129
final class LaravelEventStore implements EventStoreInterface
3230
{
@@ -36,8 +34,7 @@ public function __construct(
3634
private readonly DbConnection $connection,
3735
private readonly string $eventTableName,
3836
?ClockInterface $clock = null
39-
)
40-
{
37+
) {
4138
$this->clock = $clock ?? new class implements ClockInterface {
4239
public function now(): DateTimeImmutable
4340
{
@@ -69,7 +66,7 @@ public function load(VirtualStreamName|StreamName $streamName, ?EventStreamFilte
6966
}
7067

7168
return BatchEventStream::create(
72-
EloquentStream::create($query),
69+
LaravelEventStream::create($query),
7370
100
7471
);
7572
}
@@ -200,12 +197,12 @@ public function setup(): void
200197
$table->unsignedBigInteger('sequencenumber', true);
201198
$table->string('stream', StreamName::MAX_LENGTH)->charset('ascii');
202199
$table->unsignedBigInteger('version');
203-
$table->string('type', EventType::MAX_LENGTH)->charset('ascii');
200+
$table->string('type', Event\EventType::MAX_LENGTH)->charset('ascii');
204201
$table->text('payload');
205202
$table->json('metadata')->nullable();
206-
$table->char('id', EventId::MAX_LENGTH);
207-
$table->char('causationid', CausationId::MAX_LENGTH)->nullable();
208-
$table->char('correlationid', CorrelationId::MAX_LENGTH)->nullable();
203+
$table->char('id', Event\EventId::MAX_LENGTH);
204+
$table->char('causationid', Event\CausationId::MAX_LENGTH)->nullable();
205+
$table->char('correlationid', Event\CorrelationId::MAX_LENGTH)->nullable();
209206
$table->timestamp('recordedat');
210207

211208
$table->primary('sequencenumber');

src/LaravelEventStream.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
declare(strict_types=1);
3-
namespace Sandstorm\EventStore\EloquentAdapter;
3+
namespace Sandstorm\EventStore\LaravelAdapter;
44

55
use Illuminate\Database\Query\Builder;
66
use Neos\EventStore\Model\Event;
@@ -116,6 +116,7 @@ private function reconnectDatabaseConnection(): void
116116
->selectOne('SELECT 1');
117117
} catch (\Throwable $_) {
118118
$conn = $this->queryBuilder->getConnection();
119+
assert($conn instanceof \Illuminate\Database\Connection);
119120
$conn->disconnect();
120121
$conn->reconnect();
121122
}
Lines changed: 63 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<?php
22
declare(strict_types=1);
3+
34
namespace Sandstorm\EventStore\LaravelAdapter\Tests\Integration;
45

5-
use Doctrine\DBAL\Connection;
6-
use Doctrine\DBAL\DriverManager;
7-
use Doctrine\DBAL\Exception as DbalException;
8-
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
9-
use Doctrine\DBAL\Platforms\SqlitePlatform;
10-
use Neos\EventStore\DoctrineAdapter\LaravelEventStore;
6+
use Illuminate\Database\Capsule\Manager as Capsule;
7+
use Illuminate\Database\Connection;
8+
use Illuminate\Database\Schema\Blueprint;
9+
use Illuminate\Database\QueryException;
1110
use Neos\EventStore\EventStoreInterface;
1211
use Neos\EventStore\Model\EventStore\StatusType;
1312
use Neos\EventStore\Tests\Integration\AbstractEventStoreTestBase;
1413
use PHPUnit\Framework\Attributes\CoversClass;
14+
use Sandstorm\EventStore\LaravelAdapter\LaravelEventStore;
1515

1616
#[CoversClass(LaravelEventStore::class)]
1717
final class LaravelEventStoreTest extends AbstractEventStoreTestBase
1818
{
19-
private static ?Connection $connection = null;
19+
private static ?Capsule $capsule = null;
2020

2121
protected static function createEventStore(): EventStoreInterface
2222
{
@@ -25,30 +25,26 @@ protected static function createEventStore(): EventStoreInterface
2525

2626
protected static function resetEventStore(): void
2727
{
28-
$connection = self::connection();
29-
if (!$connection->getSchemaManager()->tablesExist([self::eventTableName()])) {
30-
return;
31-
}
32-
if ($connection->getDatabasePlatform() instanceof SqlitePlatform) {
33-
$connection->executeStatement('DELETE FROM ' . self::eventTableName());
34-
$connection->executeStatement('UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME="' . self::eventTableName() . '"');
35-
} elseif ($connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
36-
$connection->executeStatement('TRUNCATE TABLE ' . self::eventTableName() . ' RESTART IDENTITY');
37-
} else {
38-
$connection->executeStatement('TRUNCATE TABLE ' . self::eventTableName());
28+
$schema = self::connection()->getSchemaBuilder();
29+
if ($schema->hasTable(self::eventTableName())) {
30+
$schema->drop(self::eventTableName());
3931
}
4032
}
4133

4234
public static function connection(): Connection
4335
{
44-
if (self::$connection === null) {
45-
$dsn = getenv('DB_DSN');
46-
if (!is_string($dsn)) {
47-
$dsn = 'sqlite:///events_test.sqlite';
48-
}
49-
self::$connection = DriverManager::getConnection(['url' => $dsn]);
36+
if (self::$capsule === null) {
37+
$capsule = new Capsule();
38+
$capsule->addConnection([
39+
'driver' => 'sqlite',
40+
'database' => __DIR__ . '/../events_test.sqlite',
41+
'prefix' => '',
42+
], 'default');
43+
$capsule->setAsGlobal();
44+
$capsule->bootEloquent();
45+
self::$capsule = $capsule;
5046
}
51-
return self::$connection;
47+
return self::$capsule->getConnection();
5248
}
5349

5450
public static function eventTableName(): string
@@ -58,41 +54,67 @@ public static function eventTableName(): string
5854

5955
public function test_setup_throws_exception_if_database_connection_fails(): void
6056
{
61-
$connection = DriverManager::getConnection(['url' => 'mysql://invalid-connection']);
62-
$eventStore = new LaravelEventStore($connection, self::eventTableName());
57+
$bad = new Capsule();
58+
$bad->addConnection([
59+
'driver' => 'mysql',
60+
'host' => 'invalid-host',
61+
'database' => 'does_not_exist',
62+
'username' => 'foo',
63+
'password' => 'bar',
64+
], 'bad');
65+
$bad->setAsGlobal();
66+
$bad->bootEloquent();
67+
$conn = $bad->getConnection('bad');
6368

64-
$this->expectException(DbalException::class);
69+
$eventStore = new LaravelEventStore($conn, self::eventTableName());
70+
$this->expectException(QueryException::class);
6571
$eventStore->setup();
6672
}
6773

6874
public function test_status_returns_error_status_if_database_connection_fails(): void
6975
{
70-
$connection = DriverManager::getConnection(['url' => 'mysql://invalid-connection']);
71-
$eventStore = new LaravelEventStore($connection, self::eventTableName());
72-
self::assertSame($eventStore->status()->type, StatusType::ERROR);
76+
$bad = new Capsule();
77+
$bad->addConnection([
78+
'driver' => 'mysql',
79+
'host' => 'invalid-host',
80+
'database' => 'does_not_exist',
81+
'username' => 'foo',
82+
'password' => 'bar',
83+
], 'bad');
84+
$bad->setAsGlobal();
85+
$bad->bootEloquent();
86+
$conn = $bad->getConnection('bad');
87+
88+
$eventStore = new LaravelEventStore($conn, self::eventTableName());
89+
$this->assertSame(StatusType::ERROR, $eventStore->status()->type);
7390
}
7491

7592
public function test_status_returns_setup_required_status_if_event_table_is_missing(): void
7693
{
77-
$connection = DriverManager::getConnection(['url' => 'sqlite:///:memory:']);
78-
$eventStore = new LaravelEventStore($connection, self::eventTableName());
79-
self::assertSame($eventStore->status()->type, StatusType::SETUP_REQUIRED);
94+
$conn = self::connection();
95+
$eventStore = new LaravelEventStore($conn, self::eventTableName());
96+
$this->assertSame(StatusType::SETUP_REQUIRED, $eventStore->status()->type);
8097
}
8198

8299
public function test_status_returns_setup_required_status_if_event_table_requires_update(): void
83100
{
84-
$connection = self::connection();
85-
$eventStore = new LaravelEventStore($connection, self::eventTableName());
101+
$conn = self::connection();
102+
$eventStore = new LaravelEventStore($conn, self::eventTableName());
86103
$eventStore->setup();
87-
$connection->executeStatement('ALTER TABLE ' . self::eventTableName() . ' RENAME COLUMN metadata TO metadata_renamed');
88-
self::assertSame($eventStore->status()->type, StatusType::SETUP_REQUIRED);
104+
105+
// Rename one column to simulate a schema drift
106+
$conn->getSchemaBuilder()->table(self::eventTableName(), function (Blueprint $table) {
107+
$table->renameColumn('metadata', 'metadata_renamed');
108+
});
109+
110+
$this->assertSame(StatusType::SETUP_REQUIRED, $eventStore->status()->type);
89111
}
90112

91113
public function test_status_returns_ok_status_if_event_table_is_up_to_date(): void
92114
{
93-
$connection = self::connection();
94-
$eventStore = new LaravelEventStore($connection, self::eventTableName());
115+
$conn = self::connection();
116+
$eventStore = new LaravelEventStore($conn, self::eventTableName());
95117
$eventStore->setup();
96-
self::assertSame($eventStore->status()->type, StatusType::OK);
118+
$this->assertSame(StatusType::OK, $eventStore->status()->type);
97119
}
98120
}

0 commit comments

Comments
 (0)