1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2014 Ekow Abaka Ainoson |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace yentu\tests; |
28
|
|
|
|
29
|
|
|
use clearice\io\Io; |
30
|
|
|
use org\bovigo\vfs\vfsStream; |
31
|
|
|
use yentu\Yentu; |
32
|
|
|
use PHPUnit\Framework\TestCase; |
33
|
|
|
use yentu\commands\Init; |
34
|
|
|
use yentu\DatabaseManipulatorFactory; |
35
|
|
|
use ntentan\atiaa\DriverFactory; |
36
|
|
|
use ntentan\config\Config; |
37
|
|
|
|
38
|
|
|
class YentuTest extends TestCase |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* |
43
|
|
|
* @var \PDO |
44
|
|
|
*/ |
45
|
|
|
protected $pdo; |
46
|
|
|
protected $testDatabase; |
47
|
|
|
protected $testDefaultSchema; |
48
|
|
|
protected $yentu; |
49
|
|
|
protected $config; |
50
|
|
|
protected $io; |
51
|
|
|
|
52
|
|
|
public function setup() |
53
|
|
|
{ |
54
|
|
|
require_once "src/globals.php"; |
55
|
|
|
$this->io = new Io(); |
56
|
|
|
$this->setupStreams(); |
57
|
|
|
$this->io->setOutputLevel(Io::OUTPUT_LEVEL_1); |
58
|
|
|
$this->config = new Config; |
59
|
|
|
$this->yentu = new Yentu($this->io); |
60
|
|
|
$this->yentu->setDefaultHome(vfsStream::url('home/yentu')); |
61
|
|
|
|
62
|
|
|
$GLOBALS['DRIVER'] = getenv('YENTU_DRIVER'); |
63
|
|
|
$GLOBALS['DB_DSN'] = getenv('YENTU_BASE_DSN'); |
64
|
|
|
|
65
|
|
|
if (getenv('YENTU_FILE') === false) { |
66
|
|
|
$GLOBALS['DB_FULL_DSN'] = "{$GLOBALS['DB_DSN']};dbname={$this->testDatabase}"; |
67
|
|
|
$GLOBALS['DB_NAME'] = $this->testDatabase; |
68
|
|
|
$GLOBALS['DEFAULT_SCHEMA'] = (string) getenv('YENTU_DEFAULT_SCHEMA') == '' ? |
69
|
|
|
$this->testDatabase : (string) getenv('YENTU_DEFAULT_SCHEMA'); |
70
|
|
|
$GLOBALS['DB_FILE'] = ''; |
71
|
|
|
} else { |
72
|
|
|
$GLOBALS['DB_FULL_DSN'] = $GLOBALS['DB_DSN']; |
73
|
|
|
$GLOBALS['DB_FILE'] = getenv('YENTU_FILE'); |
74
|
|
|
$GLOBALS['DB_NAME'] = ''; |
75
|
|
|
$GLOBALS['DEFAULT_SCHEMA'] = ''; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$GLOBALS['DB_USER'] = (string) getenv('YENTU_USER'); |
79
|
|
|
$GLOBALS['DB_PASSWORD'] = (string) getenv('YENTU_PASSWORD'); |
80
|
|
|
$GLOBALS['DB_HOST'] = (string) getenv('YENTU_HOST'); |
81
|
|
|
|
82
|
|
|
$timer = $this->getMockBuilder("\\yentu\\Timer")->setMethods(array('stopInstance', 'startInstance'))->getMock(); |
83
|
|
|
$timer->method('stopInstance')->willReturn(10.0000); |
|
|
|
|
84
|
|
|
\yentu\Timer::setInstance($timer); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function setupStreams() |
88
|
|
|
{ |
89
|
|
|
vfsStream::setup('home'); |
90
|
|
|
$this->io->setStreamUrl('output', vfsStream::url('home/output.txt')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function tearDown() |
94
|
|
|
{ |
95
|
|
|
$this->pdo = null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function assertSchemaExists($schema, $message = '') |
99
|
|
|
{ |
100
|
|
|
$constraint = new constraints\SchemaExists(); |
101
|
|
|
$constraint->setPDO($this->pdo); |
102
|
|
|
$this->assertThat($schema, $constraint, $message); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function assertTableExists($table, $message = '') |
106
|
|
|
{ |
107
|
|
|
$constraint = new constraints\TableExists(); |
108
|
|
|
$constraint->setPDO($this->pdo); |
109
|
|
|
$this->assertThat($table, $constraint, $message); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function assertTableDoesntExist($table, $message = '') |
113
|
|
|
{ |
114
|
|
|
$constraint = new constraints\TableExists(); |
115
|
|
|
$constraint->negate(); |
116
|
|
|
$constraint->setPDO($this->pdo); |
117
|
|
|
$this->assertThat($table, $constraint, $message); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function assertColumnExists($column, $table, $message = '') |
121
|
|
|
{ |
122
|
|
|
$constraint = new constraints\ColumnExists(); |
123
|
|
|
$constraint->setPDO($this->pdo); |
124
|
|
|
$constraint->setTable($table); |
125
|
|
|
$this->assertThat($column, $constraint, $message); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function assertColumnNullable($column, $table, $message = '') |
129
|
|
|
{ |
130
|
|
|
$constraint = new constraints\ColumnNullability(); |
131
|
|
|
$constraint->setPDO($this->pdo); |
132
|
|
|
$constraint->setTable($table); |
133
|
|
|
$this->assertThat($column, $constraint, $message); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function assertColumnNotNullable($column, $table, $message = '') |
137
|
|
|
{ |
138
|
|
|
$constraint = new constraints\ColumnNullability(); |
139
|
|
|
$constraint->setPDO($this->pdo); |
140
|
|
|
$constraint->setTable($table); |
141
|
|
|
$constraint->setNullability(false); |
142
|
|
|
$this->assertThat($column, $constraint, $message); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function assertForeignKeyExists($table, $message = '') |
146
|
|
|
{ |
147
|
|
|
$constraint = new constraints\ForeignKeyExists(); |
148
|
|
|
$constraint->setPDO($this->pdo); |
149
|
|
|
$this->assertThat($table, $constraint, $message); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function assertForeignKeyDoesntExist($table, $message = '') |
153
|
|
|
{ |
154
|
|
|
$constraint = new constraints\ForeignKeyExists(); |
155
|
|
|
$constraint->negate(); |
156
|
|
|
$constraint->setPDO($this->pdo); |
157
|
|
|
$this->assertThat($table, $constraint, $message); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
protected function createDb($name) |
161
|
|
|
{ |
162
|
|
|
if (getenv('YENTU_FILE') !== false) { |
163
|
|
|
if (file_exists(getenv('YENTU_FILE'))) { |
164
|
|
|
unlink(getenv('YENTU_FILE')); |
165
|
|
|
} |
166
|
|
|
} else { |
167
|
|
|
$pdo = new \PDO($GLOBALS["DB_DSN"], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWORD']); |
168
|
|
|
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
169
|
|
|
$pdo->exec("DROP DATABASE IF EXISTS $name"); |
170
|
|
|
$pdo->exec("CREATE DATABASE $name"); |
171
|
|
|
$pdo = null; |
|
|
|
|
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
protected function initDb($dsn, $queries) |
176
|
|
|
{ |
177
|
|
|
$pdo = new \PDO($dsn, $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWORD']); |
178
|
|
|
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
179
|
|
|
$pdo->exec($queries); |
180
|
|
|
$pdo = null; |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
protected function getManipulatorFactory() |
184
|
|
|
{ |
185
|
|
|
return new DatabaseManipulatorFactory($this->yentu, $this->config, new DriverFactory(), $this->io);; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
protected function initYentu($name) |
189
|
|
|
{ |
190
|
|
|
$config = new Config(); |
191
|
|
|
$factory = new DatabaseManipulatorFactory($this->yentu, $config, new DriverFactory(), $this->io); |
192
|
|
|
$init = new Init($this->yentu, $factory, $this->io, $config); |
193
|
|
|
$init->run( |
194
|
|
|
array( |
195
|
|
|
'driver' => $GLOBALS['DRIVER'], |
196
|
|
|
'host' => $GLOBALS['DB_HOST'], |
197
|
|
|
'dbname' => $name, |
198
|
|
|
'user' => $GLOBALS['DB_USER'], |
199
|
|
|
'password' => $GLOBALS['DB_PASSWORD'], |
200
|
|
|
'file' => $GLOBALS['DB_FILE'] |
201
|
|
|
) |
202
|
|
|
); |
203
|
|
|
$this->config->readPath($this->yentu->getPath("config/default.conf.php")); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
protected function connect($dsn) |
207
|
|
|
{ |
208
|
|
|
$this->pdo = new \PDO($dsn, $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWORD']); |
209
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
protected function setupForMigration() |
213
|
|
|
{ |
214
|
|
|
$this->createDb($GLOBALS['DB_NAME']); |
215
|
|
|
$this->connect($GLOBALS["DB_FULL_DSN"]); |
216
|
|
|
$this->initYentu($GLOBALS['DB_NAME']); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
protected function skipSchemaTests() |
220
|
|
|
{ |
221
|
|
|
if (getenv('YENTU_SKIP_SCHEMAS') === 'yes') { |
222
|
|
|
$this->markTestSkipped(); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.