|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Paraunit\Parser\JSON; |
|
6
|
|
|
|
|
7
|
|
|
use Paraunit\Process\AbstractParaunitProcess; |
|
8
|
|
|
use Paraunit\TestResult\Interfaces\TestResultHandlerInterface; |
|
9
|
|
|
use Paraunit\TestResult\TestResultFactory; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class GenericParser |
|
13
|
|
|
* @package Paraunit\Parser\JSON |
|
14
|
|
|
*/ |
|
15
|
|
|
class GenericParser implements ParserChainElementInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var TestResultFactory */ |
|
18
|
|
|
protected $testResultFactory; |
|
19
|
|
|
|
|
20
|
|
|
/** @var TestResultHandlerInterface */ |
|
21
|
|
|
protected $testResultContainer; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
protected $status; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string|null */ |
|
27
|
|
|
protected $messageStartsWith; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* GenericParser constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param TestResultFactory $testResultFactory |
|
33
|
|
|
* @param TestResultHandlerInterface $testResultContainer |
|
34
|
|
|
* @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, if any |
|
36
|
|
|
*/ |
|
37
|
62 |
|
public function __construct( |
|
38
|
|
|
TestResultFactory $testResultFactory, |
|
39
|
|
|
TestResultHandlerInterface $testResultContainer, |
|
40
|
|
|
string $status, |
|
41
|
|
|
string $messageStartsWith = null |
|
42
|
|
|
) { |
|
43
|
62 |
|
$this->testResultFactory = $testResultFactory; |
|
44
|
62 |
|
$this->testResultContainer = $testResultContainer; |
|
45
|
62 |
|
$this->status = $status; |
|
46
|
62 |
|
$this->messageStartsWith = $messageStartsWith; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
57 |
|
public function handleLogItem(AbstractParaunitProcess $process, \stdClass $logItem) |
|
53
|
|
|
{ |
|
54
|
57 |
|
if ($this->logMatches($logItem)) { |
|
55
|
46 |
|
$testResult = $this->testResultFactory->createFromLog($logItem); |
|
56
|
46 |
|
$this->testResultContainer->handleTestResult($process, $testResult); |
|
57
|
|
|
|
|
58
|
46 |
|
return $testResult; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
35 |
|
return null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
49 |
|
protected function logMatches(\stdClass $log): bool |
|
65
|
|
|
{ |
|
66
|
49 |
|
if (! property_exists($log, 'status')) { |
|
67
|
3 |
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
48 |
|
if ($log->status !== $this->status) { |
|
71
|
33 |
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
38 |
|
if (null === $this->messageStartsWith) { |
|
75
|
36 |
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
16 |
|
if (! property_exists($log, 'message')) { |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
16 |
|
return 0 === strpos($log->message, $this->messageStartsWith); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|