These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * This file is part of PHP Mess Detector. |
||
4 | * |
||
5 | * Copyright (c) Manuel Pichler <[email protected]>. |
||
6 | * All rights reserved. |
||
7 | * |
||
8 | * Licensed under BSD License |
||
9 | * For full copyright and license information, please see the LICENSE file. |
||
10 | * Redistributions of files must retain the above copyright notice. |
||
11 | * |
||
12 | * @author Lukas Bestle <[email protected]> |
||
13 | * @copyright Manuel Pichler. All rights reserved. |
||
14 | * @license https://opensource.org/licenses/bsd-license.php BSD License |
||
15 | * @link http://phpmd.org/ |
||
16 | */ |
||
17 | |||
18 | namespace PHPMD\Renderer; |
||
19 | |||
20 | use PHPMD\AbstractTest; |
||
21 | use PHPMD\ProcessingError; |
||
22 | use PHPMD\Stubs\RuleStub; |
||
23 | use PHPMD\Stubs\WriterStub; |
||
24 | |||
25 | /** |
||
26 | * Test case for the SARIF renderer implementation. |
||
27 | * |
||
28 | * @covers \PHPMD\Renderer\SARIFRenderer |
||
29 | */ |
||
30 | class SARIFRendererTest extends AbstractTest |
||
31 | { |
||
32 | /** |
||
33 | * testRendererCreatesExpectedNumberOfJsonElements |
||
34 | * |
||
35 | * @return void |
||
36 | */ |
||
37 | public function testRendererCreatesExpectedNumberOfJsonElements() |
||
38 | { |
||
39 | $writer = new WriterStub(); |
||
40 | |||
41 | $rule = new RuleStub('AnotherRuleStub'); |
||
42 | $rule->addExample(" class Example\n{\n}\n "); |
||
43 | $rule->addExample("\nclass AnotherExample\n{\n public \$var;\n}\n "); |
||
44 | $rule->setSince(null); |
||
45 | |||
46 | $complexRuleViolationMock = $this->getRuleViolationMock(getcwd() . '/src/foobar.php', 23, 42, $rule); |
||
47 | $complexRuleViolationMock |
||
48 | ->method('getArgs') |
||
49 | ->willReturn(array(123, 3.2, 'awesomeFunction()')); |
||
50 | |||
51 | $violations = array( |
||
52 | $this->getRuleViolationMock('/bar.php'), |
||
53 | $this->getRuleViolationMock('/foo.php'), |
||
54 | $complexRuleViolationMock, |
||
55 | ); |
||
56 | |||
57 | $report = $this->getReportWithNoViolation(); |
||
58 | $report->expects($this->once()) |
||
59 | ->method('getRuleViolations') |
||
60 | ->will($this->returnValue(new \ArrayIterator($violations))); |
||
61 | $report->expects($this->once()) |
||
62 | ->method('getErrors') |
||
63 | ->will($this->returnValue(new \ArrayIterator(array()))); |
||
64 | |||
65 | $renderer = new SARIFRenderer(); |
||
66 | $renderer->setWriter($writer); |
||
67 | |||
68 | $renderer->start(); |
||
69 | $renderer->renderReport($report); |
||
70 | $renderer->end(); |
||
71 | |||
72 | $this->assertJsonEquals( |
||
73 | $writer->getData(), |
||
74 | 'renderer/sarif_renderer_expected.sarif', |
||
75 | View Code Duplication | function ($actual) { |
|
0 ignored issues
–
show
|
|||
76 | $actual['runs'][0]['tool']['driver']['version'] = '@package_version@'; |
||
77 | return $actual; |
||
78 | } |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * testRendererAddsProcessingErrorsToJsonReport |
||
84 | * |
||
85 | * @return void |
||
86 | */ |
||
87 | public function testRendererAddsProcessingErrorsToJsonReport() |
||
88 | { |
||
89 | $writer = new WriterStub(); |
||
90 | |||
91 | $processingErrors = array( |
||
92 | new ProcessingError('Failed for file "/tmp/foo.php".'), |
||
93 | new ProcessingError('Failed for file "/tmp/bar.php".'), |
||
94 | new ProcessingError('Failed for file "' . static::createFileUri('foobar.php') . '".'), |
||
95 | new ProcessingError('Cannot read file "/tmp/foo.php". Permission denied.'), |
||
96 | ); |
||
97 | |||
98 | $report = $this->getReportWithNoViolation(); |
||
99 | $report->expects($this->once()) |
||
100 | ->method('getRuleViolations') |
||
101 | ->will($this->returnValue(new \ArrayIterator(array()))); |
||
102 | $report->expects($this->once()) |
||
103 | ->method('getErrors') |
||
104 | ->will($this->returnValue(new \ArrayIterator($processingErrors))); |
||
105 | |||
106 | $renderer = new SARIFRenderer(); |
||
107 | $renderer->setWriter($writer); |
||
108 | |||
109 | $renderer->start(); |
||
110 | $renderer->renderReport($report); |
||
111 | $renderer->end(); |
||
112 | |||
113 | $this->assertJsonEquals( |
||
114 | $writer->getData(), |
||
115 | 'renderer/sarif_renderer_processing_errors.sarif', |
||
116 | View Code Duplication | function ($actual) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
117 | $actual['runs'][0]['tool']['driver']['version'] = '@package_version@'; |
||
118 | return $actual; |
||
119 | } |
||
120 | ); |
||
121 | } |
||
122 | } |
||
123 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.