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 League\Flysystem\AdapterInterface; |
11
|
|
|
use Mockery; |
12
|
|
|
|
13
|
|
|
class TablePopulatorTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** @var TablePopulator */ |
16
|
|
|
private $schemaParser; |
17
|
|
|
/** @var mixed */ |
18
|
|
|
private $fileSystem; |
19
|
|
|
/** @var mixed */ |
20
|
|
|
private $config; |
|
|
|
|
21
|
|
|
|
22
|
|
|
public function setUp() |
23
|
|
|
{ |
24
|
|
|
$this->fileSystem = Mockery::mock(AdapterInterface::class); |
25
|
|
|
$this->schemaParser = new TablePopulator($this->fileSystem); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testPopulateTablesWithExistingTablesDoesNothing() |
29
|
|
|
{ |
30
|
|
|
$config = Mockery::mock(Config\SchemaConfigInterface::class); |
31
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', ['table1', 'table2']); |
|
|
|
|
32
|
|
|
|
33
|
|
|
$output = $this->schemaParser->populateTables($parsedSchema); |
34
|
|
|
|
35
|
|
|
$this->assertSame($parsedSchema, $output); |
36
|
|
|
|
37
|
|
|
$this->assertEquals(['table1', 'table2'], $parsedSchema->getTables()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testPopulateTablesWithNoTablesWillSearchTheFilesystemForTables() |
41
|
|
|
{ |
42
|
|
|
$config = Mockery::mock(Config\SchemaConfigInterface::class); |
43
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', []); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$this->fileSystem->allows() |
46
|
|
|
->has('/a/path') |
47
|
|
|
->andReturns(true); |
48
|
|
|
|
49
|
|
|
$file1 = Mockery::mock(\SplFileInfo::class); |
50
|
|
|
$file1->allows(['getExtension' => 'sql', 'getSize' => 1234]); |
51
|
|
|
$file1->allows() |
52
|
|
|
->getBasename('.sql') |
|
|
|
|
53
|
|
|
->andReturns('table1'); |
54
|
|
|
|
55
|
|
|
$file2 = Mockery::mock(\SplFileInfo::class); |
56
|
|
|
$file2->allows(['getExtension' => 'sql', 'getSize' => 1235]); |
57
|
|
|
$file2->allows() |
58
|
|
|
->getBasename('.sql') |
59
|
|
|
->andReturns('table2'); |
60
|
|
|
|
61
|
|
|
$this->fileSystem->allows() |
62
|
|
|
->listContents('/a/path') |
63
|
|
|
->andReturns([$file1, $file2]); |
64
|
|
|
|
65
|
|
|
$output = $this->schemaParser->populateTables($parsedSchema); |
66
|
|
|
|
67
|
|
|
$this->assertSame($parsedSchema, $output); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testPopulateTablesWhenFolderDoesNotExistReturnsNull() |
71
|
|
|
{ |
72
|
|
|
$config = Mockery::mock(Config\SchemaConfigInterface::class); |
73
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', []); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$this->fileSystem->allows() |
76
|
|
|
->has('/a/path') |
77
|
|
|
->andReturns(false); |
78
|
|
|
|
79
|
|
|
$output = $this->schemaParser->populateTables($parsedSchema); |
80
|
|
|
|
81
|
|
|
$this->assertNull($output); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testPopulateTablesWithNoFilesReturnsNull() |
85
|
|
|
{ |
86
|
|
|
$config = Mockery::mock(Config\SchemaConfigInterface::class); |
87
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', []); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$this->fileSystem->allows() |
90
|
|
|
->has('/a/path') |
91
|
|
|
->andReturns(true); |
92
|
|
|
|
93
|
|
|
$this->fileSystem->allows() |
94
|
|
|
->listContents('/a/path') |
95
|
|
|
->andReturns([]); |
96
|
|
|
|
97
|
|
|
$output = $this->schemaParser->populateTables($parsedSchema); |
98
|
|
|
|
99
|
|
|
$this->assertNull($output); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|