1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\Sprout\Test\Unit\Parser; |
4
|
|
|
|
5
|
|
|
use Graze\Sprout\Config; |
6
|
|
|
use Graze\Sprout\Parser\ParsedSchema; |
7
|
|
|
use Graze\Sprout\Parser\SchemaParser; |
8
|
|
|
use Graze\Sprout\Parser\TablePopulator; |
9
|
|
|
use Graze\Sprout\Test\TestCase; |
10
|
|
|
use Mockery; |
11
|
|
|
|
12
|
|
|
class SchemaParserTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** @var mixed */ |
15
|
|
|
private $tablePopulator; |
16
|
|
|
/** @var mixed */ |
17
|
|
|
private $config; |
18
|
|
|
/** @var SchemaParser */ |
19
|
|
|
private $schemaParser; |
20
|
|
|
|
21
|
|
|
public function setUp() |
22
|
|
|
{ |
23
|
|
|
$this->tablePopulator = Mockery::mock(TablePopulator::class); |
24
|
|
|
$this->config = Mockery::mock(Config::class); |
25
|
|
|
$this->schemaParser = new SchemaParser( |
26
|
|
|
$this->tablePopulator, |
|
|
|
|
27
|
|
|
$this->config, |
|
|
|
|
28
|
|
|
'group' |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testExtractSchemasWithSchemasAndTablesReturnsTheInput() |
33
|
|
|
{ |
34
|
|
|
$schema1 = Mockery::mock(Config\SchemaConfigInterface::class); |
35
|
|
|
$schema2 = Mockery::mock(Config\SchemaConfigInterface::class); |
36
|
|
|
$this->config->allows() |
37
|
|
|
->getSchemaConfiguration('schema1') |
38
|
|
|
->andReturns($schema1); |
39
|
|
|
$this->config->allows() |
40
|
|
|
->getSchemaPath($schema1, 'group') |
41
|
|
|
->andReturns('/a/path/to/group/schema1'); |
42
|
|
|
$schema1->allows(['getSchema' => 'schema1']); |
43
|
|
|
|
44
|
|
|
$this->config->allows() |
45
|
|
|
->getSchemaConfiguration('schema2') |
46
|
|
|
->andReturns($schema2); |
47
|
|
|
$this->config->allows() |
48
|
|
|
->getSchemaPath($schema2, 'group') |
49
|
|
|
->andReturns('/a/path/to/group/schema2'); |
50
|
|
|
$schema2->allows(['getSchema' => 'schema2']); |
51
|
|
|
|
52
|
|
|
$parsedSchemas = []; |
53
|
|
|
$this->tablePopulator->allows() |
54
|
|
|
->populateTables(Mockery::on(function (ParsedSchema $schema) use (&$parsedSchemas) { |
55
|
|
|
$parsedSchemas[] = $schema; |
56
|
|
|
return true; |
57
|
|
|
})) |
58
|
|
|
->andReturnUsing(function () use (&$parsedSchemas) { |
59
|
|
|
return end($parsedSchemas); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
$output = $this->schemaParser->extractSchemas(['schema1:table1,table2', 'schema2:table1']); |
63
|
|
|
|
64
|
|
|
$this->assertCount(2, $output); |
65
|
|
|
|
66
|
|
|
$first = $output[0]; |
67
|
|
|
|
68
|
|
|
$this->assertEquals('schema1', $first->getSchemaName()); |
69
|
|
|
$this->assertEquals(['table1', 'table2'], $first->getTables()); |
70
|
|
|
$this->assertEquals('/a/path/to/group/schema1', $first->getPath()); |
71
|
|
|
$this->assertEquals($schema1, $first->getSchemaConfig()); |
72
|
|
|
|
73
|
|
|
$second = $output[1]; |
74
|
|
|
|
75
|
|
|
$this->assertEquals('schema2', $second->getSchemaName()); |
76
|
|
|
$this->assertEquals(['table1'], $second->getTables()); |
77
|
|
|
$this->assertEquals('/a/path/to/group/schema2', $second->getPath()); |
78
|
|
|
$this->assertEquals($schema2, $second->getSchemaConfig()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testExtractSchemasWithSchemasOnlyHasEmptyTables() |
82
|
|
|
{ |
83
|
|
|
$schema1 = Mockery::mock(Config\SchemaConfigInterface::class); |
84
|
|
|
$schema2 = Mockery::mock(Config\SchemaConfigInterface::class); |
85
|
|
|
$this->config->allows() |
86
|
|
|
->getSchemaConfiguration('schema1') |
87
|
|
|
->andReturns($schema1); |
88
|
|
|
$this->config->allows() |
89
|
|
|
->getSchemaPath($schema1, 'group') |
90
|
|
|
->andReturns('/a/path/to/group/schema1'); |
91
|
|
|
$schema1->allows(['getSchema' => 'schema1']); |
92
|
|
|
|
93
|
|
|
$this->config->allows() |
94
|
|
|
->getSchemaConfiguration('schema2') |
95
|
|
|
->andReturns($schema2); |
96
|
|
|
$this->config->allows() |
97
|
|
|
->getSchemaPath($schema2, 'group') |
98
|
|
|
->andReturns('/a/path/to/group/schema2'); |
99
|
|
|
$schema2->allows(['getSchema' => 'schema2']); |
100
|
|
|
|
101
|
|
|
$parsedSchemas = []; |
102
|
|
|
$this->tablePopulator->allows() |
103
|
|
|
->populateTables(Mockery::on(function (ParsedSchema $schema) use (&$parsedSchemas) { |
104
|
|
|
$parsedSchemas[] = $schema; |
105
|
|
|
return true; |
106
|
|
|
})) |
107
|
|
|
->andReturnUsing(function () use (&$parsedSchemas) { |
108
|
|
|
return end($parsedSchemas); |
109
|
|
|
}); |
110
|
|
|
|
111
|
|
|
$output = $this->schemaParser->extractSchemas(['schema1', 'schema2']); |
112
|
|
|
|
113
|
|
|
$this->assertCount(2, $output); |
114
|
|
|
|
115
|
|
|
$first = $output[0]; |
116
|
|
|
|
117
|
|
|
$this->assertEquals('schema1', $first->getSchemaName()); |
118
|
|
|
$this->assertEquals([], $first->getTables()); |
119
|
|
|
$this->assertEquals('/a/path/to/group/schema1', $first->getPath()); |
120
|
|
|
$this->assertEquals($schema1, $first->getSchemaConfig()); |
121
|
|
|
|
122
|
|
|
$second = $output[1]; |
123
|
|
|
|
124
|
|
|
$this->assertEquals('schema2', $second->getSchemaName()); |
125
|
|
|
$this->assertEquals([], $second->getTables()); |
126
|
|
|
$this->assertEquals('/a/path/to/group/schema2', $second->getPath()); |
127
|
|
|
$this->assertEquals($schema2, $second->getSchemaConfig()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testExtractSchemasWithMixedSchemasHasEmptyTables() |
131
|
|
|
{ |
132
|
|
|
$schema1 = Mockery::mock(Config\SchemaConfigInterface::class); |
133
|
|
|
$schema2 = Mockery::mock(Config\SchemaConfigInterface::class); |
134
|
|
|
$this->config->allows() |
135
|
|
|
->getSchemaConfiguration('schema1') |
136
|
|
|
->andReturns($schema1); |
137
|
|
|
$this->config->allows() |
138
|
|
|
->getSchemaPath($schema1, 'group') |
139
|
|
|
->andReturns('/a/path/to/group/schema1'); |
140
|
|
|
$schema1->allows(['getSchema' => 'schema1']); |
141
|
|
|
|
142
|
|
|
$this->config->allows() |
143
|
|
|
->getSchemaConfiguration('schema2') |
144
|
|
|
->andReturns($schema2); |
145
|
|
|
$this->config->allows() |
146
|
|
|
->getSchemaPath($schema2, 'group') |
147
|
|
|
->andReturns('/a/path/to/group/schema2'); |
148
|
|
|
$schema2->allows(['getSchema' => 'schema2']); |
149
|
|
|
|
150
|
|
|
$parsedSchemas = []; |
151
|
|
|
$this->tablePopulator->allows() |
152
|
|
|
->populateTables(Mockery::on(function (ParsedSchema $schema) use (&$parsedSchemas) { |
153
|
|
|
$parsedSchemas[] = $schema; |
154
|
|
|
return true; |
155
|
|
|
})) |
156
|
|
|
->andReturnUsing(function () use (&$parsedSchemas) { |
157
|
|
|
return end($parsedSchemas); |
158
|
|
|
}); |
159
|
|
|
|
160
|
|
|
$output = $this->schemaParser->extractSchemas(['schema1:table1', 'schema2']); |
161
|
|
|
|
162
|
|
|
$this->assertCount(2, $output); |
163
|
|
|
|
164
|
|
|
$first = $output[0]; |
165
|
|
|
|
166
|
|
|
$this->assertEquals('schema1', $first->getSchemaName()); |
167
|
|
|
$this->assertEquals(['table1'], $first->getTables()); |
168
|
|
|
$this->assertEquals('/a/path/to/group/schema1', $first->getPath()); |
169
|
|
|
$this->assertEquals($schema1, $first->getSchemaConfig()); |
170
|
|
|
|
171
|
|
|
$second = $output[1]; |
172
|
|
|
|
173
|
|
|
$this->assertEquals('schema2', $second->getSchemaName()); |
174
|
|
|
$this->assertEquals([], $second->getTables()); |
175
|
|
|
$this->assertEquals('/a/path/to/group/schema2', $second->getPath()); |
176
|
|
|
$this->assertEquals($schema2, $second->getSchemaConfig()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testExtractSchemasWithNoSchemasLooksForAllSchemas() |
180
|
|
|
{ |
181
|
|
|
$schema1 = Mockery::mock(Config\SchemaConfigInterface::class); |
182
|
|
|
$schema2 = Mockery::mock(Config\SchemaConfigInterface::class); |
183
|
|
|
|
184
|
|
|
$this->config->allows() |
185
|
|
|
->get('schemas') |
186
|
|
|
->andReturns([$schema1, $schema2]); |
187
|
|
|
|
188
|
|
|
$this->config->allows() |
189
|
|
|
->getSchemaConfiguration('schema1') |
190
|
|
|
->andReturns($schema1); |
191
|
|
|
$this->config->allows() |
192
|
|
|
->getSchemaPath($schema1, 'group') |
193
|
|
|
->andReturns('/a/path/to/group/schema1'); |
194
|
|
|
$schema1->allows(['getSchema' => 'schema1']); |
195
|
|
|
|
196
|
|
|
$this->config->allows() |
197
|
|
|
->getSchemaConfiguration('schema2') |
198
|
|
|
->andReturns($schema2); |
199
|
|
|
$this->config->allows() |
200
|
|
|
->getSchemaPath($schema2, 'group') |
201
|
|
|
->andReturns('/a/path/to/group/schema2'); |
202
|
|
|
$schema2->allows(['getSchema' => 'schema2']); |
203
|
|
|
|
204
|
|
|
$parsedSchemas = []; |
205
|
|
|
$this->tablePopulator->allows() |
206
|
|
|
->populateTables(Mockery::on(function (ParsedSchema $schema) use (&$parsedSchemas) { |
207
|
|
|
$parsedSchemas[] = $schema; |
208
|
|
|
return true; |
209
|
|
|
})) |
210
|
|
|
->andReturnUsing(function () use (&$parsedSchemas) { |
211
|
|
|
return end($parsedSchemas); |
212
|
|
|
}); |
213
|
|
|
|
214
|
|
|
$output = $this->schemaParser->extractSchemas([]); |
215
|
|
|
|
216
|
|
|
$this->assertCount(2, $output); |
217
|
|
|
|
218
|
|
|
$first = $output[0]; |
219
|
|
|
|
220
|
|
|
$this->assertEquals('schema1', $first->getSchemaName()); |
221
|
|
|
$this->assertEquals([], $first->getTables()); |
222
|
|
|
$this->assertEquals('/a/path/to/group/schema1', $first->getPath()); |
223
|
|
|
$this->assertEquals($schema1, $first->getSchemaConfig()); |
224
|
|
|
|
225
|
|
|
$second = $output[1]; |
226
|
|
|
|
227
|
|
|
$this->assertEquals('schema2', $second->getSchemaName()); |
228
|
|
|
$this->assertEquals([], $second->getTables()); |
229
|
|
|
$this->assertEquals('/a/path/to/group/schema2', $second->getPath()); |
230
|
|
|
$this->assertEquals($schema2, $second->getSchemaConfig()); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|