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\Db; |
15
|
|
|
|
16
|
|
|
use Graze\Sprout\Config\ConnectionConfigInterface; |
17
|
|
|
use Graze\Sprout\Config\SchemaConfigInterface; |
18
|
|
|
use Graze\Sprout\Db\DbTablePopulator; |
19
|
|
|
use Graze\Sprout\Db\PdoFactory; |
20
|
|
|
use Graze\Sprout\Parser\ParsedSchema; |
21
|
|
|
use Graze\Sprout\Parser\TableFilterer; |
22
|
|
|
use Graze\Sprout\Test\TestCase; |
23
|
|
|
use Mockery; |
24
|
|
|
use PDO; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @runTestsInSeparateProcesses |
28
|
|
|
* @preserveGlobalState disabled |
29
|
|
|
*/ |
30
|
|
|
class DbTablePopulatorTest extends TestCase |
31
|
|
|
{ |
32
|
|
|
/** @var DbTablePopulator */ |
33
|
|
|
private $tablePopulator; |
34
|
|
|
/** @var mixed */ |
35
|
|
|
private $tableFilterer; |
36
|
|
|
/** @var mixed */ |
37
|
|
|
private $pdoFactory; |
38
|
|
|
|
39
|
|
|
public function setUp() |
40
|
|
|
{ |
41
|
|
|
$this->pdoFactory = Mockery::mock(PdoFactory::class); |
42
|
|
|
$this->tableFilterer = Mockery::mock(TableFilterer::class); |
43
|
|
|
$this->tablePopulator = new DbTablePopulator($this->pdoFactory, $this->tableFilterer); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testTablePopulation() |
47
|
|
|
{ |
48
|
|
|
$config = Mockery::mock(SchemaConfigInterface::class); |
49
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', []); |
|
|
|
|
50
|
|
|
$connection = Mockery::mock(ConnectionConfigInterface::class); |
51
|
|
|
$config->allows([ |
52
|
|
|
'getExcludes' => [], |
53
|
|
|
'getSchema' => 'schema1', |
54
|
|
|
'getConnection' => $connection, |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$pdo = Mockery::mock(PDO::class); |
58
|
|
|
$this->pdoFactory->allows() |
59
|
|
|
->getPdo($connection) |
60
|
|
|
->andReturns($pdo); |
61
|
|
|
$statement = Mockery::mock(\PDOStatement::class); |
62
|
|
|
$pdo->allows() |
63
|
|
|
->prepare( |
|
|
|
|
64
|
|
|
'SELECT table_name |
65
|
|
|
FROM INFORMATION_SCHEMA.TABLES |
66
|
|
|
WHERE table_schema = :schema |
67
|
|
|
AND table_type = "BASE TABLE"' |
68
|
|
|
) |
69
|
|
|
->andReturns($statement); |
70
|
|
|
$statement->allows() |
71
|
|
|
->execute(['schema' => 'schema1']) |
|
|
|
|
72
|
|
|
->andReturns($statement); |
73
|
|
|
$statement->allows() |
74
|
|
|
->fetchAll(PDO::FETCH_COLUMN) |
|
|
|
|
75
|
|
|
->andReturns(['table1', 'table2']); |
76
|
|
|
|
77
|
|
|
$output = $this->tablePopulator->populateTables($parsedSchema); |
78
|
|
|
|
79
|
|
|
$this->assertSame($parsedSchema, $output); |
80
|
|
|
|
81
|
|
|
$this->assertEquals(['table1', 'table2'], $output->getTables()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testNoTablesArePopulatedIfTheyAreAlreadyProvided() |
85
|
|
|
{ |
86
|
|
|
$config = Mockery::mock(SchemaConfigInterface::class); |
87
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', ['table1', 'table2']); |
|
|
|
|
88
|
|
|
$config->allows(['getExcludes' => []]); |
89
|
|
|
|
90
|
|
|
$output = $this->tablePopulator->populateTables($parsedSchema); |
91
|
|
|
|
92
|
|
|
$this->assertSame($parsedSchema, $output); |
93
|
|
|
|
94
|
|
|
$this->assertEquals(['table1', 'table2'], $output->getTables()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testTablesAreNotExcludedIfIncludedInExcludeList() |
98
|
|
|
{ |
99
|
|
|
$config = Mockery::mock(SchemaConfigInterface::class); |
100
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', ['table1', 'table2']); |
|
|
|
|
101
|
|
|
$config->allows(['getExcludes' => ['table1']]); |
102
|
|
|
|
103
|
|
|
$output = $this->tablePopulator->populateTables($parsedSchema); |
104
|
|
|
|
105
|
|
|
$this->assertSame($parsedSchema, $output); |
106
|
|
|
|
107
|
|
|
$this->assertEquals(['table1', 'table2'], $output->getTables()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testTablesAreFilteredWhenPopulatedFromADatabase() |
111
|
|
|
{ |
112
|
|
|
$config = Mockery::mock(SchemaConfigInterface::class); |
113
|
|
|
$parsedSchema = new ParsedSchema($config, '/a/path', []); |
|
|
|
|
114
|
|
|
$connection = Mockery::mock(ConnectionConfigInterface::class); |
115
|
|
|
$config->allows([ |
116
|
|
|
'getExcludes' => ['table1'], |
117
|
|
|
'getSchema' => 'schema1', |
118
|
|
|
'getConnection' => $connection, |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
$pdo = Mockery::mock(PDO::class); |
122
|
|
|
$this->pdoFactory->allows() |
123
|
|
|
->getPdo($connection) |
124
|
|
|
->andReturns($pdo); |
125
|
|
|
$statement = Mockery::mock(\PDOStatement::class); |
126
|
|
|
$pdo->allows() |
127
|
|
|
->prepare( |
128
|
|
|
'SELECT table_name |
129
|
|
|
FROM INFORMATION_SCHEMA.TABLES |
130
|
|
|
WHERE table_schema = :schema |
131
|
|
|
AND table_type = "BASE TABLE"' |
132
|
|
|
) |
133
|
|
|
->andReturns($statement); |
134
|
|
|
$statement->allows() |
135
|
|
|
->execute(['schema' => 'schema1']) |
136
|
|
|
->andReturns($statement); |
137
|
|
|
$statement->allows() |
138
|
|
|
->fetchAll(PDO::FETCH_COLUMN) |
139
|
|
|
->andReturns(['table1', 'table2']); |
140
|
|
|
|
141
|
|
|
$this->tableFilterer->allows() |
142
|
|
|
->filter(['table1', 'table2'], ['table1']) |
143
|
|
|
->andReturns(['table2']); |
144
|
|
|
|
145
|
|
|
$output = $this->tablePopulator->populateTables($parsedSchema); |
146
|
|
|
|
147
|
|
|
$this->assertSame($parsedSchema, $output); |
148
|
|
|
|
149
|
|
|
$this->assertEquals(['table2'], $output->getTables()); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|