Completed
Push — master ( 84bbbc...a3911e )
by Joschi
03:15
created

CliTest::testAccount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 9.125
c 0
b 0
f 0
cc 2
eloc 33
nc 2
nop 0
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
        $command = $this->getAdminCmd()->addArg('account:create')->addArg('test');
130
        $success = $command->execute();
131
        $this->assertTrue($success);
132
        if (!$success) {
133
            echo $command->getOutput().' ('.$command->getExitCode().')';
134
        }
135
136
137
        $this->assertEquals(1, $this->getConnection()->getRowCount('account'));
138
        $queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account');
139
        $expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_create.xml'))->getTable('account');
140
        $this->assertTablesEqual($expectedTable, $queryTable);
141
        $this->assertDirectoryExists(self::$homebase.'test');
142
        $this->assertUserExists('test');
143
144
        // Enable the account
145
        $this->assertTrue($this->getAdminCmd()->addArg('account:enable')->addArg('test')->execute());
146
        $queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account');
147
        $expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_enable.xml'))->getTable('account');
148
        $this->assertTablesEqual($expectedTable, $queryTable);
149
150
        // Disable the account
151
        $this->assertTrue($this->getAdminCmd()->addArg('account:disable')->addArg('test')->execute());
152
        $queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account');
153
        $expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_create.xml'))->getTable('account');
154
        $this->assertTablesEqual($expectedTable, $queryTable);
155
156
        // Rename the account
157
        $this->assertTrue($this->getAdminCmd()->addArg('account:rename')->addArg('test')->addArg('renamed')->execute());
158
        $queryTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account');
159
        $expectedTable = $this->createFlatXMLDataSet($this->getFixture('account_rename.xml'))->getTable('account');
160
        $this->assertTablesEqual($expectedTable, $queryTable);
161
        $this->assertDirectoryExists(self::$homebase.'renamed');
162
        $this->assertDirectoryNotExists(self::$homebase.'test');
163
        $this->assertUserExists('renamed');
164
        $this->assertUserNotExists('tests');
165
166
        // Delete the account
167
        $this->assertTrue($this->getAdminCmd()->addArg('account:delete')->addArg('renamed')->execute());
168
        $this->assertEquals(0, $this->getConnection()->getRowCount('account'));
169
        $this->assertDirectoryExists(self::$homebase.'renamed');
170
        $this->assertUserNotExists('renamed');
171
172
        // Register home directory for removal
173
        self::$tmpDirectories[] = self::$homebase.'renamed';
174
    }
175
176
    /**
177
     * Returns the test dataset
178
     *
179
     * @return \PHPUnit_Extensions_Database_DataSet_IDataSet
180
     */
181
    protected function getDataSet()
182
    {
183
        return $this->createMySQLXMLDataSet(
184
            __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'admin_test_cli.xml'
185
        );
186
    }
187
188
    /**
189
     * Create and return an admin base command
190
     *
191
     * @return Command Admin base command
192
     */
193
    protected function getAdminCmd()
194
    {
195
        $command = new Command();
196
        $command->setCommand(self::$phpcmd);
197
        $command->addArg(self::$admincli);
198
        return $command;
199
    }
200
201
    /**
202
     * Return the absolute path to a fixture file
203
     *
204
     * @param string $fixture Fixture file name
205
     * @return string Absolute fixture file
206
     */
207
    protected function getFixture($fixture)
208
    {
209
        return __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.$fixture;
210
    }
211
212
    /**
213
     * Recursively delete a directory
214
     *
215
     * @param string $dir Directory
216
     * @return bool Success
217
     */
218
    protected function deleteTree($dir)
219
    {
220
        foreach (array_diff(scandir($dir), array('.', '..')) as $file) {
221
            if (is_dir("$dir/$file")) {
222
                $this->deleteTree("$dir/$file");
223
                continue;
224
            }
225
            unlink("$dir/$file");
226
        }
227
        return rmdir($dir);
228
    }
229
230
    /**
231
     * Assert that a system user exists
232
     *
233
     * @param string $user User name
234
     */
235
    protected function assertUserExists($user)
236
    {
237
        $command = new Command();
238
        $command->setCommand(self::$idcmd);
239
        $command->addArg($user);
240
        return $command->execute();
241
    }
242
243
    /**
244
     * Assert that a system user doesn't exist
245
     *
246
     * @param string $user User name
247
     */
248
    protected function assertUserNotExists($user)
249
    {
250
        $command = new Command();
251
        $command->setCommand(self::$idcmd);
252
        $command->addArg($user);
253
        return !$command->execute();
254
    }
255
}
256