SourceHandlerTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
dl 0
loc 217
rs 10
c 1
b 0
f 0
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testInstantiation() 0 7 1
A setUp() 0 54 2
A testAddSourceFiles() 0 6 1
A testExcludeMatchingPattern() 0 12 1
A testGetFiles() 0 22 1
A testExcludeMatchingPCRE() 0 12 1
A testGetFilesWithIssues() 0 8 1
A testGetCommonPathPrefix() 0 5 1
A testAddSourceFilesWithNonExisting() 0 6 1
1
<?php
2
3
/**
4
 * Test case
5
 *
6
 * Copyright (c) 2007-2010, Mayflower GmbH
7
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 *
13
 *   * Redistributions of source code must retain the above copyright
14
 *     notice, this list of conditions and the following disclaimer.
15
 *
16
 *   * Redistributions in binary form must reproduce the above copyright
17
 *     notice, this list of conditions and the following disclaimer in
18
 *     the documentation and/or other materials provided with the
19
 *     distribution.
20
 *
21
 *   * Neither the name of Mayflower GmbH nor the names of his
22
 *     contributors may be used to endorse or promote products derived
23
 *     from this software without specific prior written permission.
24
 *
25
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
 * POSSIBILITY OF SUCH DAMAGE.
37
 *
38
 * @category PHP_CodeBrowser
39
 *
40
 * @author Simon Kohlmeyer <[email protected]
41
 *
42
 * @copyright 2007-2010 Mayflower GmbH
43
 *
44
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
45
 *
46
 * @version SVN: $Id$
47
 *
48
 * @link http://www.phpunit.de/
49
 *
50
 * @since File available since  0.1.0
51
 */
52
53
namespace PHPCodeBrowser\Tests;
54
55
use Exception;
56
use Monolog\Handler\NullHandler;
57
use Monolog\Logger;
58
use PHPCodeBrowser\File;
59
use PHPCodeBrowser\Issue;
60
use PHPCodeBrowser\IssueXML;
61
use PHPCodeBrowser\Plugins\ErrorCheckstyle;
62
use PHPCodeBrowser\Plugins\ErrorPMD;
63
use PHPCodeBrowser\SourceHandler;
64
use SplFileInfo;
65
66
/**
67
 * SourceHandlerTest
68
 *
69
 * @category PHP_CodeBrowser
70
 *
71
 * @author Simon Kohlmeyer <[email protected]>
72
 *
73
 * @copyright 2007-2010 Mayflower GmbH
74
 *
75
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
76
 *
77
 * @version Release: @package_version@
78
 *
79
 * @link http://www.phpunit.de/
80
 *
81
 * @since Class available since  0.1.0
82
 */
83
class SourceHandlerTest extends AbstractTestCase
84
{
85
    /**
86
     * SourceHandler object to test
87
     *
88
     * @var SourceHandler
89
     */
90
    protected $sourceHandler;
91
92
    /**
93
     * Plugin array populated with example files.
94
     * TODO: Mock this
95
     *
96
     * @var array of AbstractPlugin
97
     */
98
    protected $plugins;
99
100
    /**
101
     * @var Logger
102
     */
103
    private $logger;
104
105
    /**
106
     * (non-PHPDoc)
107
     *
108
     * @see AbstractTests#setUp()
109
     */
110
    protected function setUp(): void
111
    {
112
        parent::setUp();
113
114
        $xmlStrings = [
115
            <<<HERE
116
<?xml version="1.0" encoding="UTF-8"?>
117
<pmd version="0.2.6" timestamp="2010-08-12T00:00:00+02:000">
118
    <file name='/a/nother/dir/src.php'>
119
        <violation beginline="291" endline="291" rule="ExitExpression"
120
            ruleset="Design Rules"
121
            externalInfoUrl="http://example.com" priority="1">descr</violation>
122
    </file>
123
</pmd>
124
HERE
125
            ,
126
            <<<HERE
127
<?xml version="1.0" encoding="UTF-8"?>
128
<checkstyle version="1.2.0RC3">
129
    <file name="/a/dir/source.php">
130
        <error line="37" column="1" severity="error"
131
            message="m1" source="PEAR.Commenting.FileCommentSniff"/>
132
    </file>
133
    <file name="/a/nother/dir/src.php">
134
        <error line="39" column="1" severity="error"
135
            message="m3" source="PEAR.Commenting.FileCommentSniff"/>
136
        <error line="40" column="1" severity="error"
137
            message="m4" source="PEAR.Commenting.FileCommentSniff"/>
138
    </file>
139
</checkstyle>
140
HERE
141
            ,
142
        ];
143
        $issueXML   = new IssueXML();
144
145
        foreach ($xmlStrings as $xmlString) {
146
            $xml                  = new \DOMDocument('1.0', 'UTF-8');
147
            $xml->validateOnParse = true;
148
            $xml->loadXML($xmlString);
149
            $issueXML->addXMLFile($xml);
150
        }
151
152
        $this->plugins = [
153
            new ErrorCheckstyle($issueXML),
154
            new ErrorPMD($issueXML),
155
        ];
156
157
        $this->logger = new Logger('PHPCodeBrowser');
158
        $this->logger->pushHandler(new NullHandler());
159
160
        $this->sourceHandler = new SourceHandler($this->logger);
161
        \array_walk(
162
            $this->plugins,
163
            [$this->sourceHandler, 'addPlugin']
164
        );
165
    }
166
167
    /**
168
     * Test the constructor.
169
     *
170
     * @return void
171
     */
172
    public function testInstantiation(): void
173
    {
174
        $sourceHandler = new SourceHandler(
175
            $this->logger,
176
            $this->plugins
177
        );
178
        static::assertEquals($this->sourceHandler, $sourceHandler);
179
    }
180
181
    /**
182
     * Test getFiles.
183
     *
184
     * @return void
185
     */
186
    public function testGetFiles(): void
187
    {
188
        $expected = [
189
            '/a/nother/dir/src.php' => new File(
190
                '/a/nother/dir/src.php',
191
                [
192
                    new Issue('/a/nother/dir/src.php', 39, 39, 'Checkstyle', 'm3', 'error'),
193
                    new Issue('/a/nother/dir/src.php', 40, 40, 'Checkstyle', 'm4', 'error'),
194
                    new Issue('/a/nother/dir/src.php', 291, 291, 'PMD', 'descr', 'error'),
195
                ]
196
            ),
197
            '/a/dir/source.php'     => new File(
198
                '/a/dir/source.php',
199
                [
200
                    new Issue('/a/dir/source.php', 37, 37, 'Checkstyle', 'm1', 'error'),
201
                ]
202
            ),
203
        ];
204
        File::sort($expected);
205
206
        $actual = $this->sourceHandler->getFiles();
207
        static::assertEquals($expected, $actual);
208
    }
209
210
    /**
211
     * Test getFilesWithIssues
212
     *
213
     * @return void
214
     */
215
    public function testGetFilesWithIssues(): void
216
    {
217
        $expectedFiles = [
218
            '/a/dir/source.php',
219
            '/a/nother/dir/src.php',
220
        ];
221
        $actualFiles   = $this->sourceHandler->getFilesWithIssues();
222
        static::assertEquals($expectedFiles, $actualFiles);
223
    }
224
225
    /**
226
     * Test addSourceFiles
227
     *
228
     * @return void
229
     */
230
    public function testAddSourceFiles(): void
231
    {
232
        $this->sourceHandler->addSourceFiles(
233
            [new SplFileInfo(__FILE__), __FILE__]
234
        );
235
        static::assertContains(__FILE__, \array_keys($this->sourceHandler->getFiles()));
236
    }
237
238
    /**
239
     * Test if addSourceFile chokes on non-existent files.
240
     *
241
     * @return void
242
     */
243
    public function testAddSourceFilesWithNonExisting(): void
244
    {
245
        $this->expectException(Exception::class);
246
247
        $this->sourceHandler->addSourceFiles(
248
            [new SplFileInfo('/i/do/not/exist')]
249
        );
250
    }
251
252
    /**
253
     * Test getCommonPathPrefix
254
     *
255
     * @return void
256
     */
257
    public function testGetCommonPathPrefix(): void
258
    {
259
        $expected = '/a/';
260
        $actual   = $this->sourceHandler->getCommonPathPrefix();
261
        static::assertEquals($expected, $actual);
262
    }
263
264
    /**
265
     * Test excludeMatchingPCRE
266
     *
267
     * @return void
268
     */
269
    public function testExcludeMatchingPCRE(): void
270
    {
271
        $expected = [
272
            '/a/dir/source.php' => new File(
273
                '/a/dir/source.php',
274
                [
275
                    new Issue('/a/dir/source.php', 37, 37, 'Checkstyle', 'm1', 'error'),
276
                ]
277
            ),
278
        ];
279
        $this->sourceHandler->excludeMatchingPCRE('/^\/a.*src\.php$/');
280
        static::assertEquals($expected, $this->sourceHandler->getFiles());
281
    }
282
283
    /**
284
     * Test excludeMatchingPattern
285
     *
286
     * @return void
287
     */
288
    public function testExcludeMatchingPattern(): void
289
    {
290
        $expected = [
291
            '/a/dir/source.php' => new File(
292
                '/a/dir/source.php',
293
                [
294
                    new Issue('/a/dir/source.php', 37, 37, 'Checkstyle', 'm1', 'error'),
295
                ]
296
            ),
297
        ];
298
        $this->sourceHandler->excludeMatchingPattern('*src.php');
299
        static::assertEquals($expected, $this->sourceHandler->getFiles());
300
    }
301
}
302