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