1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tests\Unit\Parser\JSON; |
6
|
|
|
|
7
|
|
|
use Paraunit\Lifecycle\ProcessEvent; |
8
|
|
|
use Paraunit\Parser\JSON\LogFetcher; |
9
|
|
|
use Paraunit\Parser\JSON\LogParser; |
10
|
|
|
use Paraunit\Parser\JSON\ParserChainElementInterface; |
11
|
|
|
use Paraunit\Parser\JSON\RetryParser; |
12
|
|
|
use Paraunit\TestResult\Interfaces\TestResultHandlerInterface; |
13
|
|
|
use Prophecy\Argument; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
15
|
|
|
use Tests\BaseUnitTestCase; |
16
|
|
|
use Tests\Stub\StubbedParaunitProcess; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class LogParserTest |
20
|
|
|
* @package Tests\Unit\Parser\JSON |
21
|
|
|
*/ |
22
|
|
|
class LogParserTest extends BaseUnitTestCase |
23
|
|
|
{ |
24
|
|
|
public function testOnProcessTerminatedHasProperChainInterruption() |
25
|
|
|
{ |
26
|
|
|
$process = new StubbedParaunitProcess(); |
27
|
|
|
$process->setOutput('All ok'); |
28
|
|
|
$parser1 = $this->prophesize(ParserChainElementInterface::class); |
29
|
|
|
$parser1->handleLogItem($process, Argument::cetera()) |
30
|
|
|
->shouldBeCalledTimes(2) |
31
|
|
|
->willReturn(null); |
32
|
|
|
$parser2 = $this->prophesize(ParserChainElementInterface::class); |
33
|
|
|
$parser2->handleLogItem($process, Argument::cetera()) |
34
|
|
|
->shouldBeCalledTimes(2) |
35
|
|
|
->willReturn($this->mockTestResult()); |
36
|
|
|
$parser3 = $this->prophesize(ParserChainElementInterface::class); |
37
|
|
|
$parser3->handleLogItem($process, Argument::cetera()) |
38
|
|
|
->shouldNotBeCalled(); |
39
|
|
|
|
40
|
|
|
$parser = $this->createParser(true, false); |
41
|
|
|
$parser->addParser($parser1->reveal()); |
42
|
|
|
$parser->addParser($parser2->reveal()); |
43
|
|
|
$parser->addParser($parser3->reveal()); |
44
|
|
|
|
45
|
|
|
$parser->onProcessTerminated(new ProcessEvent($process)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testParseHandlesMissingLogs() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$process = new StubbedParaunitProcess(); |
51
|
|
|
$process->setOutput('Test output (core dumped)'); |
52
|
|
|
$process->setExitCode(139); |
53
|
|
|
$parser1 = $this->prophesize(ParserChainElementInterface::class); |
54
|
|
|
$parser1->handleLogItem($process, Argument::cetera()) |
55
|
|
|
->shouldBeCalledTimes(1) |
56
|
|
|
->willReturn($this->mockTestResult()); |
57
|
|
|
$parser2 = $this->prophesize(ParserChainElementInterface::class); |
58
|
|
|
$parser2->handleLogItem($process, Argument::cetera()) |
59
|
|
|
->shouldNotBeCalled(); |
60
|
|
|
|
61
|
|
|
$parser = $this->createParser(false); |
62
|
|
|
$parser->addParser($parser1->reveal()); |
63
|
|
|
$parser->addParser($parser2->reveal()); |
64
|
|
|
|
65
|
|
|
$parser->onProcessTerminated(new ProcessEvent($process)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
public function testParseHandlesNoTestExecuted() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$process = new StubbedParaunitProcess(); |
71
|
|
|
$process->setOutput('No tests executed!'); |
72
|
|
|
$process->setExitCode(0); |
73
|
|
|
$parser1 = $this->prophesize(ParserChainElementInterface::class); |
74
|
|
|
$parser1->handleLogItem($process, Argument::cetera()) |
75
|
|
|
->shouldNotBeCalled(); |
76
|
|
|
|
77
|
|
|
$parser = $this->createParser(false, false, true); |
78
|
|
|
$parser->addParser($parser1->reveal()); |
79
|
|
|
|
80
|
|
|
$parser->onProcessTerminated(new ProcessEvent($process)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function testParseHandlesTestToBeRetried() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$process = new StubbedParaunitProcess(); |
86
|
|
|
$process->setOutput('No tests executed!'); |
87
|
|
|
$process->setExitCode(0); |
88
|
|
|
$parser1 = $this->prophesize(ParserChainElementInterface::class); |
89
|
|
|
$parser1->handleLogItem($process, Argument::cetera()) |
90
|
|
|
->shouldNotBeCalled(); |
91
|
|
|
|
92
|
|
|
$parser = $this->createParser(true, false, false, true); |
93
|
|
|
$parser->addParser($parser1->reveal()); |
94
|
|
|
|
95
|
|
|
$parser->onProcessTerminated(new ProcessEvent($process)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function createParser(bool $logFound = true, bool $abnormal = true, bool $noTestExecuted = false, bool $willBeRetried = false): LogParser |
99
|
|
|
{ |
100
|
|
|
$logLocator = $this->prophesize(LogFetcher::class); |
101
|
|
|
$endLog = new \stdClass(); |
102
|
|
|
$endLog->status = LogFetcher::LOG_ENDING_STATUS; |
103
|
|
|
if ($logFound) { |
104
|
|
|
$log1 = new \stdClass(); |
105
|
|
|
$log1->event = $abnormal ? 'testStart' : 'test'; |
106
|
|
|
$log1->test = 'testSomething'; |
107
|
|
|
$logLocator->fetch(Argument::cetera()) |
108
|
|
|
->willReturn([$log1, $endLog]); |
109
|
|
|
} else { |
110
|
|
|
$logLocator->fetch(Argument::cetera()) |
111
|
|
|
->willReturn([$endLog]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$noTestExecutedContainer = $this->prophesize(TestResultHandlerInterface::class); |
115
|
|
|
$noTestExecutedContainer->addProcessToFilenames(Argument::any()) |
116
|
|
|
->shouldBeCalledTimes((int) $noTestExecuted); |
117
|
|
|
|
118
|
|
|
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class); |
119
|
|
|
$eventDispatcher->dispatch('OtherEvents') |
120
|
|
|
->shouldNotBeCalled(); |
121
|
|
|
|
122
|
|
|
if ($willBeRetried) { |
123
|
|
|
$eventDispatcher->dispatch( |
124
|
|
|
ProcessEvent::PROCESS_TO_BE_RETRIED, |
125
|
|
|
Argument::type(ProcessEvent::class) |
126
|
|
|
) |
127
|
|
|
->shouldBeCalledTimes(1); |
128
|
|
|
} elseif (! $noTestExecuted) { |
129
|
|
|
$eventDispatcher->dispatch( |
130
|
|
|
ProcessEvent::PROCESS_PARSING_COMPLETED, |
131
|
|
|
Argument::type(ProcessEvent::class) |
132
|
|
|
) |
133
|
|
|
->shouldBeCalledTimes(1); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$retryParser = $this->prophesize(RetryParser::class); |
137
|
|
|
$retryParser->processWillBeRetried(Argument::cetera()) |
138
|
|
|
->shouldBeCalledTimes((int) ! $noTestExecuted) |
139
|
|
|
->willReturn($willBeRetried); |
140
|
|
|
|
141
|
|
|
return new LogParser( |
142
|
|
|
$logLocator->reveal(), |
143
|
|
|
$noTestExecutedContainer->reveal(), |
144
|
|
|
$eventDispatcher->reveal(), |
145
|
|
|
$retryParser->reveal() |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
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.