Passed
Push — master ( baff11...71ae2f )
by Michael
04:40
created

testRunCleansExistingOutputFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Test case
4
 *
5
 * Copyright (c) 2007-2010, Mayflower GmbH
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 *   * Redistributions of source code must retain the above copyright
13
 *     notice, this list of conditions and the following disclaimer.
14
 *
15
 *   * Redistributions in binary form must reproduce the above copyright
16
 *     notice, this list of conditions and the following disclaimer in
17
 *     the documentation and/or other materials provided with the
18
 *     distribution.
19
 *
20
 *   * Neither the name of Mayflower GmbH nor the names of his
21
 *     contributors may be used to endorse or promote products derived
22
 *     from this software without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
 * POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 * @category   PHP_CodeBrowser
38
 *
39
 * @author     Simon Kohlmeyer <[email protected]
40
 *
41
 * @copyright  2007-2010 Mayflower GmbH
42
 *
43
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
44
 *
45
 * @version    SVN: $Id$
46
 *
47
 * @link       http://www.phpunit.de/
48
 *
49
 * @since      File available since  0.1.0
50
 */
51
52
53
namespace PHPCodeBrowser\Tests;
54
55
use Monolog\Logger;
56
use PHPCodeBrowser\CLIController;
57
use PHPCodeBrowser\Helper\IOHelper;
58
use PHPCodeBrowser\Plugins\ErrorCRAP;
59
60
/**
61
 * CLIControllerTest
62
 *
63
 * @category   PHP_CodeBrowser
64
 *
65
 * @author     Simon Kohlmeyer <[email protected]>
66
 *
67
 * @copyright  2007-2010 Mayflower GmbH
68
 *
69
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
70
 *
71
 * @version    Release: @package_version@
72
 *
73
 * @link       http://www.phpunit.de/
74
 *
75
 * @since      Class available since  0.1.0
76
 */
77
class CLIControllerTest extends AbstractTestCase
78
{
79
    /**
80
     * @var CLIController $controller
81
     */
82
    public $controller;
83
84
    /**
85
     * Create and configure a CLIController instance.
86
     */
87
    public function setUp(): void
88
    {
89
        parent::setUp();
90
91
        $this->controller = new CLIController(
92
            null,
93
            [self::$phpcbSourceDir],
94
            self::$testOutputDir,
95
            [],
96
            [],
97
            [ErrorCRAP::class => ['threshold' => 1]],
98
            new IOHelper(),
99
            new Logger('PHPCodeBrowser'),
100
            ['php']
101
        );
102
103
        $this->controller->addErrorPlugins(
104
            [
105
                'ErrorCheckstyle',
106
                'ErrorPMD',
107
                'ErrorCPD',
108
                'ErrorPadawan',
109
                'ErrorCoverage',
110
                'ErrorCRAP',
111
            ]
112
        );
113
    }
114
115
    /**
116
     * Assert a set of directories and files are present in output dir.
117
     */
118
    public function assertOutputIsPresent(): void
119
    {
120
        self::assertFileExists(self::$testOutputDir.'/index.html');
121
        self::assertFileExists(self::$testOutputDir.'/Bad.php.html');
122
        self::assertFileExists(self::$testOutputDir.'/Good.php.html');
123
        self::assertDirectoryExists(self::$testOutputDir.'/css');
124
        self::assertDirectoryExists(self::$testOutputDir.'/img');
125
        self::assertDirectoryExists(self::$testOutputDir.'/js');
126
    }
127
128
    /**
129
     * Run a full system test based on phpcs output.
130
     */
131
    public function testRunCreatesFilesAndDirs(): void
132
    {
133
        $this->controller->run();
134
135
        $this->assertOutputIsPresent();
136
    }
137
138
    /**
139
     * Assert existing files and directories within output dir are removed.
140
     */
141
    public function testRunCleansExistingOutputDir(): void
142
    {
143
        mkdir(self::$testOutputDir.'/clear-directory');
144
        touch(self::$testOutputDir.'/clear-file');
145
        touch(self::$testOutputDir.'/clear-directory/clear-file');
146
147
        $this->controller->run();
148
149
        $this->assertOutputIsPresent();
150
        $this->assertDirectoryNotExists(self::$testOutputDir.'/clear-directory');
151
        $this->assertFileNotExists(self::$testOutputDir.'/clear-file');
152
    }
153
154
    /**
155
     * Assert that if there is a file with outputs name, it is replaced with a directory.
156
     */
157
    public function testRunCleansExistingOutputFile(): void
158
    {
159
        rmdir(self::$testOutputDir);
160
        touch(self::$testOutputDir);
161
162
        $this->controller->run();
163
164
        $this->assertOutputIsPresent();
165
    }
166
167
    /**
168
     * Assert only index.html is present if all source files are excluded.
169
     */
170
    public function testRunExcludingAllSources(): void
171
    {
172
        $this->controller = new CLIController(
173
            null,
174
            [self::$phpcbSourceDir],
175
            self::$testOutputDir,
176
            ['/Bad.php/'],
177
            ['*Good.php'],
178
            [ErrorCRAP::class => ['threshold' => 1]],
179
            new IOHelper(),
180
            new Logger('PHPCodeBrowser'),
181
            ['php']
182
        );
183
184
        $this->controller->run();
185
186
        $this->assertFileExists(self::$testOutputDir.'/index.html');
187
        $this->assertFileNotExists(self::$testOutputDir.'/Bad.php.html');
188
        $this->assertFileNotExists(self::$testOutputDir.'/Good.php.html');
189
        $this->assertDirectoryNotExists(self::$testOutputDir.'/css');
190
        $this->assertDirectoryNotExists(self::$testOutputDir.'/img');
191
        $this->assertDirectoryNotExists(self::$testOutputDir.'/js');
192
    }
193
}
194