|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Avoran\RapidoBundle; |
|
4
|
|
|
|
|
5
|
|
|
use Avoran\Rapido\ReadModel\DataType\Boolean; |
|
6
|
|
|
use Avoran\Rapido\ReadModel\DataType\Integer; |
|
7
|
|
|
use Avoran\Rapido\ReadModel\DataType\TextString; |
|
8
|
|
|
use Avoran\Rapido\ReadModel\ReadModelConfiguration; |
|
9
|
|
|
use Avoran\Rapido\ReadModel\ReadModelField; |
|
10
|
|
|
use Avoran\Rapido\ReadModel\ReadModelId; |
|
11
|
|
|
use Avoran\Rapido\ReadModel\Record; |
|
12
|
|
|
use Avoran\Rapido\ReadModel\StorageWriter; |
|
13
|
|
|
use Avoran\RapidoAdapter\DoctrineDbalStorage\SchemaSynchronizer; |
|
14
|
|
|
use Doctrine\DBAL\Connection; |
|
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
|
16
|
|
|
|
|
17
|
|
|
class StorageWriterTest extends KernelTestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var StorageWriter */ |
|
20
|
|
|
private $writer; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Connection */ |
|
23
|
|
|
private $connection; |
|
24
|
|
|
|
|
25
|
|
|
/** @var SchemaSynchronizer */ |
|
26
|
|
|
private $synchronizer; |
|
27
|
|
|
|
|
28
|
|
|
public static function setUpBeforeClass() |
|
29
|
|
|
{ |
|
30
|
|
|
unlink(__DIR__ . '/Config/sqlite.db'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function setUp() |
|
34
|
|
|
{ |
|
35
|
|
|
$container = self::bootKernel()->getContainer(); |
|
36
|
|
|
$this->writer = $container->get('rapido.storage_writer'); |
|
37
|
|
|
$this->connection = $container->get('database_connection'); |
|
38
|
|
|
$this->synchronizer = $container->get('rapido_adapter.doctrine_dbal_storage.schema_synchronizer'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** @test */ |
|
42
|
|
|
public function it_should_create_a_table_on_first_insert() |
|
43
|
|
|
{ |
|
44
|
|
|
$meta = new ReadModelConfiguration( |
|
45
|
|
|
'test', |
|
46
|
|
|
new ReadModelId(new Integer()), |
|
47
|
|
|
[ |
|
48
|
|
|
new ReadModelField('f1', new Boolean()), |
|
49
|
|
|
new ReadModelField('f2', new TextString(10)), |
|
50
|
|
|
], |
|
51
|
|
|
function ($data) { return new Record($data['id'], ['f1' => $data['f1'], 'f2' => $data['f2']]); }, |
|
52
|
|
|
function () {} |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
$this->synchronizer->ensureTableExists($meta); |
|
56
|
|
|
$this->writer->writeRecord($meta, ['id' => 1, 'f1' => true, 'f2' => 'test']); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertCount(1, $this->connection->createQueryBuilder()->select('*')->from('prefix_test')->execute()->fetchAllAssociative()); |
|
59
|
|
|
$this->assertCount(3, $this->connection->createQueryBuilder()->select('*')->from('prefix_test')->execute()->fetchAssociative()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** @test */ |
|
63
|
|
|
public function it_should_create_an_indexed_table() |
|
64
|
|
|
{ |
|
65
|
|
|
$meta = new ReadModelConfiguration( |
|
66
|
|
|
'test', |
|
67
|
|
|
new ReadModelId(new Integer()), |
|
68
|
|
|
[ |
|
69
|
|
|
new ReadModelField('f1', new TextString(10), true), |
|
70
|
|
|
], |
|
71
|
|
|
function ($data) { return new Record($data['id'], ['f1' => $data['f1']]); }, |
|
72
|
|
|
function () {} |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$this->synchronizer->ensureTableExists($meta); |
|
76
|
|
|
$this->writer->writeRecord($meta, ['id' => 1, 'f1' => 'test']); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertCount(1, $this->connection->createQueryBuilder()->select('*')->from('sqlite_master')->where("type = 'index' and name = 'IDX_f1'")->execute()->fetchAllAssociative()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** @test */ |
|
82
|
|
|
public function it_should_update_the_table_on_record_change() |
|
83
|
|
|
{ |
|
84
|
|
|
$meta = new ReadModelConfiguration( |
|
85
|
|
|
'test', |
|
86
|
|
|
new ReadModelId(new Integer()), |
|
87
|
|
|
[ |
|
88
|
|
|
new ReadModelField('f1', new Boolean()), |
|
89
|
|
|
new ReadModelField('f2', new TextString(10)), |
|
90
|
|
|
new ReadModelField('f3', new TextString(10)), |
|
91
|
|
|
], |
|
92
|
|
|
function ($data) { return new Record($data['id'], ['f1' => $data['f1'], 'f2' => $data['f2'], 'f3' => $data['f2']]); }, |
|
93
|
|
|
function () {} |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
$this->synchronizer->ensureTableExists($meta); |
|
97
|
|
|
$this->writer->writeRecord($meta, ['id' => 2, 'f1' => true, 'f2' => 'test']); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertEquals('test', $this->connection->createQueryBuilder()->select('f3')->from('prefix_test')->where('identifier = 2')->execute()->fetchOne(0)); |
|
100
|
|
|
$this->assertCount(4, $this->connection->createQueryBuilder()->select('*')->from('prefix_test')->execute()->fetchAssociative()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** @test */ |
|
104
|
|
|
public function it_should_update_a_record() |
|
105
|
|
|
{ |
|
106
|
|
|
$meta = new ReadModelConfiguration( |
|
107
|
|
|
'test', |
|
108
|
|
|
new ReadModelId(new Integer()), |
|
109
|
|
|
[ |
|
110
|
|
|
new ReadModelField('f1', new Boolean()), |
|
111
|
|
|
new ReadModelField('f2', new TextString(10)) |
|
112
|
|
|
], |
|
113
|
|
|
function ($data) { return new Record($data['id'], ['f1' => $data['f1'], 'f2' => $data['f2']]); }, |
|
114
|
|
|
function () {} |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
$this->synchronizer->ensureTableExists($meta); |
|
118
|
|
|
$this->writer->writeRecord($meta, ['id' => 2, 'f1' => true, 'f2' => 'test2']); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertEquals('test2', $this->connection->createQueryBuilder()->select('f2')->from('prefix_test')->where('identifier = 2')->execute()->fetchOne(0)); |
|
121
|
|
|
$this->assertCount(3, $this->connection->createQueryBuilder()->select('*')->from('prefix_test')->execute()->fetchAssociative()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** @test */ |
|
125
|
|
|
public function it_should_create_a_snapshot_table() |
|
126
|
|
|
{ |
|
127
|
|
|
$meta = new ReadModelConfiguration( |
|
128
|
|
|
'test', |
|
129
|
|
|
new ReadModelId(new Integer()), |
|
130
|
|
|
[ |
|
131
|
|
|
new ReadModelField('f1', new Boolean()), |
|
132
|
|
|
new ReadModelField('f2', new TextString(10)), |
|
133
|
|
|
], |
|
134
|
|
|
function ($data) { return new Record($data['id'], ['f1' => $data['f1'], 'f2' => $data['f2']]); }, |
|
135
|
|
|
function () {}, |
|
136
|
|
|
'created_at' |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
$this->synchronizer->ensureTableExists($meta); |
|
140
|
|
|
$this->writer->writeRecord($meta, ['id' => 1, 'f1' => true, 'f2' => 'test']); |
|
141
|
|
|
|
|
142
|
|
|
$this->assertEquals(1, $this->connection->fetchOne("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='prefix_test_suffix'")); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|