|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Paraunit\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use Paraunit\Printer\OutputContainer; |
|
6
|
|
|
use Paraunit\Process\ProcessResultInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class AbstractParser |
|
10
|
|
|
* @package Paraunit\Parser |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class AbstractParser implements JSONParserChainElementInterface, OutputContainerBearerInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var OutputContainer */ |
|
15
|
|
|
protected $outputContainer; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $singleResultMarker; |
|
19
|
|
|
|
|
20
|
37 |
|
/** @var string */ |
|
21
|
|
|
protected $title; |
|
22
|
37 |
|
|
|
23
|
37 |
|
/** @var string */ |
|
24
|
37 |
|
protected $status; |
|
25
|
|
|
|
|
26
|
37 |
|
/** @var string */ |
|
27
|
37 |
|
protected $messageStartsWith; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* AbstractParser constructor. |
|
31
|
|
|
* |
|
32
|
26 |
|
* @param OutputContainer $outputContainer |
|
33
|
|
|
* @param string $singleResultMarker The output of the single test result (.FERW etc) |
|
34
|
26 |
|
* @param string $status The status that the parser should catch |
|
35
|
|
|
* @param string | null $messageStartsWith The start of the message that the parser should look for |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(OutputContainer $outputContainer, $singleResultMarker, $status, $messageStartsWith = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->outputContainer = $outputContainer; |
|
40
|
|
|
$this->singleResultMarker = $singleResultMarker; |
|
41
|
24 |
|
$this->status = $status; |
|
42
|
|
|
$this->messageStartsWith = $messageStartsWith; |
|
43
|
24 |
|
} |
|
44
|
|
|
|
|
45
|
24 |
|
/** |
|
46
|
10 |
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
10 |
View Code Duplication |
public function parsingFoundResult(ProcessResultInterface $process, \stdClass $log) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
10 |
|
if ($log->status == $this->status) { |
|
51
|
10 |
|
$process->addTestResult($this->singleResultMarker); |
|
52
|
|
|
$this->outputContainer->addToOutputBuffer($process, $log->message); |
|
53
|
10 |
|
|
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
18 |
|
|
|
57
|
|
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return OutputContainer |
|
62
|
|
|
*/ |
|
63
|
18 |
|
public function getOutputContainer() |
|
64
|
|
|
{ |
|
65
|
18 |
|
return $this->outputContainer; |
|
66
|
4 |
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
/** |
|
69
|
|
|
* @param ProcessResultInterface $process |
|
70
|
|
|
* @return bool |
|
71
|
15 |
|
*/ |
|
72
|
|
|
protected function storeParsedBlocks(ProcessResultInterface $process) |
|
73
|
|
|
{ |
|
74
|
37 |
|
$parsedBlob = $this->parse($process); |
|
75
|
|
|
|
|
76
|
37 |
|
if (isset($parsedBlob[1])) { |
|
77
|
37 |
|
$parsedBlocks = preg_split('/^\d+\) /m', $parsedBlob[1]); |
|
78
|
|
|
// il primo è sempre vuoto a causa dello split |
|
79
|
37 |
|
unset($parsedBlocks[0]); |
|
80
|
|
|
|
|
81
|
|
|
$this->outputContainer->addToOutputBuffer($parsedBlocks); |
|
|
|
|
|
|
82
|
|
|
$this->outputContainer->addFileName($process->getFilename()); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
37 |
|
|
|
87
|
|
|
return false; |
|
88
|
37 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
37 |
|
* @param ProcessResultInterface $process |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function parsingFoundSomething(ProcessResultInterface $process) |
|
95
|
|
|
{ |
|
96
|
|
|
if (count($this->parse($process)) > 0) { |
|
97
|
|
|
$this->outputContainer->addFileName($process->getFilename()); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
private function parse(ProcessResultInterface $process) |
|
106
|
|
|
{ |
|
107
|
|
|
$parsedBlob = array(); |
|
108
|
|
|
preg_match(static::PARSING_REGEX, $process->getOutput(), $parsedBlob); |
|
109
|
|
|
|
|
110
|
|
|
return $parsedBlob; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
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.