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
|
|
|
public function setMasterListener(MasterListenerInterface $listener) |
27
|
|
|
{ |
28
|
|
|
$listener->addListener($this); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function addCharacteristic($type, $value) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$this->info( |
34
|
|
|
sprintf('Set %s to %s', $type, $value), |
35
|
|
|
[ |
36
|
|
|
'type' => 'characteristic', |
37
|
|
|
'characteristic' => $type, |
38
|
|
|
'value' => $value |
39
|
|
|
] |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getInvokedTest() |
44
|
|
|
{ |
45
|
|
|
return $this->invokedTest; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setTestName($name) |
49
|
|
|
{ |
50
|
|
|
$this->testName = $name; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setTestStatus($status) |
54
|
|
|
{ |
55
|
|
|
$this->status = $status; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setTestId($testId) |
59
|
|
|
{ |
60
|
|
|
$this->testId = $testId; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function logStep($stepId) |
64
|
|
|
{ |
65
|
|
|
$this->log($stepId, $this->createExtra(['type' => 'step'])); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function logAssertionSuccess(AbstractAssertion $assertion, array $extra) |
69
|
|
|
{ |
70
|
|
|
$extra = array_merge($extra, ['type' => 'assertion', 'result' => self::STATUS_PASSED]); |
71
|
|
|
$this->info(get_class($assertion) . ' - passed', $this->createExtra($extra)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function logAssertionFailure(Exception $e, AbstractAssertion $assertion, array $extra) |
75
|
|
|
{ |
76
|
|
|
$extra = array_merge($extra, ['type' => 'assertion', 'result' => self::STATUS_FAILED, 'stack_trace' => $e->getTrace()]); |
77
|
|
|
$this->err(get_class($assertion) . ' - ' . $e->getMessage(), $this->createExtra($extra)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function createExtra($includeArray = []) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$defaultArray = [ |
83
|
|
|
'type' => 'message', |
84
|
|
|
'status' => $this->status, |
85
|
|
|
'name' => $this->testName, |
86
|
|
|
'testId' => $this->testId |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
if ($this->selectorConfig) { |
90
|
|
|
$defaultArray = array_merge($this->selectorConfig, $defaultArray); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return array_merge($defaultArray, $includeArray); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
97
|
|
|
{ |
98
|
|
|
$this->setTestStatus(self::STATUS_FAILED); |
99
|
|
|
$this->notice($e->getMessage(), $this->createExtra(['trace' => $e->getTraceAsString()])); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time) |
103
|
|
|
{ |
104
|
|
|
$this->setTestStatus(self::STATUS_FAILED); |
105
|
|
|
$this->notice($e->getMessage(), $this->createExtra(['trace' => $e->getTraceAsString()])); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function addIncompleteTest(\PHPUnit_Framework_Test $test, Exception $e, $time) |
109
|
|
|
{ |
110
|
|
|
$this->setTestStatus(self::STATUS_INCOMPLETE); |
111
|
|
|
$this->notice($e->getMessage(), $this->createExtra()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function addRiskyTest(\PHPUnit_Framework_Test $test, Exception $e, $time) |
115
|
|
|
{ |
116
|
|
|
// ignore |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function addSkippedTest(\PHPUnit_Framework_Test $test, Exception $e, $time) |
120
|
|
|
{ |
121
|
|
|
self::setTestStatus(self::STATUS_SKIPPED); |
122
|
|
|
$this->notice($e->getMessage(), $this->createExtra()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function startTestSuite(\PHPUnit_Framework_TestSuite $suite) |
126
|
|
|
{ |
127
|
|
|
// ignore |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function endTestSuite(\PHPUnit_Framework_TestSuite $suite) |
131
|
|
|
{ |
132
|
|
|
$this->testName = self::NAME_DEFAULT; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
View Code Duplication |
public function startTest(\PHPUnit_Framework_Test $test) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
if ($test instanceof TestCase) { |
138
|
|
|
if (!$this->testName) { |
139
|
|
|
$this->setTestName(get_class($test) . '::' . $test->getName()); |
140
|
|
|
} |
141
|
|
|
$this->invokedTest = get_class($test) . '::' . $test->getName(); |
142
|
|
|
$this->setTestStatus(self::STATUS_PASSED); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function endTest(\PHPUnit_Framework_Test $test, $time) |
148
|
|
|
{ |
149
|
|
|
$this->info(sprintf('Test completed with status: %s', $this->status), $this->createExtra()); |
150
|
|
|
$this->testName = self::NAME_DEFAULT; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|