Passed
Push — master ( 86c65e...d5128a )
by Harry
02:41
created

SchemaParserTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 153
dl 0
loc 219
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testExtractSchemasWithSchemasAndTablesReturnsTheInput() 0 47 1
A testExtractSchemasWithSchemasOnlyHasEmptyTables() 0 47 1
A testExtractSchemasWithMixedSchemasHasEmptyTables() 0 47 1
A setUp() 0 8 1
A testExtractSchemasWithNoSchemasLooksForAllSchemas() 0 52 1
1
<?php
2
3
namespace Graze\Sprout\Test\Unit\Parser;
4
5
use Graze\Sprout\Config\Config;
6
use Graze\Sprout\Config\SchemaConfigInterface;
7
use Graze\Sprout\Parser\ParsedSchema;
8
use Graze\Sprout\Parser\SchemaParser;
9
use Graze\Sprout\Parser\TablePopulator;
10
use Graze\Sprout\Test\TestCase;
11
use Mockery;
12
13
class SchemaParserTest extends TestCase
14
{
15
    /** @var mixed */
16
    private $tablePopulator;
17
    /** @var mixed */
18
    private $config;
19
    /** @var SchemaParser */
20
    private $schemaParser;
21
22
    public function setUp()
23
    {
24
        $this->tablePopulator = Mockery::mock(TablePopulator::class);
25
        $this->config = Mockery::mock(Config::class);
26
        $this->schemaParser = new SchemaParser(
27
            $this->tablePopulator,
0 ignored issues
show
Bug introduced by
$this->tablePopulator of type Mockery\MockInterface is incompatible with the type Graze\Sprout\Parser\TablePopulator expected by parameter $populator of Graze\Sprout\Parser\SchemaParser::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
            /** @scrutinizer ignore-type */ $this->tablePopulator,
Loading history...
28
            $this->config,
0 ignored issues
show
Bug introduced by
$this->config of type Mockery\MockInterface is incompatible with the type Graze\Sprout\Config\Config expected by parameter $config of Graze\Sprout\Parser\SchemaParser::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

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