1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Util\Log; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Magium\Assertions\AbstractAssertion; |
7
|
|
|
use Magium\Util\Phpunit\MasterListenerAware; |
8
|
|
|
use Magium\Util\Phpunit\MasterListenerInterface; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use PHPUnit\Framework\TestListener; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class LoggerPHPUnit5 extends \Zend\Log\Logger implements TestListener, MasterListenerAware, LoggerInterface |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
protected $status = self::STATUS_PASSED; |
17
|
|
|
|
18
|
|
|
protected $testName = self::NAME_DEFAULT; |
19
|
|
|
|
20
|
|
|
protected $testId; |
21
|
|
|
|
22
|
|
|
protected $invokedTest; |
23
|
|
|
|
24
|
|
|
protected $selectorConfig = null; |
25
|
|
|
|
26
|
|
|
protected static $testRunId; |
27
|
|
|
|
28
|
|
|
public function setMasterListener(MasterListenerInterface $listener) |
29
|
|
|
{ |
30
|
|
|
$listener->addListener($this); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function addCharacteristic($type, $value) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this->info( |
36
|
|
|
sprintf('Set %s to %s', $type, $value), |
37
|
|
|
[ |
38
|
|
|
'type' => 'characteristic', |
39
|
|
|
'characteristic' => $type, |
40
|
|
|
'value' => $value |
41
|
|
|
] |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getInvokedTest() |
46
|
|
|
{ |
47
|
|
|
return $this->invokedTest; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function setTestName($name) |
51
|
|
|
{ |
52
|
|
|
$this->testName = $name; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setTestStatus($status) |
56
|
|
|
{ |
57
|
|
|
$this->status = $status; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function setTestId($testId) |
61
|
|
|
{ |
62
|
|
|
$this->testId = $testId; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function logStep($stepId) |
66
|
|
|
{ |
67
|
|
|
$this->log($stepId, $this->createExtra(['type' => 'step'])); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function logAssertionSuccess(AbstractAssertion $assertion, array $extra) |
71
|
|
|
{ |
72
|
|
|
$extra = array_merge($extra, ['type' => 'assertion', 'result' => self::STATUS_PASSED]); |
73
|
|
|
$this->info(get_class($assertion) . ' - passed', $this->createExtra($extra)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function logAssertionFailure(Exception $e, AbstractAssertion $assertion, array $extra) |
77
|
|
|
{ |
78
|
|
|
$extra = array_merge($extra, ['type' => 'assertion', 'result' => self::STATUS_FAILED, 'stack_trace' => $e->getTrace()]); |
79
|
|
|
$this->err(get_class($assertion) . ' - ' . $e->getMessage(), $this->createExtra($extra)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function createExtra($includeArray = []) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$defaultArray = [ |
85
|
|
|
'type' => 'message', |
86
|
|
|
'status' => $this->status, |
87
|
|
|
'name' => $this->testName, |
88
|
|
|
'test_id' => $this->testId, |
89
|
|
|
'test_run_id' => $this->getTestRunId() |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
if ($this->selectorConfig) { |
93
|
|
|
$defaultArray = array_merge($this->selectorConfig, $defaultArray); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return array_merge($defaultArray, $includeArray); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function getTestRunId() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
if (!self::$testRunId) { |
102
|
|
|
// See https://github.com/ircmaxell/RandomLib/issues/55 |
103
|
|
|
if (function_exists('random_bytes')) { |
104
|
|
|
$unique = uniqid(substr(bin2hex(random_bytes(64)), 0, 64)); |
105
|
|
|
|
106
|
|
|
} else if (function_exists('openssl_random_pseudo_bytes')) { |
107
|
|
|
$unique = uniqid(openssl_random_pseudo_bytes(64)); |
108
|
|
|
} else { |
109
|
|
|
$unique = uniqid('', true); |
110
|
|
|
} |
111
|
|
|
self::$testRunId = $unique; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return self::$testRunId; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
119
|
|
|
{ |
120
|
|
|
$this->setTestStatus(self::STATUS_FAILED); |
121
|
|
|
$this->notice($e->getMessage(), $this->createExtra(['trace' => $e->getTraceAsString()])); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time) |
125
|
|
|
{ |
126
|
|
|
$this->setTestStatus(self::STATUS_FAILED); |
127
|
|
|
$this->notice($e->getMessage(), $this->createExtra(['trace' => $e->getTraceAsString()])); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function addIncompleteTest(\PHPUnit_Framework_Test $test, Exception $e, $time) |
131
|
|
|
{ |
132
|
|
|
$this->setTestStatus(self::STATUS_INCOMPLETE); |
133
|
|
|
$this->notice($e->getMessage(), $this->createExtra()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function addRiskyTest(\PHPUnit_Framework_Test $test, Exception $e, $time) |
137
|
|
|
{ |
138
|
|
|
// ignore |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function addSkippedTest(\PHPUnit_Framework_Test $test, Exception $e, $time) |
142
|
|
|
{ |
143
|
|
|
self::setTestStatus(self::STATUS_SKIPPED); |
144
|
|
|
$this->notice($e->getMessage(), $this->createExtra()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function startTestSuite(\PHPUnit_Framework_TestSuite $suite) |
148
|
|
|
{ |
149
|
|
|
// ignore |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function endTestSuite(\PHPUnit_Framework_TestSuite $suite) |
153
|
|
|
{ |
154
|
|
|
$this->testName = self::NAME_DEFAULT; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
View Code Duplication |
public function startTest(\PHPUnit_Framework_Test $test) |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
if ($test instanceof TestCase) { |
160
|
|
|
if (!$this->testName) { |
161
|
|
|
$this->setTestName(get_class($test) . '::' . $test->getName()); |
162
|
|
|
} |
163
|
|
|
$this->invokedTest = get_class($test) . '::' . $test->getName(); |
164
|
|
|
$this->setTestStatus(self::STATUS_PASSED); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function endTest(\PHPUnit_Framework_Test $test, $time) |
170
|
|
|
{ |
171
|
|
|
$this->info(sprintf('Test completed with status: %s', $this->status), $this->createExtra()); |
172
|
|
|
$this->testName = self::NAME_DEFAULT; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|