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 = ['path' => '/a/path/table1.sql', 'size' => 1234]; |
50
|
|
|
$file2 = ['path' => '/a/path/table2.sql', 'size' => 1234]; |
51
|
|
|
|
52
|
|
|
$this->fileSystem->allows() |
53
|
|
|
->listContents('/a/path') |
54
|
|
|
->andReturns([$file1, $file2]); |
55
|
|
|
|
56
|
|
|
$output = $this->schemaParser->populateTables($parsedSchema); |
57
|
|
|
|
58
|
|
|
$this->assertSame($parsedSchema, $output); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testPopulateTablesWhenFolderDoesNotExistReturnsNull() |
62
|
|
|
{ |
63
|
|
|
$config = Mockery::mock(Config\SchemaConfigInterface::class); |
64
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', []); |
|
|
|
|
65
|
|
|
|
66
|
|
|
$this->fileSystem->allows() |
67
|
|
|
->has('/a/path') |
68
|
|
|
->andReturns(false); |
69
|
|
|
|
70
|
|
|
$output = $this->schemaParser->populateTables($parsedSchema); |
71
|
|
|
|
72
|
|
|
$this->assertNull($output); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testPopulateTablesWithNoFilesReturnsNull() |
76
|
|
|
{ |
77
|
|
|
$config = Mockery::mock(Config\SchemaConfigInterface::class); |
78
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', []); |
|
|
|
|
79
|
|
|
|
80
|
|
|
$this->fileSystem->allows() |
81
|
|
|
->has('/a/path') |
82
|
|
|
->andReturns(true); |
83
|
|
|
|
84
|
|
|
$this->fileSystem->allows() |
85
|
|
|
->listContents('/a/path') |
86
|
|
|
->andReturns([]); |
87
|
|
|
|
88
|
|
|
$output = $this->schemaParser->populateTables($parsedSchema); |
89
|
|
|
|
90
|
|
|
$this->assertNull($output); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|