1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2014 ekow. |
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\cases; |
28
|
|
|
|
29
|
|
|
use org\bovigo\vfs\vfsStream; |
30
|
|
|
use yentu\commands\Import; |
31
|
|
|
|
32
|
|
|
class ImportTest extends \yentu\tests\YentuTest |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
public function setUp() |
36
|
|
|
{ |
37
|
|
|
$this->testDatabase = 'yentu_import_test'; |
38
|
|
|
parent::setup(); |
39
|
|
|
$this->createDb($GLOBALS['DB_NAME']); |
40
|
|
|
$this->initYentu($GLOBALS['DB_NAME']); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testImport() |
44
|
|
|
{ |
45
|
|
|
$this->initDb($GLOBALS['DB_FULL_DSN'], file_get_contents("tests/sql/{$GLOBALS['DRIVER']}/system.sql")); |
46
|
|
|
$codeWriter = $this->createMock('\\yentu\\CodeWriter', array('getTimestamp')); |
|
|
|
|
47
|
|
|
$codeWriter->method('getTimestamp')->willReturn('25th August, 2014 14:30:13'); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$import = new Import($this->yentu, $this->getManipulatorFactory(), $this->io); |
50
|
|
|
$import->setCodeWriter($codeWriter); |
51
|
|
|
$description = $import->run(array()); |
|
|
|
|
52
|
|
|
$newVersion = $import->getNewVersion(); |
53
|
|
|
$this->assertFileExists( |
54
|
|
|
vfsStream::url("home/yentu/migrations/{$newVersion}_import.php") |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
require "tests/expected/{$GLOBALS['DRIVER']}/import.php"; |
58
|
|
|
$descriptionArray = $description->getArray(); |
59
|
|
|
|
60
|
|
|
unset($descriptionArray['tables']['yentu_history']); |
61
|
|
|
$this->assertEquals( |
62
|
|
|
$expectedDescription, [ |
|
|
|
|
63
|
|
|
'schemata' => $descriptionArray['schemata'], |
64
|
|
|
'tables' => $descriptionArray['tables'], |
65
|
|
|
'views' => $descriptionArray['views'] |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testSchemaImport() |
70
|
|
|
{ |
71
|
|
|
$this->skipSchemaTests(); |
72
|
|
|
$this->connect($GLOBALS['DB_FULL_DSN']); |
73
|
|
|
$this->pdo->query('DROP SCHEMA IF EXISTS hr'); |
74
|
|
|
$this->pdo->query('DROP SCHEMA IF EXISTS common'); |
75
|
|
|
|
76
|
|
|
$this->initDb($GLOBALS['DB_FULL_DSN'], file_get_contents("tests/sql/{$GLOBALS['DRIVER']}/import_schema.sql")); |
77
|
|
|
$this->connect($GLOBALS['DB_FULL_DSN']); |
78
|
|
|
|
79
|
|
|
$codeWriter = $this->createMock('\\yentu\\CodeWriter', array('getTimestamp')); |
|
|
|
|
80
|
|
|
$codeWriter->method('getTimestamp')->willReturn('25th August, 2014 14:30:13'); |
81
|
|
|
$import = new Import($this->yentu, $this->getManipulatorFactory(), $this->io); |
82
|
|
|
$import->setCodeWriter($codeWriter); |
83
|
|
|
$import->run(array()); |
|
|
|
|
84
|
|
|
$newVersion = $import->getNewVersion(); |
85
|
|
|
$this->assertFileExists( |
86
|
|
|
vfsStream::url("home/yentu/migrations/{$newVersion}_import.php") |
87
|
|
|
); |
88
|
|
|
$this->assertSchemaExists('common'); |
89
|
|
|
$this->assertSchemaExists('hr'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testViewImport() |
93
|
|
|
{ |
94
|
|
|
$this->initDb($GLOBALS['DB_FULL_DSN'], file_get_contents("tests/sql/{$GLOBALS['DRIVER']}/import_views.sql")); |
95
|
|
|
$this->connect($GLOBALS['DB_FULL_DSN']); |
96
|
|
|
|
97
|
|
|
$codeWriter = $this->createMock('\\yentu\\CodeWriter', array('getTimestamp')); |
|
|
|
|
98
|
|
|
$codeWriter->method('getTimestamp')->willReturn('25th August, 2014 14:30:13'); |
99
|
|
|
$import = new Import($this->yentu, $this->getManipulatorFactory(), $this->io); |
100
|
|
|
$import->setCodeWriter($codeWriter); |
101
|
|
|
$import->run(array()); |
|
|
|
|
102
|
|
|
$newVersion = $import->getNewVersion(); |
103
|
|
|
$this->assertFileExists( |
104
|
|
|
vfsStream::url("home/yentu/migrations/{$newVersion}_import.php") |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$this->assertTableExists('employees_view'); |
108
|
|
|
$this->assertTableExists('disabled_employees_view'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @expectedException \yentu\exceptions\CommandException |
113
|
|
|
*/ |
114
|
|
|
public function testImportNonEmptyMigrations() |
115
|
|
|
{ |
116
|
|
|
file_put_contents(vfsStream::url('home/yentu/migrations/1234568901234_existing.php'), 'nothing'); |
117
|
|
|
$import = new Import($this->yentu, $this->getManipulatorFactory(), $this->io); |
118
|
|
|
$import->run(array()); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testDatabaseNotExisting() |
122
|
|
|
{ |
123
|
|
|
$this->skipSchemaTests(); |
124
|
|
|
$this->connect($GLOBALS['DB_FULL_DSN']); |
125
|
|
|
try { |
126
|
|
|
$this->pdo->query('DROP TABLE IF EXISTS yentu_history'); |
127
|
|
|
$this->pdo->query('DROP SEQUENCE IF EXISTS yentu_history_id_seq'); |
128
|
|
|
} catch (\PDOException $e) { |
|
|
|
|
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
$import = new Import($this->yentu, $this->getManipulatorFactory(), $this->io); |
132
|
|
|
$import->run(array()); |
|
|
|
|
133
|
|
|
$this->assertTableExists('yentu_history'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
class NegativeMockAssertor |
139
|
|
|
{ |
140
|
|
|
|
141
|
|
|
public function __call($name, $arguments) |
142
|
|
|
{ |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.