Completed
Push — dev ( 94208d...ada7da )
by James Ekow Abaka
02:35
created

InitTest::testInterractive()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 2
nop 0
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
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 clearice\io\Io;
30
use \org\bovigo\vfs\vfsStream;
31
use yentu\commands\Init;
32
use yentu\tests\YentuTest;
33
34
class InitTest extends YentuTest
35
{
36
37
    public function setUp()
38
    {
39
        $this->testDatabase = 'yentu_tests';
40
        parent::setup();
41
        $this->createDb($GLOBALS['DB_NAME']);
42
        $this->connect($GLOBALS['DB_FULL_DSN']);
43
        $this->setupStreams();
44
    }
45
46
    public function testParameters()
47
    {
48
        $initCommand = new Init($this->yentu, $this->getManipulatorFactory(), $this->io, $this->config);
49
50
        ob_start();
51
        $initCommand->run(
52
            array(
53
                'driver' => $GLOBALS['DRIVER'],
54
                'host' => $GLOBALS['DB_HOST'],
55
                'dbname' => $GLOBALS['DB_NAME'],
56
                'user' => $GLOBALS['DB_USER'],
57
                'password' => $GLOBALS['DB_PASSWORD'],
58
                'file' => $GLOBALS['DB_FILE']
59
            )
60
        );
61
        $this->runAssertions();
62
    }
63
64
    private function runAssertions()
65
    {
66
        $output = ob_get_clean();
67
        $this->assertEquals(true, file_exists(vfsStream::url("home/yentu")));
68
        $this->assertEquals(true, is_dir(vfsStream::url("home/yentu")));
69
        $this->assertEquals(true, file_exists(vfsStream::url("home/yentu/config")));
70
        $this->assertEquals(true, is_dir(vfsStream::url("home/yentu/config")));
71
        $this->assertEquals(true, file_exists(vfsStream::url("home/yentu/migrations")));
72
        $this->assertEquals(true, is_dir(vfsStream::url("home/yentu/migrations")));
73
        $this->assertEquals(true, is_file(vfsStream::url('home/yentu/config/default.conf.php')));
74
75
        // provides $config below
76
        $config = require(vfsStream::url('home/yentu/config/default.conf.php'));
77
78
        $this->assertEquals(array(
79
            'driver' => $GLOBALS['DRIVER'],
80
            'host' => $GLOBALS['DB_HOST'],
81
            'port' => '',
82
            'dbname' => $GLOBALS['DB_NAME'],
83
            'user' => $GLOBALS['DB_USER'],
84
            'password' => $GLOBALS['DB_PASSWORD'],
85
            'file' => $GLOBALS['DB_FILE']
86
            ), $config['db']);
87
88
        $this->assertTableExists('yentu_history');
89
        $this->assertColumnExists('session', 'yentu_history');
90
        $this->assertColumnExists('version', 'yentu_history');
91
        $this->assertColumnExists('method', 'yentu_history');
92
        $this->assertColumnExists('arguments', 'yentu_history');
93
        $this->assertColumnExists('migration', 'yentu_history');
94
        $this->assertColumnExists('id', 'yentu_history');
95
    }
96
97
    public function testInterractive()
98
    {
99
        $this->io->setStreamUrl('error', vfsStream::url("home/error.out"));
100
        $this->io->setStreamUrl('output', vfsStream::url("home/standard.out"));
101
        $this->io->setStreamUrl('input', vfsStream::url("home/responses.in"));
102
103
        // Write the input for the interractive test
104
        if (getenv('YENTU_HOST') === false) {
105
            file_put_contents(vfsStream::url("home/responses.in"), "{$GLOBALS['DRIVER']}\n"
106
                . "{$GLOBALS['DB_FILE']}\n"
107
            );
108
        } else {
109
            file_put_contents(vfsStream::url("home/responses.in"), "{$GLOBALS['DRIVER']}\n"
110
                . "{$GLOBALS['DB_HOST']}\n"
111
                . "\n"
112
                . "{$GLOBALS['DB_NAME']}\n"
113
                . "{$GLOBALS['DB_USER']}\n"
114
                . "{$GLOBALS['DB_PASSWORD']}\n"
115
            );
116
        }
117
118
        $initCommand = new Init($this->yentu, $this->getManipulatorFactory(), $this->io, $this->config);
119
        $this->yentu->setDefaultHome(vfsStream::url('home/yentu'));
120
        ob_start();
121
        $initCommand->run(array(
122
            'interractive' => true
123
        ));
124
        $this->runAssertions();
125
    }
126
127
    /**
128
     * @expectedException \yentu\exceptions\CommandException
129
     */
130
    public function testUnwritable()
131
    {
132
        vfsStream::setup('home', 0444);
133
        $initCommand = new Init($this->yentu, $this->getManipulatorFactory(), $this->io, $this->config);
134
        $this->yentu->setDefaultHome(vfsStream::url("home/yentu"));
135
        $this->io->setOutputLevel(Io::OUTPUT_LEVEL_0);
136
        $initCommand->run(
137
            array(
138
                'driver' => 'postgresql',
139
                'host' => $GLOBALS['DB_HOST'],
140
                'dbname' => $GLOBALS['DB_NAME'],
141
                'user' => $GLOBALS['DB_USER'],
142
                'password' => $GLOBALS['DB_PASSWORD']
143
            )
144
        );
145
    }
146
147
    /**
148
     * @expectedException \yentu\exceptions\CommandException
149
     */
150
    public function testExistingDir()
151
    {
152
        mkdir(vfsStream::url('home/yentu'));
153
        $initCommand = new Init($this->yentu, $this->getManipulatorFactory(), $this->io, $this->config);
154
        $this->yentu->setDefaultHome(vfsStream::url("home/yentu"));
155
        $initCommand->run(
156
            array(
157
                'driver' => 'postgresql',
158
                'host' => $GLOBALS['DB_HOST'],
159
                'dbname' => $GLOBALS['DB_NAME'],
160
                'user' => $GLOBALS['DB_USER'],
161
                'password' => $GLOBALS['DB_PASSWORD']
162
            )
163
        );
164
    }
165
166
    /**
167
     * @expectedException \yentu\exceptions\CommandException
168
     */
169
    public function testNoParams()
170
    {
171
        $initCommand = new Init($this->yentu, $this->getManipulatorFactory(), $this->io, $this->config);
172
        $this->yentu->setDefaultHome(vfsStream::url("home/yentu"));
173
        $initCommand->run(array());
174
    }
175
176
    /**
177
     * @expectedException \yentu\exceptions\CommandException
178
     */
179
    public function testExistingDb()
180
    {
181
        $this->pdo->query('CREATE TABLE yentu_history(dummy INTEGER)');
182
        $initCommand = new Init($this->yentu, $this->getManipulatorFactory(), $this->io, $this->config);
183
        $this->yentu->setDefaultHome(vfsStream::url("home/yentu"));
184
        $initCommand->run(
185
            array(
186
                'driver' => $GLOBALS['DRIVER'],
187
                'host' => $GLOBALS['DB_HOST'],
188
                'dbname' => $GLOBALS['DB_NAME'],
189
                'user' => $GLOBALS['DB_USER'],
190
                'password' => $GLOBALS['DB_PASSWORD'],
191
                'file' => $GLOBALS['DB_FILE']
192
            )
193
        );
194
    }
195
196
}
197