1
1
<?php
2
2
declare (strict_types=1 );
3
+
3
4
namespace Sandstorm \EventStore \LaravelAdapter \Tests \Integration ;
4
5
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 ;
11
10
use Neos \EventStore \EventStoreInterface ;
12
11
use Neos \EventStore \Model \EventStore \StatusType ;
13
12
use Neos \EventStore \Tests \Integration \AbstractEventStoreTestBase ;
14
13
use PHPUnit \Framework \Attributes \CoversClass ;
14
+ use Sandstorm \EventStore \LaravelAdapter \LaravelEventStore ;
15
15
16
16
#[CoversClass(LaravelEventStore::class)]
17
17
final class LaravelEventStoreTest extends AbstractEventStoreTestBase
18
18
{
19
- private static ?Connection $ connection = null ;
19
+ private static ?Capsule $ capsule = null ;
20
20
21
21
protected static function createEventStore (): EventStoreInterface
22
22
{
@@ -25,30 +25,26 @@ protected static function createEventStore(): EventStoreInterface
25
25
26
26
protected static function resetEventStore (): void
27
27
{
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 ());
39
31
}
40
32
}
41
33
42
34
public static function connection (): Connection
43
35
{
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 ;
50
46
}
51
- return self ::$ connection ;
47
+ return self ::$ capsule -> getConnection () ;
52
48
}
53
49
54
50
public static function eventTableName (): string
@@ -58,41 +54,67 @@ public static function eventTableName(): string
58
54
59
55
public function test_setup_throws_exception_if_database_connection_fails (): void
60
56
{
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 ' );
63
68
64
- $ this ->expectException (DbalException::class);
69
+ $ eventStore = new LaravelEventStore ($ conn , self ::eventTableName ());
70
+ $ this ->expectException (QueryException::class);
65
71
$ eventStore ->setup ();
66
72
}
67
73
68
74
public function test_status_returns_error_status_if_database_connection_fails (): void
69
75
{
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 );
73
90
}
74
91
75
92
public function test_status_returns_setup_required_status_if_event_table_is_missing (): void
76
93
{
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 );
80
97
}
81
98
82
99
public function test_status_returns_setup_required_status_if_event_table_requires_update (): void
83
100
{
84
- $ connection = self ::connection ();
85
- $ eventStore = new LaravelEventStore ($ connection , self ::eventTableName ());
101
+ $ conn = self ::connection ();
102
+ $ eventStore = new LaravelEventStore ($ conn , self ::eventTableName ());
86
103
$ 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 );
89
111
}
90
112
91
113
public function test_status_returns_ok_status_if_event_table_is_up_to_date (): void
92
114
{
93
- $ connection = self ::connection ();
94
- $ eventStore = new LaravelEventStore ($ connection , self ::eventTableName ());
115
+ $ conn = self ::connection ();
116
+ $ eventStore = new LaravelEventStore ($ conn , self ::eventTableName ());
95
117
$ eventStore ->setup ();
96
- self :: assertSame ($ eventStore ->status ()->type , StatusType:: OK );
118
+ $ this -> assertSame (StatusType:: OK , $ eventStore ->status ()->type );
97
119
}
98
120
}
0 commit comments