FileTest::testSort()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 39
nc 1
nop 0
dl 0
loc 45
rs 9.296
c 0
b 0
f 0
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 PHPCodeBrowser\File;
56
use PHPCodeBrowser\Issue;
57
58
/**
59
 * FileTest
60
 *
61
 * @category PHP_CodeBrowser
62
 *
63
 * @author Simon Kohlmeyer <[email protected]>
64
 *
65
 * @copyright 2007-2010 Mayflower GmbH
66
 *
67
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
68
 *
69
 * @version Release: @package_version@
70
 *
71
 * @link http://www.phpunit.de/
72
 *
73
 * @since Class available since  0.1.0
74
 */
75
class FileTest extends AbstractTestCase
76
{
77
    /**
78
     * File object to test
79
     *
80
     * @var File
81
     */
82
    protected $file;
83
84
    /**
85
     * Some issues to work with.
86
     *
87
     * @var array<Issue>
88
     */
89
    protected $issues;
90
91
    /**
92
     * (non-PHPDoc)
93
     *
94
     * @see AbstractTests#setUp()
95
     */
96
    protected function setUp(): void
97
    {
98
        parent::setUp();
99
        $this->issues = [
100
            new Issue('/some/file/name.php', 39, 39, 'Checkstyle', 'm3', 'error'),
101
            new Issue('/some/file/name.php', 50, 52, 'Checkstyle', 'm4', 'warning'),
102
            new Issue('/some/file/name.php', 40, 40, 'Checkstyle', 'm4', 'error'),
103
        ];
104
        $this->file   = new File('/some/file/name.php');
105
    }
106
107
    /**
108
     * Test constructor if variables are stored properly
109
     *
110
     * @return void
111
     */
112
    public function testInstantiation(): void
113
    {
114
        static::assertEquals('/some/file/name.php', $this->file->name());
115
116
        $this->file = new File('/some/file/name.php', $this->issues);
117
118
        static::assertEquals('/some/file/name.php', $this->file->name());
119
        static::assertEquals($this->issues, $this->file->getIssues());
120
    }
121
122
    /**
123
     * Test if adding issues works.
124
     *
125
     * @return void
126
     */
127
    public function testIssueAdding(): void
128
    {
129
        $this->file->addIssue($this->issues[0]);
130
        static::assertEquals(
131
            [$this->issues[0]],
132
            $this->file->getIssues()
133
        );
134
    }
135
136
    /**
137
     * Tries to add invalid issue to file.
138
     *
139
     * @return void
140
     */
141
    public function testAddingIssueToWrongFile(): void
142
    {
143
        $this->expectException(\InvalidArgumentException::class);
144
145
        $issue = new Issue('/the/wrong/file/name.php', 39, 39, 'Checkstyle', 'm3', 'error');
146
        $this->file->addIssue($issue);
147
    }
148
149
    /**
150
     * Test the basename function
151
     *
152
     * @return void
153
     */
154
    public function testBasename(): void
155
    {
156
        static::assertEquals('name.php', $this->file->basename());
157
    }
158
159
    /**
160
     * Test the dirName function
161
     *
162
     * @return void
163
     */
164
    public function testDirName(): void
165
    {
166
        static::assertEquals('/some/file', $this->file->dirName());
167
    }
168
169
    /**
170
     * Test if the issue count is returned correctly
171
     *
172
     * @return void
173
     */
174
    public function testIssueCount(): void
175
    {
176
        static::assertEquals(0, $this->file->getIssueCount());
177
178
        $this->file->addIssue($this->issues[0]);
179
        static::assertEquals(1, $this->file->getIssueCount());
180
181
        $this->file = new File(
182
            '/some/file/name.php',
183
            [$this->issues[0]]
184
        );
185
        static::assertEquals(1, $this->file->getIssueCount());
186
187
        $this->file->addIssue($this->issues[1]);
188
        static::assertEquals(2, $this->file->getIssueCount());
189
    }
190
191
    /**
192
     * Test the errorCount function
193
     *
194
     * @return void
195
     */
196
    public function testErrorCount(): void
197
    {
198
        $this->file = new File('/some/file/name.php', $this->issues);
199
        static::assertEquals(2, $this->file->getErrorCount());
200
    }
201
202
    /**
203
     * Test the warningCount function
204
     *
205
     * @return void
206
     */
207
    public function testEarningCount(): void
208
    {
209
        $this->file = new File('/some/file/name.php', $this->issues);
210
        static::assertEquals(1, $this->file->getWarningCount());
211
    }
212
213
    /**
214
     * Test the mergeWith function
215
     *
216
     * @return void
217
     */
218
    public function testMergeWith(): void
219
    {
220
        $this->file = new File(
221
            '/some/file/name.php',
222
            [$this->issues[0], $this->issues[1]]
223
        );
224
        $otherFile  = new File(
225
            '/some/file/name.php',
226
            [$this->issues[2]]
227
        );
228
        $this->file->mergeWith($otherFile);
229
230
        static::assertEquals(2, $this->file->getErrorCount());
231
        static::assertEquals(1, $this->file->getWarningCount());
232
        static::assertEquals(
233
            \array_values($this->issues),
234
            \array_values($this->file->getIssues())
235
        );
236
    }
237
238
    /**
239
     * Try to merge with a non-compatible file.
240
     *
241
     * @return void
242
     */
243
    public function testMergeWithDifferentFile(): void
244
    {
245
        $this->expectException(\InvalidArgumentException::class);
246
247
        $this->file->mergeWith(new File('/the/wrong/file/name.php'));
248
    }
249
250
    /**
251
     * Test the sort function.
252
     *
253
     * @return void
254
     */
255
    public function testSort(): void
256
    {
257
        $sorted = [
258
            new File('src/Helper/IOHelper.php'),
259
            new File('src/Plugins/ErrorCPD.php'),
260
            new File('src/Plugins/ErrorCheckstyle.php'),
261
            new File('src/Plugins/ErrorCoverage.php'),
262
            new File('src/Plugins/ErrorPMD.php'),
263
            new File('src/Plugins/ErrorPadawan.php'),
264
            new File('src/Util/Autoloader.php'),
265
            new File('src/Util/Logger.php'),
266
            new File('src/View/ViewAbstract.php'),
267
            new File('src/View/ViewReview.php'),
268
            new File('src/AbstractPlugin.php'),
269
            new File('src/CLIController.php'),
270
            new File('src/File.php'),
271
            new File('src/Issue.php'),
272
            new File('src/IssueXML.php'),
273
            new File('src/SourceHandler.php'),
274
            new File('src/SourceIterator.php'),
275
        ];
276
277
        $mixed = [
278
            new File('src/AbstractPlugin.php'),
279
            new File('src/Plugins/ErrorCheckstyle.php'),
280
            new File('src/CLIController.php'),
281
            new File('src/Plugins/ErrorPadawan.php'),
282
            new File('src/SourceIterator.php'),
283
            new File('src/SourceHandler.php'),
284
            new File('src/Issue.php'),
285
            new File('src/View/ViewReview.php'),
286
            new File('src/File.php'),
287
            new File('src/Util/Autoloader.php'),
288
            new File('src/Helper/IOHelper.php'),
289
            new File('src/IssueXML.php'),
290
            new File('src/Plugins/ErrorCoverage.php'),
291
            new File('src/View/ViewAbstract.php'),
292
            new File('src/Util/Logger.php'),
293
            new File('src/Plugins/ErrorPMD.php'),
294
            new File('src/Plugins/ErrorCPD.php'),
295
        ];
296
297
        File::sort($mixed);
298
        $mixed = \array_values($mixed);
299
        static::assertEquals($sorted, $mixed);
300
    }
301
}
302