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