|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Paraunit\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use Paraunit\Process\ProcessWithResultsInterface; |
|
6
|
|
|
use Paraunit\TestResult\TestResultFactory; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class AbstractParser |
|
10
|
|
|
* @package Paraunit\Parser |
|
11
|
|
|
*/ |
|
12
|
|
|
class AbstractParser implements JSONParserChainElementInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var TestResultFactory */ |
|
15
|
|
|
protected $testResultFactory; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $status; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
protected $messageStartsWith; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* AbstractParser constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param TestResultFactory $testResultFactory |
|
27
|
|
|
* @param string $status The status that the parser should catch |
|
28
|
|
|
* @param string | null $messageStartsWith The start of the message that the parser should look for, if any |
|
29
|
|
|
*/ |
|
30
|
50 |
|
public function __construct(TestResultFactory $testResultFactory, $status, $messageStartsWith = null) |
|
31
|
|
|
{ |
|
32
|
50 |
|
$this->testResultFactory = $testResultFactory; |
|
33
|
50 |
|
$this->status = $status; |
|
34
|
50 |
|
$this->messageStartsWith = $messageStartsWith; |
|
35
|
50 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
42 |
|
public function handleLogItem(ProcessWithResultsInterface $process, \stdClass $logItem) |
|
41
|
|
|
{ |
|
42
|
42 |
|
if ($this->logMatches($logItem)) { |
|
43
|
31 |
|
return $this->testResultFactory->createFromLog($logItem); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
29 |
|
return null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param \stdClass $log |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
34 |
|
protected function logMatches(\stdClass $log) |
|
54
|
|
|
{ |
|
55
|
34 |
|
if ( ! property_exists($log, 'status')) { |
|
56
|
2 |
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
33 |
|
if ($log->status != $this->status) { |
|
60
|
27 |
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
23 |
|
if (is_null($this->messageStartsWith)) { |
|
64
|
21 |
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
12 |
|
if ( ! property_exists($log, 'message')) { |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
12 |
|
return 0 === strpos($log->message, $this->messageStartsWith); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|