Skip to content

Commit 88b22a1

Browse files
committed
somehow working tests - not yet fully
1 parent e064a5d commit 88b22a1

File tree

3 files changed

+85
-19
lines changed

3 files changed

+85
-19
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"phpstan/phpstan": "^1.10",
1616
"squizlabs/php_codesniffer": "^4.0.x-dev",
1717
"phpunit/phpunit": "^10",
18-
"brianium/paratest": "^7.2"
18+
"brianium/paratest": "^7.2",
19+
"illuminate/support": "^11"
1920
},
2021
"autoload": {
2122
"psr-4": {
@@ -34,9 +35,9 @@
3435
"test:cs:fix": "phpcbf --colors src",
3536
"test:integration": "phpunit tests/Integration --exclude-group=parallel",
3637
"test:consistency": [
37-
"Sandstorm\\EventStore\\LaravelAdapter\\Tests\\Integration\\DoctrineEventStoreTest::consistency_prepare",
38+
"Sandstorm\\EventStore\\LaravelAdapter\\Tests\\Integration\\LaravelEventStoreTest::consistency_prepare",
3839
"paratest tests/Integration --group=parallel --functional --processes 10",
39-
"Sandstorm\\EventStore\\LaravelAdapter\\Tests\\Integration\\DoctrineEventStoreTest::consistency_validateEvents"
40+
"Sandstorm\\EventStore\\LaravelAdapter\\Tests\\Integration\\LaravelEventStoreTest::consistency_validateEvents"
4041
],
4142
"test": [
4243
"@test:phpstan",

src/LaravelEventStore.php

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,40 +179,77 @@ public function status(): Status
179179

180180
/** @var SchemaBuilder $schema */
181181
$schema = $this->connection->getSchemaBuilder();
182-
if (!$schema->hasTable($this->eventTableName)) {
182+
if (! $schema->hasTable($this->eventTableName)) {
183183
return Status::setupRequired(
184184
sprintf('Table `%s` does not exist.', $this->eventTableName)
185185
);
186186
}
187187

188+
// detect schema drift: make sure all the columns we created in setup() are still here
189+
$actual = $schema->getColumnListing($this->eventTableName);
190+
$expected = [
191+
'sequencenumber',
192+
'stream',
193+
'version',
194+
'type',
195+
'payload',
196+
'metadata',
197+
'id',
198+
'causationid',
199+
'correlationid',
200+
'recordedat',
201+
];
202+
203+
if (count(array_diff($expected, $actual)) > 0) {
204+
return Status::setupRequired(
205+
sprintf('Table `%s` requires schema update.', $this->eventTableName)
206+
);
207+
}
208+
188209
return Status::ok();
189210
}
190211

212+
191213
public function setup(): void
192214
{
193215
/** @var SchemaBuilder $schema */
194216
$schema = $this->connection->getSchemaBuilder();
195-
if (!$schema->hasTable($this->eventTableName)) {
217+
218+
if (! $schema->hasTable($this->eventTableName)) {
196219
$schema->create($this->eventTableName, function (Blueprint $table) {
197-
$table->unsignedBigInteger('sequencenumber', true);
198-
$table->string('stream', StreamName::MAX_LENGTH)->charset('ascii');
220+
// == PRIMARY KEY ==
221+
// bigIncrements on MySQL => BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
222+
// on SQLite Laravel will collapse it to INTEGER PRIMARY KEY AUTOINCREMENT
223+
$table->bigIncrements('sequencenumber');
224+
225+
// == COLUMNS ==
226+
$table->string('stream', StreamName::MAX_LENGTH)
227+
->charset('ascii');
199228
$table->unsignedBigInteger('version');
200-
$table->string('type', Event\EventType::MAX_LENGTH)->charset('ascii');
229+
$table->string('type', Event\EventType::MAX_LENGTH)
230+
->charset('ascii');
201231
$table->text('payload');
202232
$table->json('metadata')->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();
206-
$table->timestamp('recordedat');
233+
$table->char('id', Event\EventId::MAX_LENGTH)
234+
->charset('ascii')
235+
->unique();
236+
$table->char('causationid', Event\CausationId::MAX_LENGTH)
237+
->charset('ascii')
238+
->nullable();
239+
$table->char('correlationid', Event\CorrelationId::MAX_LENGTH)
240+
->charset('ascii')
241+
->nullable();
242+
// Doctrine used DATETIME, so use dateTime() here (not timestamp)
243+
$table->dateTime('recordedat');
207244

208-
$table->primary('sequencenumber');
209-
$table->unique('id');
245+
// == INDEXES ==
210246
$table->unique(['stream', 'version']);
211247
$table->index('correlationid');
212248
});
213249
}
214250
}
215251

252+
216253
private function getStreamVersion(StreamName $streamName): MaybeVersion
217254
{
218255
$version = $this->connection

tests/Integration/LaravelEventStoreTest.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace Sandstorm\EventStore\LaravelAdapter\Tests\Integration;
55

6+
use Illuminate\Config\Repository;
7+
use Illuminate\Container\Container;
68
use Illuminate\Database\Capsule\Manager as Capsule;
79
use Illuminate\Database\Connection;
810
use Illuminate\Database\Schema\Blueprint;
@@ -13,6 +15,26 @@
1315
use PHPUnit\Framework\Attributes\CoversClass;
1416
use Sandstorm\EventStore\LaravelAdapter\LaravelEventStore;
1517

18+
require_once __DIR__ . '/../../vendor/autoload.php';
19+
20+
class TestApplication extends Container
21+
{
22+
public function basePath(string $path = ''): string
23+
{
24+
// point this at your package root (or test root) so that SQLite can write its files
25+
$root = __DIR__ . '/..';
26+
return $path ? $root . DIRECTORY_SEPARATOR . $path : $root;
27+
}
28+
29+
public function databasePath(string $path = ''): string
30+
{
31+
// if you want SQLite files under "database/"
32+
$db = $this->basePath('database');
33+
return $path ? $db . DIRECTORY_SEPARATOR . $path : $db;
34+
}
35+
}
36+
37+
1638
#[CoversClass(LaravelEventStore::class)]
1739
final class LaravelEventStoreTest extends AbstractEventStoreTestBase
1840
{
@@ -25,19 +47,25 @@ protected static function createEventStore(): EventStoreInterface
2547

2648
protected static function resetEventStore(): void
2749
{
28-
$schema = self::connection()->getSchemaBuilder();
29-
if ($schema->hasTable(self::eventTableName())) {
30-
$schema->drop(self::eventTableName());
31-
}
50+
unlink(__DIR__ . '/../events_test.sqlite');
3251
}
3352

3453
public static function connection(): Connection
3554
{
3655
if (self::$capsule === null) {
56+
57+
// 1) replace the global container
58+
Container::setInstance(new TestApplication());
59+
60+
$path = __DIR__ . '/../events_test.sqlite';
61+
if (!file_exists($path)) {
62+
touch($path);
63+
}
64+
3765
$capsule = new Capsule();
3866
$capsule->addConnection([
3967
'driver' => 'sqlite',
40-
'database' => __DIR__ . '/../events_test.sqlite',
68+
'database' => $path,
4169
'prefix' => '',
4270
], 'default');
4371
$capsule->setAsGlobal();

0 commit comments

Comments
 (0)