Completed
Push — master ( faf2cb...9003f0 )
by Joschi
04:55
created

CliTest::deleteTree()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
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 © 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
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('shell.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 an account
107
     */
108
    public function testAccount()
109
    {
110
        $command = $this->getAdminCmd()->addArg('account:create')->addArg('test');
111
        $success = $command->execute();
112
        if (!$success) {
113
            echo $command->getOutput().' ('.$command->getExitCode().')';
114
        }
115
        $this->assertTrue($success);
116
117
        $this->assertEquals(1, $this->getConnection()->getRowCount('account'));
118
        $queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account');
119
        $expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_create.xml'))->getTable('account');
120
        $this->assertTablesEqual($expectedTable, $queryTable);
121
        $this->assertDirectoryExists(self::$homebase.'test');
122
        $this->assertUserExists('test');
123
124
        // Enable the account
125
        $this->assertTrue($this->getAdminCmd()->addArg('account:enable')->addArg('test')->execute());
126
        $queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account');
127
        $expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_enable.xml'))->getTable('account');
128
        $this->assertTablesEqual($expectedTable, $queryTable);
129
130
        // Disable the account
131
        $this->assertTrue($this->getAdminCmd()->addArg('account:disable')->addArg('test')->execute());
132
        $queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account');
133
        $expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_create.xml'))->getTable('account');
134
        $this->assertTablesEqual($expectedTable, $queryTable);
135
136
        // Rename the account
137
        $this->assertTrue($this->getAdminCmd()->addArg('account:rename')->addArg('test')->addArg('renamed')->execute());
138
        $queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account');
139
        $expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_rename.xml'))->getTable('account');
140
        $this->assertTablesEqual($expectedTable, $queryTable);
141
        $this->assertDirectoryExists(self::$homebase.'renamed');
142
        $this->assertDirectoryNotExists(self::$homebase.'test');
143
        $this->assertUserExists('renamed');
144
        $this->assertUserNotExists('tests');
145
146
        // Delete the account
147
        $this->assertTrue($this->getAdminCmd()->addArg('account:delete')->addArg('renamed')->execute());
148
        $this->assertEquals(0, $this->getConnection()->getRowCount('account'));
149
        $this->assertDirectoryExists(self::$homebase.'renamed');
150
        $this->assertUserNotExists('renamed');
151
152
        // Register home directory for removal
153
        self::$tmpDirectories[] = self::$homebase.'renamed';
154
    }
155
156
    /**
157
     * Returns the test dataset
158
     *
159
     * @return \PHPUnit_Extensions_Database_DataSet_IDataSet
160
     */
161
    protected function getDataSet()
162
    {
163
        return $this->createMySQLXMLDataSet(
164
            __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'admin_test_cli.xml'
165
        );
166
    }
167
168
    /**
169
     * Create and return an admin base command
170
     *
171
     * @return Command Admin base command
172
     */
173
    protected function getAdminCmd()
174
    {
175
        $command = new Command();
176
        $command->setCommand(Binary::get('php'));
177
        $command->addArg(self::$admincli);
178
        return $command;
179
    }
180
181
    /**
182
     * Return the absolute path to a fixture file
183
     *
184
     * @param string $fixture Fixture file name
185
     * @return string Absolute fixture file
186
     */
187
    protected function getFixture($fixture)
188
    {
189
        return __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.$fixture;
190
    }
191
192
    /**
193
     * Assert that a system user exists
194
     *
195
     * @param string $user User name
196
     */
197
    protected function assertUserExists($user)
198
    {
199
        $command = new Command();
200
        $command->setCommand(Binary::get('id'));
201
        $command->addArg($user);
202
        return $command->execute();
203
    }
204
205
    /**
206
     * Assert that a system user doesn't exist
207
     *
208
     * @param string $user User name
209
     */
210
    protected function assertUserNotExists($user)
211
    {
212
        $command = new Command();
213
        $command->setCommand(Binary::get('id'));
214
        $command->addArg($user);
215
        return !$command->execute();
216
    }
217
}
218