Test Failed
Pull Request — master (#132)
by Alessandro
03:15
created

LogPrinterTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 273
Duplicated Lines 54.95 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 150
loc 273
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testStartTestSuite() 0 6 1
A testAddError() 0 28 1
A testAddWarning() 30 30 1
A testAddFailure() 30 30 1
A testAddIncompleteTest() 30 30 1
A testAddRiskyTest() 30 30 1
A testAddSkippedTest() 30 30 1
A testEndTest() 0 31 1
A createPrinterAndStartTestSuite() 0 15 1
A getLogContent() 0 7 1
A encodeWithStartTestSuite() 0 14 3
A getStartTestSuiteLog() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests\Unit\Parser\JSON;
6
7
use Paraunit\Configuration\EnvVariables;
8
use Paraunit\Parser\JSON\LogPrinter;
9
use PHPUnit\Framework\AssertionFailedError;
10
use PHPUnit\Framework\Test;
11
use PHPUnit\Framework\TestSuite;
12
use PHPUnit\Framework\Warning;
13
use Tests\BaseUnitTestCase;
14
15
/**
16
 * Class LogPrinterTest
17
 * @package Tests\Unit\Parser\JSON
18
 */
19
class LogPrinterTest extends BaseUnitTestCase
20
{
21
    public function testStartTestSuite()
22
    {
23
        $this->createPrinterAndStartTestSuite();
24
25
        $this->assertEquals($this->encodeWithStartTestSuite(), $this->getLogContent());
26
    }
27
28
    public function testAddError()
29
    {
30
        $printer = $this->createPrinterAndStartTestSuite();
31
        $test = $this->prophesize(Test::class)->reveal();
32
33
        $printer->startTest($test);
34
        $printer->addError($test, new \Exception('Exception message'), 1);
35
        $line = __LINE__ - 1;
36
37
        $expectedContent = $this->encodeWithStartTestSuite([
38
            [
39
                'event' => 'testStart',
40
                'suite' => get_class($this),
41
                'test' => get_class($test),
42
            ],
43
            [
44
                'event' => 'test',
45
                'suite' => get_class($this),
46
                'test' => get_class($test),
47
                'status' => 'error',
48
                'time' => 1,
49
                'trace' => __FILE__ . ':' . $line . "\n",
50
                'message' => "Exception: Exception message\n",
51
                'output' => '',
52
            ],
53
        ]);
54
        $this->assertEquals($expectedContent, $this->getLogContent());
55
    }
56
57 View Code Duplication
    public function testAddWarning()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
58
    {
59
        $printer = $this->createPrinterAndStartTestSuite();
60
        $test = $this->prophesize(Test::class)->reveal();
61
        // has final methods, cannot be mocked
62
        $warning = new Warning('Warning message', null, new \Exception());
63
        $line = __LINE__ - 1;
64
65
        $printer->startTest($test);
66
        $printer->addWarning($test, $warning, 1);
67
68
        $expectedContent = $this->encodeWithStartTestSuite([
69
            [
70
                'event' => 'testStart',
71
                'suite' => get_class($this),
72
                'test' => get_class($test),
73
            ],
74
            [
75
                'event' => 'test',
76
                'suite' => get_class($this),
77
                'test' => get_class($test),
78
                'status' => 'warning',
79
                'time' => 1,
80
                'trace' => __FILE__ . ':' . $line . "\n",
81
                'message' => "Warning message\n",
82
                'output' => '',
83
            ],
84
        ]);
85
        $this->assertEquals($expectedContent, $this->getLogContent());
86
    }
87
88 View Code Duplication
    public function testAddFailure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
89
    {
90
        $printer = $this->createPrinterAndStartTestSuite();
91
        $test = $this->prophesize(Test::class)->reveal();
92
        // has final methods, cannot be mocked
93
        $failure = new AssertionFailedError('Failure message', null, new \Exception());
94
        $line = __LINE__ - 1;
95
96
        $printer->startTest($test);
97
        $printer->addFailure($test, $failure, 1);
98
99
        $expectedContent = $this->encodeWithStartTestSuite([
100
            [
101
                'event' => 'testStart',
102
                'suite' => get_class($this),
103
                'test' => get_class($test),
104
            ],
105
            [
106
                'event' => 'test',
107
                'suite' => get_class($this),
108
                'test' => get_class($test),
109
                'status' => 'fail',
110
                'time' => 1,
111
                'trace' => __FILE__ . ':' . $line . "\n",
112
                'message' => "Failure message\n",
113
                'output' => '',
114
            ],
115
        ]);
116
        $this->assertEquals($expectedContent, $this->getLogContent());
117
    }
118
119 View Code Duplication
    public function testAddIncompleteTest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
120
    {
121
        $printer = $this->createPrinterAndStartTestSuite();
122
        $test = $this->prophesize(Test::class)->reveal();
123
        // has final methods, cannot be mocked
124
        $failure = new \Exception('Incomplete message');
125
        $line = __LINE__ - 1;
126
127
        $printer->startTest($test);
128
        $printer->addIncompleteTest($test, $failure, 1);
129
130
        $expectedContent = $this->encodeWithStartTestSuite([
131
            [
132
                'event' => 'testStart',
133
                'suite' => get_class($this),
134
                'test' => get_class($test),
135
            ],
136
            [
137
                'event' => 'test',
138
                'suite' => get_class($this),
139
                'test' => get_class($test),
140
                'status' => 'error',
141
                'time' => 1,
142
                'trace' => __FILE__ . ':' . $line . "\n",
143
                'message' => 'Incomplete Test: Incomplete message',
144
                'output' => '',
145
            ],
146
        ]);
147
        $this->assertEquals($expectedContent, $this->getLogContent());
148
    }
149
150 View Code Duplication
    public function testAddRiskyTest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
151
    {
152
        $printer = $this->createPrinterAndStartTestSuite();
153
        $test = $this->prophesize(Test::class)->reveal();
154
        // has final methods, cannot be mocked
155
        $failure = new \Exception('Risky message');
156
        $line = __LINE__ - 1;
157
158
        $printer->startTest($test);
159
        $printer->addRiskyTest($test, $failure, 1);
160
161
        $expectedContent = $this->encodeWithStartTestSuite([
162
            [
163
                'event' => 'testStart',
164
                'suite' => get_class($this),
165
                'test' => get_class($test),
166
            ],
167
            [
168
                'event' => 'test',
169
                'suite' => get_class($this),
170
                'test' => get_class($test),
171
                'status' => 'error',
172
                'time' => 1,
173
                'trace' => __FILE__ . ':' . $line . "\n",
174
                'message' => 'Risky Test: Risky message',
175
                'output' => '',
176
            ],
177
        ]);
178
        $this->assertEquals($expectedContent, $this->getLogContent());
179
    }
180
181 View Code Duplication
    public function testAddSkippedTest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
182
    {
183
        $printer = $this->createPrinterAndStartTestSuite();
184
        $test = $this->prophesize(Test::class)->reveal();
185
        // has final methods, cannot be mocked
186
        $failure = new \Exception('Skipped message');
187
        $line = __LINE__ - 1;
188
189
        $printer->startTest($test);
190
        $printer->addSkippedTest($test, $failure, 1);
191
192
        $expectedContent = $this->encodeWithStartTestSuite([
193
            [
194
                'event' => 'testStart',
195
                'suite' => get_class($this),
196
                'test' => get_class($test),
197
            ],
198
            [
199
                'event' => 'test',
200
                'suite' => get_class($this),
201
                'test' => get_class($test),
202
                'status' => 'error',
203
                'time' => 1,
204
                'trace' => __FILE__ . ':' . $line . "\n",
205
                'message' => 'Skipped Test: Skipped message',
206
                'output' => '',
207
            ],
208
        ]);
209
        $this->assertEquals($expectedContent, $this->getLogContent());
210
    }
211
212
    public function testEndTest()
213
    {
214
        $printer = $this->createPrinterAndStartTestSuite();
215
        $test = $this->prophesize(Test::class)->reveal();
216
217
        $printer->startTest($test);
218
        $printer->endTest($test, 1);
219
220
        $expectedContent = $this->encodeWithStartTestSuite([
221
            [
222
                'event' => 'testStart',
223
                'suite' => get_class($this),
224
                'test' => get_class($test),
225
            ],
226
            [
227
                'event' => 'test',
228
                'suite' => get_class($this),
229
                'test' => get_class($test),
230
                'status' => 'pass',
231
                'time' => 1,
232
                'trace' => '',
233
                'message' => '',
234
                'output' => '',
235
            ],
236
        ]);
237
        $this->assertEquals($expectedContent, $this->getLogContent());
238
239
        $printer->endTestSuite($this->prophesize(TestSuite::class)->reveal());
240
241
        $this->assertEquals($expectedContent, $this->getLogContent());
242
    }
243
244
    private function createPrinterAndStartTestSuite(): LogPrinter
245
    {
246
        $this->createRandomTmpDir();
247
        putenv(EnvVariables::PROCESS_UNIQUE_ID . '=log-file-name');
248
        $printer = new LogPrinter();
249
        $testSuite = $this->prophesize(TestSuite::class);
250
        $testSuite->getName()
251
            ->willReturn(get_class($this));
252
        $testSuite->count()
253
            ->willReturn(1);
254
255
        $printer->startTestSuite($testSuite->reveal());
256
257
        return $printer;
258
    }
259
260
    private function getLogContent(): string
261
    {
262
        $logFilename = $this->getRandomTempDir() . 'log-file-name.json.log';
263
        $content = $this->getFileContent($logFilename);
264
265
        return preg_replace('/\r\n/', "\n", $content);
266
    }
267
268
    private function encodeWithStartTestSuite(array $data = []): string
269
    {
270
        $logElements = [$this->getStartTestSuiteLog()];
271
        foreach ($data as $datum) {
272
            $logElements[] = $datum;
273
        }
274
275
        $result = '';
276
        foreach ($logElements as $logElement) {
277
            $result .= json_encode($logElement, JSON_PRETTY_PRINT);
278
        }
279
280
        return $result;
281
    }
282
283
    private function getStartTestSuiteLog(): array
284
    {
285
        return [
286
            'event' => 'suiteStart',
287
            'suite' => get_class($this),
288
            'tests' => 1,
289
        ];
290
    }
291
}
292