CliTest::testVhost()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 92

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 92
rs 8.1745
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * admin
5
 *
6
 * @category    Tollwerk
7
 * @package     Tollwerk\Admin
8
 * @subpackage  Tollwerk\Admin\Tests
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2018 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 © 2018 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
use Tollwerk\Admin\Infrastructure\App;
41
use Tollwerk\Admin\Infrastructure\Shell\Binary;
42
43
/**
44
 * Command line tool tests
45
 *
46
 * @package Tollwerk\Admin
47
 * @subpackage Tollwerk\Admin\Tests
48
 */
49
class CliTest extends AbstractDatabaseTest
50
{
51
    /**
52
     * Admin CLI interface
53
     *
54
     * @var string
55
     */
56
    protected static $admincli;
57
    /**
58
     * Home directory base
59
     *
60
     * @var string
61
     */
62
    protected static $homebase;
63
    /**
64
     * Temporary directories
65
     *
66
     * @var array
67
     */
68
    protected static $tmpDirectories = [];
69
70
    /**
71
     * This method is called before the first test of this test class is run.
72
     */
73
    public static function setUpBeforeClass()
74
    {
75
        self::$admincli =
76
            dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'tollwerk-admin.php';
77
78
        // TODO: Make environment dependent
79
        self::$homebase = dirname(dirname(dirname(__DIR__))).DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR;
80
81
        // Ensure the account system group exists
82
        App::bootstrap();
83
        $command = Binary::sudo('groupadd');
84
        $command->addArg('-f');
85
        $command->addArg(App::getConfig('general.group'));
86
        if (!$command->execute()) {
87
            echo $command->getOutput().' ('.$command->getExitCode().')'.PHP_EOL;
88
            throw new \RuntimeException($command->getOutput(), $command->getExitCode());
89
        }
90
    }
91
92
    /**
93
     * Performs operation returned by getTearDownOperation().
94
     */
95
    public function tearDown()
96
    {
97
        parent::tearDown();
98
99
        // Remove all temporary directories
100
        foreach (self::$tmpDirectories as $tmpDirectory) {
101
            Binary::sudo('rm')->addArg('-Rf')->addArg($tmpDirectory)->execute();
102
        }
103
    }
104
105
    /**
106
     * Test creating, modifying and deleting an account
107
     */
108
    public function testAccount()
109
    {
110
        // Create an account
111
        $this->assertTrue($this->getAdminCmd()->addArg('account:create')->addArg('test')->execute());
112
        $this->assertEquals(1, $this->getConnection()->getRowCount('account'));
113
        $queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account');
114
        $expectedTable = $this->getFixtureDataSet('account_create.xml')->getTable('account');
115
        $this->assertTablesEqual($expectedTable, $queryTable);
116
        $this->assertDirectoryExists(self::$homebase.'test');
117
        $this->assertUserExists('test');
118
119
        // Enable the account
120
        $this->assertTrue($this->getAdminCmd()->addArg('account:enable')->addArg('test')->execute());
121
        $queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account');
122
        $expectedTable = $this->getFixtureDataSet('account_enable.xml')->getTable('account');
123
        $this->assertTablesEqual($expectedTable, $queryTable);
124
125
        // Disable the account
126
        $this->assertTrue($this->getAdminCmd()->addArg('account:disable')->addArg('test')->execute());
127
        $queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account');
128
        $expectedTable = $this->getFixtureDataSet('account_create.xml')->getTable('account');
129
        $this->assertTablesEqual($expectedTable, $queryTable);
130
131
        // Rename the account
132
        $this->assertTrue($this->getAdminCmd()->addArg('account:rename')->addArg('test')->addArg('renamed')->execute());
133
        $queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account');
134
        $expectedTable = $this->getFixtureDataSet('account_rename.xml')->getTable('account');
135
        $this->assertTablesEqual($expectedTable, $queryTable);
136
        $this->assertDirectoryExists(self::$homebase.'renamed');
137
        $this->assertDirectoryNotExists(self::$homebase.'test');
138
        $this->assertUserExists('renamed');
139
        $this->assertUserNotExists('tests');
140
141
        // Delete the account
142
        $this->assertTrue($this->getAdminCmd()->addArg('account:delete')->addArg('renamed')->execute());
143
        $this->assertEquals(0, $this->getConnection()->getRowCount('account'));
144
        $this->assertDirectoryExists(self::$homebase.'renamed');
145
        $this->assertUserNotExists('renamed');
146
147
        // Register home directory for removal
148
        self::$tmpDirectories[] = self::$homebase.'renamed';
149
    }
150
151
    /**
152
     * Test creating, modifying and deleting a domain
153
     */
154
    public function testDomain()
155
    {
156
        // Create an account
157
        $this->assertTrue($this->getAdminCmd()->addArg('account:create')->addArg('test')->execute());
158
159
        // Create a domain
160
        $this->assertTrue($this->getAdminCmd()->addArg('domain:create')->addArg('test')->addArg('example.com')
161
            ->execute());
162
        $this->assertEquals(1, $this->getConnection()->getRowCount('domain'));
163
        $queryTable = $this->getConnection()->createQueryTable('domain', 'SELECT * FROM domain');
164
        $expectedTable = $this->getFixtureDataSet('domain_create.xml')->getTable('domain');
165
        $this->assertTablesEqual($expectedTable, $queryTable);
166
167
        // Enable the domain
168
        $this->assertTrue($this->getAdminCmd()->addArg('domain:enable')->addArg('example.com')->execute());
169
        $queryTable = $this->getConnection()->createQueryTable('domain', 'SELECT * FROM domain');
170
        $expectedTable = $this->getFixtureDataSet('domain_enable.xml')->getTable('domain');
171
        $this->assertTablesEqual($expectedTable, $queryTable);
172
173
        // Enable the domain wildcard
174
        $this->assertTrue($this->getAdminCmd()->addArg('domain:enable:wildcard')->addArg('example.com')->execute());
175
        $queryTable = $this->getConnection()->createQueryTable('domain', 'SELECT * FROM domain');
176
        $expectedTable = $this->getFixtureDataSet('domain_enable_wildcard.xml')->getTable('domain');
177
        $this->assertTablesEqual($expectedTable, $queryTable);
178
179
        // Disable the domain
180
        $this->assertTrue($this->getAdminCmd()->addArg('domain:disable')->addArg('example.com')->execute());
181
        $queryTable = $this->getConnection()->createQueryTable('domain', 'SELECT * FROM domain');
182
        $expectedTable = $this->getFixtureDataSet('domain_disable.xml')->getTable('domain');
183
        $this->assertTablesEqual($expectedTable, $queryTable);
184
185
        // Disable the domain wildcard
186
        $this->assertTrue($this->getAdminCmd()->addArg('domain:disable:wildcard')->addArg('example.com')->execute());
187
        $queryTable = $this->getConnection()->createQueryTable('domain', 'SELECT * FROM domain');
188
        $expectedTable = $this->getFixtureDataSet('domain_create.xml')->getTable('domain');
189
        $this->assertTablesEqual($expectedTable, $queryTable);
190
191
        // Delete the domain
192
        $this->assertTrue($this->getAdminCmd()->addArg('domain:delete')->addArg('example.com')->execute());
193
        $this->assertEquals(0, $this->getConnection()->getRowCount('domain'));
194
195
        // Delete the account and the home directory
196
        $this->assertTrue($this->getAdminCmd()->addArg('account:delete')->addArg('test')->execute());
197
        $this->assertEquals(0, $this->getConnection()->getRowCount('account'));
198
        $this->assertUserNotExists('test');
199
        self::$tmpDirectories[] = self::$homebase.'test';
200
    }
201
202
    /**
203
     * Test creating, modifying and deleting a virtual host
204
     */
205
    public function testVhost()
206
    {
207
        // Create an account
208
        $this->assertTrue($this->getAdminCmd()->addArg('account:create')->addArg('test')->execute());
209
210
        // Create two domains
211
        $this->assertTrue($this->getAdminCmd()->addArg('domain:create')->addArg('test')->addArg('example.com')
212
            ->execute());
213
        $this->assertTrue($this->getAdminCmd()->addArg('domain:create')->addArg('test')->addArg('test.com')->execute());
214
        $this->assertEquals(2, $this->getConnection()->getRowCount('domain'));
215
        $queryTable = $this->getConnection()->createQueryTable('domain', 'SELECT * FROM domain');
216
        $expectedTable = $this->getFixtureDataSet('vhost_create_domain.xml')->getTable('domain');
217
        $this->assertTablesEqual($expectedTable, $queryTable);
218
219
        // Create a virtual host
220
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:create')->addArg('test')->addArg('example.com')
221
            ->execute());
222
        $this->assertEquals(1, $this->getConnection()->getRowCount('vhost'));
223
        $queryTable = $this->getConnection()->createQueryTable('vhost', 'SELECT * FROM vhost');
224
        $expectedTable = $this->getFixtureDataSet('vhost_create.xml')->getTable('vhost');
225
        $this->assertTablesEqual($expectedTable, $queryTable);
226
        $queryTable = $this->getConnection()->createQueryTable('domain', 'SELECT * FROM domain');
227
        $expectedTable = $this->getFixtureDataSet('vhost_create.xml')->getTable('domain');
228
        $this->assertTablesEqual($expectedTable, $queryTable);
229
230
        // Enable the vhost
231
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:enable')->addArg('test')->execute());
232
        $queryTable = $this->getConnection()->createQueryTable('vhost', 'SELECT * FROM vhost');
233
        $expectedTable = $this->getFixtureDataSet('vhost_enable.xml')->getTable('vhost');
234
        $this->assertTablesEqual($expectedTable, $queryTable);
235
236
        // Disable the vhost
237
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:disable')->addArg('test')->execute());
238
        $queryTable = $this->getConnection()->createQueryTable('vhost', 'SELECT * FROM vhost');
239
        $expectedTable = $this->getFixtureDataSet('vhost_create.xml')->getTable('vhost');
240
        $this->assertTablesEqual($expectedTable, $queryTable);
241
242
        // Configure PHP, the HTTP and the HTTPS ports, the redirect URL and status
243
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:php')->addArg('test')->addArg('/')->addArg('7.0')
244
            ->execute());
245
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:port:add')->addArg('test')->addArg('/')->addArg('http')
246
            ->addArg('81')->execute());
247
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:port:add')->addArg('test')->addArg('/')->addArg('https')
248
            ->addArg('444')->execute());
249
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:redirect')->addArg('test')->addArg('/')
250
            ->addArg('http://test.com')->addArg('302')->execute());
251
        $queryTable = $this->getConnection()->createQueryTable('vhost', 'SELECT * FROM vhost');
252
        $expectedTable = $this->getFixtureDataSet('vhost_php_ports_redirect.xml')->getTable('vhost');
253
        $this->assertTablesEqual($expectedTable, $queryTable);
254
        $queryTable = $this->getConnection()->createQueryTable('port', 'SELECT * FROM port');
255
        $expectedTable = $this->getFixtureDataSet('vhost_php_ports_redirect.xml')->getTable('port');
256
        $this->assertTablesEqual($expectedTable, $queryTable);
257
258
        // Reset PHP, the HTTP and the HTTPS ports, the redirect URL and status
259
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:php')->addArg('test')->execute());
260
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:port:remove')->addArg('test')->addArg('/')
261
            ->addArg('http')->addArg('81')->execute());
262
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:port:remove')->addArg('test')->addArg('/')
263
            ->addArg('https')->addArg('444')->execute());
264
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:redirect')->addArg('test')->execute());
265
        $queryTable = $this->getConnection()->createQueryTable('vhost', 'SELECT * FROM vhost');
266
        $expectedTable = $this->getFixtureDataSet('vhost_reset.xml')->getTable('vhost');
267
        $this->assertTablesEqual($expectedTable, $queryTable);
268
        $this->assertEquals(0, $this->getConnection()->getRowCount('port'));
269
270
        // Add a secondary domain
271
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:domain:add')->addArg('test')->addArg('/')
272
            ->addArg('test.com')->execute());
273
        $queryTable = $this->getConnection()->createQueryTable('domain', 'SELECT * FROM domain');
274
        $expectedTable = $this->getFixtureDataSet('vhost_domain_add.xml')->getTable('domain');
275
        $this->assertTablesEqual($expectedTable, $queryTable);
276
277
        // Remove a secondary domain
278
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:domain:remove')->addArg('test')->addArg('/')
279
            ->addArg('test.com')->execute());
280
        $queryTable = $this->getConnection()->createQueryTable('domain', 'SELECT * FROM domain');
281
        $expectedTable = $this->getFixtureDataSet('vhost_create.xml')->getTable('domain');
282
        $this->assertTablesEqual($expectedTable, $queryTable);
283
284
        // Delete the virtual host
285
        $this->assertTrue($this->getAdminCmd()->addArg('vhost:delete')->addArg('test')->execute());
286
        $this->assertEquals(0, $this->getConnection()->getRowCount('vhost'));
287
        $queryTable = $this->getConnection()->createQueryTable('domain', 'SELECT * FROM domain');
288
        $expectedTable = $this->getFixtureDataSet('vhost_create_domain.xml')->getTable('domain');
289
        $this->assertTablesEqual($expectedTable, $queryTable);
290
291
        // Delete the account and the home directory
292
        $this->assertTrue($this->getAdminCmd()->addArg('account:delete')->addArg('test')->execute());
293
        $this->assertEquals(0, $this->getConnection()->getRowCount('account'));
294
        $this->assertUserNotExists('test');
295
//        self::$tmpDirectories[] = self::$homebase.'test';
296
    }
297
298
    /**
299
     * Returns the test dataset
300
     *
301
     * @return \PHPUnit_Extensions_Database_DataSet_IDataSet
302
     */
303
    protected function getDataSet()
304
    {
305
        return $this->createMySQLXMLDataSet(
306
            __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'admin_test_cli.xml'
307
        );
308
    }
309
310
    /**
311
     * Create and return an admin base command
312
     *
313
     * @return Command Admin base command
314
     */
315
    protected function getAdminCmd()
316
    {
317
        $command = new TestCommand();
318
        $command->setCommand(Binary::get('php'));
319
        $command->addArg(self::$admincli);
320
        return $command;
321
    }
322
323
    /**
324
     * Assert that a system user exists
325
     *
326
     * @param string $user User name
327
     */
328
    protected function assertUserExists($user)
329
    {
330
        $command = new Command();
331
        $command->setCommand(Binary::get('id'));
332
        $command->addArg($user);
333
        return $command->execute();
334
    }
335
336
    /**
337
     * Assert that a system user doesn't exist
338
     *
339
     * @param string $user User name
340
     */
341
    protected function assertUserNotExists($user)
342
    {
343
        $command = new Command();
344
        $command->setCommand(Binary::get('id'));
345
        $command->addArg($user);
346
        return !$command->execute();
347
    }
348
}
349