1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* admin |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Server |
8
|
|
|
* @subpackage Tollwerk\Admin\Tests |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Tollwerk\Admin\Tests; |
38
|
|
|
|
39
|
|
|
use mikehaertl\shellcommand\Command; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Command line tool tests |
43
|
|
|
* |
44
|
|
|
* @package Tollwerk\Admin |
45
|
|
|
* @subpackage Tollwerk\Admin\Tests |
46
|
|
|
*/ |
47
|
|
|
class CliTest extends AbstractDatabaseTest |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* PHP binary |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected static $phpcmd; |
55
|
|
|
/** |
56
|
|
|
* id command |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected static $idcmd; |
61
|
|
|
/** |
62
|
|
|
* Admin CLI interface |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected static $admincli; |
67
|
|
|
/** |
68
|
|
|
* Home directory base |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected static $homebase; |
73
|
|
|
/** |
74
|
|
|
* Temporary directories |
75
|
|
|
* |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
protected static $tmpDirectories = []; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* This method is called before the first test of this test class is run. |
82
|
|
|
*/ |
83
|
|
|
public static function setUpBeforeClass() |
84
|
|
|
{ |
85
|
|
|
self::$admincli = |
86
|
|
|
dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'tollwerk-admin.php'; |
87
|
|
|
|
88
|
|
|
// TODO: Make environment dependent |
89
|
|
|
self::$homebase = dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR; |
90
|
|
|
|
91
|
|
|
// Get the PHP cli command |
92
|
|
|
$command = new Command(); |
93
|
|
|
$command->setCommand('which'); |
94
|
|
|
$command->addArg('php'); |
95
|
|
|
|
96
|
|
|
if (!$command->execute()) { |
97
|
|
|
throw new \RuntimeException($command->getError(), $command->getExitCode()); |
98
|
|
|
} |
99
|
|
|
self::$phpcmd = $command->getOutput(); |
100
|
|
|
|
101
|
|
|
// Get the `id` command |
102
|
|
|
$command = new Command(); |
103
|
|
|
$command->setCommand('which'); |
104
|
|
|
$command->addArg('id'); |
105
|
|
|
if (!$command->execute()) { |
106
|
|
|
throw new \RuntimeException($command->getError(), $command->getExitCode()); |
107
|
|
|
} |
108
|
|
|
self::$idcmd = $command->getOutput(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Performs operation returned by getTearDownOperation(). |
113
|
|
|
*/ |
114
|
|
|
public function tearDown() |
115
|
|
|
{ |
116
|
|
|
parent::tearDown(); |
117
|
|
|
|
118
|
|
|
// Remove all temporary directories |
119
|
|
|
foreach (self::$tmpDirectories as $tmpDirectory) { |
120
|
|
|
$this->deleteTree($tmpDirectory); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Test creating an account |
126
|
|
|
*/ |
127
|
|
|
public function testAccount() |
128
|
|
|
{ |
129
|
|
|
$this->assertTrue($this->getAdminCmd()->addArg('account:create')->addArg('test')->execute()); |
130
|
|
|
$this->assertEquals(1, $this->getConnection()->getRowCount('account')); |
131
|
|
|
$queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account'); |
132
|
|
|
$expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_create.xml'))->getTable('account'); |
133
|
|
|
$this->assertTablesEqual($expectedTable, $queryTable); |
134
|
|
|
$this->assertDirectoryExists(self::$homebase.'test'); |
135
|
|
|
$this->assertUserExists('test'); |
136
|
|
|
|
137
|
|
|
// Enable the account |
138
|
|
|
$this->assertTrue($this->getAdminCmd()->addArg('account:enable')->addArg('test')->execute()); |
139
|
|
|
$queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account'); |
140
|
|
|
$expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_enable.xml'))->getTable('account'); |
141
|
|
|
$this->assertTablesEqual($expectedTable, $queryTable); |
142
|
|
|
|
143
|
|
|
// Disable the account |
144
|
|
|
$this->assertTrue($this->getAdminCmd()->addArg('account:disable')->addArg('test')->execute()); |
145
|
|
|
$queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account'); |
146
|
|
|
$expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_create.xml'))->getTable('account'); |
147
|
|
|
$this->assertTablesEqual($expectedTable, $queryTable); |
148
|
|
|
|
149
|
|
|
// Rename the account |
150
|
|
|
$this->assertTrue($this->getAdminCmd()->addArg('account:rename')->addArg('test')->addArg('renamed')->execute()); |
151
|
|
|
$queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account'); |
152
|
|
|
$expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_rename.xml'))->getTable('account'); |
153
|
|
|
$this->assertTablesEqual($expectedTable, $queryTable); |
154
|
|
|
$this->assertDirectoryExists(self::$homebase.'renamed'); |
155
|
|
|
$this->assertDirectoryNotExists(self::$homebase.'test'); |
156
|
|
|
$this->assertUserExists('renamed'); |
157
|
|
|
$this->assertUserNotExists('tests'); |
158
|
|
|
|
159
|
|
|
// Delete the account |
160
|
|
|
$this->assertTrue($this->getAdminCmd()->addArg('account:delete')->addArg('renamed')->execute()); |
161
|
|
|
$this->assertEquals(0, $this->getConnection()->getRowCount('account')); |
162
|
|
|
$this->assertDirectoryExists(self::$homebase.'renamed'); |
163
|
|
|
$this->assertUserNotExists('renamed'); |
164
|
|
|
|
165
|
|
|
// Register home directory for removal |
166
|
|
|
self::$tmpDirectories[] = self::$homebase.'renamed'; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns the test dataset |
171
|
|
|
* |
172
|
|
|
* @return \PHPUnit_Extensions_Database_DataSet_IDataSet |
173
|
|
|
*/ |
174
|
|
|
protected function getDataSet() |
175
|
|
|
{ |
176
|
|
|
return $this->createMySQLXMLDataSet( |
177
|
|
|
__DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'admin_test_cli.xml' |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Create and return an admin base command |
183
|
|
|
* |
184
|
|
|
* @return Command Admin base command |
185
|
|
|
*/ |
186
|
|
|
protected function getAdminCmd() |
187
|
|
|
{ |
188
|
|
|
$command = new Command(); |
189
|
|
|
$command->setCommand(self::$phpcmd); |
190
|
|
|
$command->addArg(self::$admincli); |
191
|
|
|
return $command; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Return the absolute path to a fixture file |
196
|
|
|
* |
197
|
|
|
* @param string $fixture Fixture file name |
198
|
|
|
* @return string Absolute fixture file |
199
|
|
|
*/ |
200
|
|
|
protected function getFixture($fixture) |
201
|
|
|
{ |
202
|
|
|
return __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.$fixture; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Recursively delete a directory |
207
|
|
|
* |
208
|
|
|
* @param string $dir Directory |
209
|
|
|
* @return bool Success |
210
|
|
|
*/ |
211
|
|
|
protected function deleteTree($dir) |
212
|
|
|
{ |
213
|
|
|
foreach (array_diff(scandir($dir), array('.', '..')) as $file) { |
214
|
|
|
if (is_dir("$dir/$file")) { |
215
|
|
|
$this->deleteTree("$dir/$file"); |
216
|
|
|
continue; |
217
|
|
|
} |
218
|
|
|
unlink("$dir/$file"); |
219
|
|
|
} |
220
|
|
|
return rmdir($dir); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Assert that a system user exists |
225
|
|
|
* |
226
|
|
|
* @param string $user User name |
227
|
|
|
*/ |
228
|
|
|
protected function assertUserExists($user) |
229
|
|
|
{ |
230
|
|
|
$command = new Command(); |
231
|
|
|
$command->setCommand(self::$idcmd); |
232
|
|
|
$command->addArg($user); |
233
|
|
|
return $command->execute(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Assert that a system user doesn't exist |
238
|
|
|
* |
239
|
|
|
* @param string $user User name |
240
|
|
|
*/ |
241
|
|
|
protected function assertUserNotExists($user) |
242
|
|
|
{ |
243
|
|
|
$command = new Command(); |
244
|
|
|
$command->setCommand(self::$idcmd); |
245
|
|
|
$command->addArg($user); |
246
|
|
|
return !$command->execute(); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|