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; |
17
|
|
|
use Graze\Sprout\Config\SchemaConfigInterface; |
18
|
|
|
use Graze\Sprout\Parser\FileTablePopulator; |
19
|
|
|
use Graze\Sprout\Parser\ParsedSchema; |
20
|
|
|
use Graze\Sprout\Parser\TableFilterer; |
21
|
|
|
use Graze\Sprout\Test\TestCase; |
22
|
|
|
use League\Flysystem\AdapterInterface; |
23
|
|
|
use Mockery; |
24
|
|
|
|
25
|
|
|
class FileTablePopulatorTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
/** @var FileTablePopulator */ |
28
|
|
|
private $tablePopulator; |
29
|
|
|
/** @var mixed */ |
30
|
|
|
private $fileSystem; |
31
|
|
|
/** @var mixed */ |
32
|
|
|
private $tableFilterer; |
33
|
|
|
|
34
|
|
|
public function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->fileSystem = Mockery::mock(AdapterInterface::class); |
37
|
|
|
$this->tableFilterer = Mockery::mock(TableFilterer::class); |
38
|
|
|
$this->tablePopulator = new FileTablePopulator($this->fileSystem, $this->tableFilterer); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testPopulateTablesWithExistingTablesDoesNothing() |
42
|
|
|
{ |
43
|
|
|
$config = Mockery::mock(SchemaConfigInterface::class); |
44
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', ['table1', 'table2']); |
|
|
|
|
45
|
|
|
$config->allows(['getExcludes' => []]); |
46
|
|
|
|
47
|
|
|
$output = $this->tablePopulator->populateTables($parsedSchema); |
48
|
|
|
|
49
|
|
|
$this->assertSame($parsedSchema, $output); |
50
|
|
|
|
51
|
|
|
$this->assertEquals(['table1', 'table2'], $parsedSchema->getTables()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testPopulateTablesWithNoTablesWillSearchTheFilesystemForTables() |
55
|
|
|
{ |
56
|
|
|
$config = Mockery::mock(SchemaConfigInterface::class); |
57
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', []); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->fileSystem->allows() |
60
|
|
|
->has('/a/path') |
61
|
|
|
->andReturns(true); |
62
|
|
|
|
63
|
|
|
$file1 = ['path' => '/a/path/table1.sql', 'size' => 1234]; |
64
|
|
|
$file2 = ['path' => '/a/path/table2.sql', 'size' => 1234]; |
65
|
|
|
|
66
|
|
|
$this->fileSystem->allows() |
67
|
|
|
->listContents('/a/path') |
68
|
|
|
->andReturns([$file1, $file2]); |
69
|
|
|
|
70
|
|
|
$config->allows(['getExcludes' => []]); |
71
|
|
|
|
72
|
|
|
$output = $this->tablePopulator->populateTables($parsedSchema); |
73
|
|
|
|
74
|
|
|
$this->assertSame($parsedSchema, $output); |
75
|
|
|
$this->assertEquals(['table1', 'table2'], $output->getTables()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testPopulateTablesWillFilterOutExcludedTables() |
79
|
|
|
{ |
80
|
|
|
$config = Mockery::mock(SchemaConfigInterface::class); |
81
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', []); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$this->fileSystem->allows() |
84
|
|
|
->has('/a/path') |
85
|
|
|
->andReturns(true); |
86
|
|
|
|
87
|
|
|
$file1 = ['path' => '/a/path/table1.sql', 'size' => 1234]; |
88
|
|
|
$file2 = ['path' => '/a/path/table2.sql', 'size' => 1234]; |
89
|
|
|
|
90
|
|
|
$this->fileSystem->allows() |
91
|
|
|
->listContents('/a/path') |
92
|
|
|
->andReturns([$file1, $file2]); |
93
|
|
|
|
94
|
|
|
$config->allows(['getExcludes' => ['table1']]); |
95
|
|
|
|
96
|
|
|
$this->tableFilterer->allows() |
97
|
|
|
->filter(['table1', 'table2'], ['table1']) |
98
|
|
|
->andReturns(['table2']); |
99
|
|
|
|
100
|
|
|
$output = $this->tablePopulator->populateTables($parsedSchema); |
101
|
|
|
|
102
|
|
|
$this->assertSame($parsedSchema, $output); |
103
|
|
|
$this->assertEquals(['table2'], $output->getTables()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testPopulateTablesWhenFolderDoesNotExistReturnsNull() |
107
|
|
|
{ |
108
|
|
|
$config = Mockery::mock(SchemaConfigInterface::class); |
109
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', []); |
|
|
|
|
110
|
|
|
$config->allows(['getExcludes' => []]); |
111
|
|
|
|
112
|
|
|
$this->fileSystem->allows() |
113
|
|
|
->has('/a/path') |
114
|
|
|
->andReturns(false); |
115
|
|
|
|
116
|
|
|
$output = $this->tablePopulator->populateTables($parsedSchema); |
117
|
|
|
|
118
|
|
|
$this->assertNull($output); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testPopulateTablesWithNoFilesReturnsNull() |
122
|
|
|
{ |
123
|
|
|
$config = Mockery::mock(Config\SchemaConfigInterface::class); |
124
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', []); |
|
|
|
|
125
|
|
|
$config->allows(['getExcludes' => []]); |
126
|
|
|
|
127
|
|
|
$this->fileSystem->allows() |
128
|
|
|
->has('/a/path') |
129
|
|
|
->andReturns(true); |
130
|
|
|
|
131
|
|
|
$this->fileSystem->allows() |
132
|
|
|
->listContents('/a/path') |
133
|
|
|
->andReturns([]); |
134
|
|
|
|
135
|
|
|
$output = $this->tablePopulator->populateTables($parsedSchema); |
136
|
|
|
|
137
|
|
|
$this->assertNull($output); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testEmptyPathReturnsNull() |
141
|
|
|
{ |
142
|
|
|
$config = Mockery::mock(SchemaConfigInterface::class); |
143
|
|
|
$parsedSchema = new ParsedSchema($config, '', []); |
|
|
|
|
144
|
|
|
|
145
|
|
|
$output = $this->tablePopulator->populateTables($parsedSchema); |
146
|
|
|
|
147
|
|
|
$this->assertNull($output); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|