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\View; |
54
|
|
|
|
55
|
|
|
use PHPCodeBrowser\File; |
56
|
|
|
use PHPCodeBrowser\Helper\IOHelper; |
57
|
|
|
use PHPCodeBrowser\Issue; |
58
|
|
|
use PHPCodeBrowser\Tests\AbstractTestCase; |
59
|
|
|
use PHPCodeBrowser\View\ViewReview; |
60
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* ViewReviewTest |
64
|
|
|
* |
65
|
|
|
* @category PHP_CodeBrowser |
66
|
|
|
* |
67
|
|
|
* @author Simon Kohlmeyer <[email protected]> |
68
|
|
|
* |
69
|
|
|
* @copyright 2007-2010 Mayflower GmbH |
70
|
|
|
* |
71
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
72
|
|
|
* |
73
|
|
|
* @version Release: @package_version@ |
74
|
|
|
* |
75
|
|
|
* @link http://www.phpunit.de/ |
76
|
|
|
* |
77
|
|
|
* @since Class available since 0.1.0 |
78
|
|
|
*/ |
79
|
|
|
class ViewReviewTest extends AbstractTestCase |
80
|
|
|
{ |
81
|
|
|
/** |
82
|
|
|
* The ViewReview object to test. |
83
|
|
|
* |
84
|
|
|
* @var ViewReview |
85
|
|
|
*/ |
86
|
|
|
protected $viewReview; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* IOHelper mock to simulate filesystem interaction. |
90
|
|
|
* |
91
|
|
|
* @var MockObject |
92
|
|
|
*/ |
93
|
|
|
protected $ioMock; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* (non-PHPDoc) |
97
|
|
|
* |
98
|
|
|
* @see tests/cbAbstractTests#setUp() |
99
|
|
|
*/ |
100
|
|
|
protected function setUp(): void |
101
|
|
|
{ |
102
|
|
|
parent::setUp(); |
103
|
|
|
|
104
|
|
|
$this->ioMock = $this->createMock(IOHelper::class); |
105
|
|
|
|
106
|
|
|
$this->viewReview = new ViewReview( |
107
|
|
|
\getenv('PHPCB_TEMPLATE_DIR') ? \getenv('PHPCB_TEMPLATE_DIR') : \dirname(__FILE__, 5).'/templates', |
108
|
|
|
self::$testOutputDir, |
109
|
|
|
$this->ioMock |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Test the generate method without any issues |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function testGenerateNoIssues(): void |
119
|
|
|
{ |
120
|
|
|
$expectedFile = self::$testOutputDir.DIRECTORY_SEPARATOR.\basename(__FILE__).'.html'; |
121
|
|
|
|
122
|
|
|
$this->ioMock->expects(static::once()) |
123
|
|
|
->method('loadFile') |
124
|
|
|
->with(static::equalTo(__FILE__)) |
125
|
|
|
->willReturn(\file_get_contents(__FILE__)); |
126
|
|
|
$this->ioMock->expects(static::once()) |
127
|
|
|
->method('createFile') |
128
|
|
|
->with(static::equalTo($expectedFile)); |
129
|
|
|
|
130
|
|
|
$this->viewReview->generate( |
131
|
|
|
[], |
132
|
|
|
__FILE__, |
133
|
|
|
__DIR__.DIRECTORY_SEPARATOR |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Test the generate method with an issue |
139
|
|
|
* |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
public function testGenerate(): void |
143
|
|
|
{ |
144
|
|
|
$issueList = [ |
145
|
|
|
new Issue( |
146
|
|
|
__FILE__, |
147
|
|
|
80, |
148
|
|
|
85, |
149
|
|
|
'finder', |
150
|
|
|
'description', |
151
|
|
|
'severe' |
152
|
|
|
), |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
$expectedFile = self::$testOutputDir.DIRECTORY_SEPARATOR.\basename(__FILE__).'.html'; |
156
|
|
|
$this->ioMock->expects(static::once()) |
157
|
|
|
->method('loadFile') |
158
|
|
|
->with(static::equalTo(__FILE__)) |
159
|
|
|
->willReturn(\file_get_contents(__FILE__)); |
160
|
|
|
$this->ioMock->expects(static::once()) |
161
|
|
|
->method('createFile') |
162
|
|
|
->with(static::equalTo($expectedFile)); |
163
|
|
|
|
164
|
|
|
$this->viewReview->generate( |
165
|
|
|
$issueList, |
166
|
|
|
__FILE__, |
167
|
|
|
__DIR__.DIRECTORY_SEPARATOR |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Test the generate method with multiple errors on one line. |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
public function testGenerateMultiple(): void |
177
|
|
|
{ |
178
|
|
|
$issueList = [ |
179
|
|
|
new Issue(__FILE__, 80, 80, 'finder', 'description', 'severe'), |
180
|
|
|
new Issue(__FILE__, 80, 80, 'other finder', 'other description', 'more severe'), |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
$expectedFile = self::$testOutputDir.DIRECTORY_SEPARATOR.\basename(__FILE__).'.html'; |
184
|
|
|
$this->ioMock->expects(static::once()) |
185
|
|
|
->method('loadFile') |
186
|
|
|
->with(static::equalTo(__FILE__)) |
187
|
|
|
->willReturn(\file_get_contents(__FILE__)); |
188
|
|
|
$this->ioMock->expects(static::once()) |
189
|
|
|
->method('createFile') |
190
|
|
|
->with(static::equalTo($expectedFile)); |
191
|
|
|
|
192
|
|
|
$this->viewReview->generate( |
193
|
|
|
$issueList, |
194
|
|
|
__FILE__, |
195
|
|
|
__DIR__.DIRECTORY_SEPARATOR |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Try to test highlighting with Text_Highlighter |
201
|
|
|
* |
202
|
|
|
* @return void |
203
|
|
|
*/ |
204
|
|
|
public function testGenerateWithTextHighlighter(): void |
205
|
|
|
{ |
206
|
|
|
if (!\class_exists('Text_Highlighter')) { |
207
|
|
|
static::markTestIncomplete(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$html = <<< EOT |
211
|
|
|
<html> |
212
|
|
|
<head> |
213
|
|
|
<title>Title</title> |
214
|
|
|
</head> |
215
|
|
|
<body> |
216
|
|
|
<p>Body</p> |
217
|
|
|
</body> |
218
|
|
|
</html> |
219
|
|
|
EOT; |
220
|
|
|
$prefix = '/dir/'; |
221
|
|
|
$fileName = $prefix.'file.html'; |
222
|
|
|
|
223
|
|
|
$expectedFile = self::$testOutputDir.'/file.html.html'; |
224
|
|
|
$this->ioMock->expects(static::once()) |
225
|
|
|
->method('loadFile') |
226
|
|
|
->with(static::equalTo($fileName)) |
227
|
|
|
->willReturn($html); |
228
|
|
|
$this->ioMock->expects(static::once()) |
229
|
|
|
->method('createFile') |
230
|
|
|
->with(static::equalTo($expectedFile)); |
231
|
|
|
|
232
|
|
|
$issues = [ |
233
|
|
|
new Issue($fileName, 5, 5, 'finder', 'description', 'severity'), |
234
|
|
|
]; |
235
|
|
|
|
236
|
|
|
$this->viewReview->generate($issues, $fileName, $prefix); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Test highlighting of unknown code files. |
241
|
|
|
* |
242
|
|
|
* @return void |
243
|
|
|
*/ |
244
|
|
|
public function testGenerateUnknownType(): void |
245
|
|
|
{ |
246
|
|
|
$expectedFile = self::$testOutputDir.DIRECTORY_SEPARATOR.\basename(self::$xmlBasic).'.html'; |
247
|
|
|
|
248
|
|
|
$this->ioMock->expects(static::once()) |
249
|
|
|
->method('createFile') |
250
|
|
|
->with(static::equalTo($expectedFile)); |
251
|
|
|
|
252
|
|
|
$issueList = [ |
253
|
|
|
new Issue(self::$xmlBasic, 5, 5, 'finder', 'description', 'severity'), |
254
|
|
|
]; |
255
|
|
|
|
256
|
|
|
$this->viewReview->generate( |
257
|
|
|
$issueList, |
258
|
|
|
self::$xmlBasic, |
259
|
|
|
\dirname(self::$xmlBasic).DIRECTORY_SEPARATOR |
260
|
|
|
); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Test if the resource folders are copied. |
265
|
|
|
* |
266
|
|
|
* @return void |
267
|
|
|
*/ |
268
|
|
|
public function testCopyResourceFolders(): void |
269
|
|
|
{ |
270
|
|
|
$this->ioMock->expects(static::exactly(3)) |
271
|
|
|
->method('copyDirectory') |
272
|
|
|
->with( |
273
|
|
|
static::matchesRegularExpression( |
274
|
|
|
'|^'.\realpath(__DIR__.'/../../../templates/').'|' |
275
|
|
|
) |
276
|
|
|
); |
277
|
|
|
$this->viewReview->copyResourceFolders(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Test the generateIndex function |
282
|
|
|
* |
283
|
|
|
* @return void |
284
|
|
|
*/ |
285
|
|
|
public function testGenerateIndex(): void |
286
|
|
|
{ |
287
|
|
|
$files = [ |
288
|
|
|
's/A/somefile.php' => new File('s/A/somefile.php'), |
289
|
|
|
's/file.php' => new File('s/file.php'), |
290
|
|
|
's/B/anotherfile.php' => new File('s/B/anotherfile.php'), |
291
|
|
|
]; |
292
|
|
|
|
293
|
|
|
$this->ioMock->expects(static::once()) |
294
|
|
|
->method('createFile') |
295
|
|
|
->with( |
296
|
|
|
static::logicalAnd( |
297
|
|
|
static::stringEndsWith('index.html') |
298
|
|
|
) |
299
|
|
|
); |
300
|
|
|
$this->viewReview->generateIndex($files); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|