|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Collision. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Nuno Maduro <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace NunoMaduro\Collision\Adapters\Phpunit; |
|
13
|
|
|
|
|
14
|
|
|
use NunoMaduro\Collision\Contracts\Adapters\Phpunit\Listener as ListenerContract; |
|
15
|
|
|
use NunoMaduro\Collision\Contracts\Writer as WriterContract; |
|
16
|
|
|
use NunoMaduro\Collision\Writer; |
|
17
|
|
|
use PHPUnit\Framework\AssertionFailedError; |
|
18
|
|
|
use PHPUnit\Framework\ExceptionWrapper; |
|
19
|
|
|
use PHPUnit\Framework\ExpectationFailedException; |
|
20
|
|
|
use PHPUnit\Framework\Test; |
|
21
|
|
|
use PHPUnit\Framework\TestSuite; |
|
22
|
|
|
use PHPUnit\Framework\Warning; |
|
23
|
|
|
use ReflectionObject; |
|
24
|
|
|
use Symfony\Component\Console\Application; |
|
25
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
|
26
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
27
|
|
|
use Whoops\Exception\Inspector; |
|
28
|
|
|
|
|
29
|
|
|
if (class_exists(\PHPUnit\Runner\Version::class) && intval(substr(\PHPUnit\Runner\Version::id(), 0, 1)) >= 7) { |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* This is an Collision Phpunit Adapter implementation. |
|
33
|
|
|
* |
|
34
|
|
|
* @author Nuno Maduro <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
class Listener implements ListenerContract |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* Holds an instance of the writer. |
|
40
|
|
|
* |
|
41
|
|
|
* @var \NunoMaduro\Collision\Contracts\Writer |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $writer; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Creates a new instance of the class. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \NunoMaduro\Collision\Contracts\Writer|null $writer |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public function __construct(WriterContract $writer = null) |
|
51
|
|
|
{ |
|
52
|
2 |
|
$this->writer = $writer ?: $this->buildWriter(); |
|
53
|
2 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function render(Test $test, \Throwable $throwable) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$this->writer->ignoreFilesIn([ |
|
61
|
1 |
|
'/vendor\/phpunit\/phpunit\/src/', |
|
62
|
|
|
'/vendor\/mockery\/mockery/', |
|
63
|
|
|
'/vendor\/laravel\/framework\/src\/Illuminate\/Testing/', |
|
64
|
|
|
'/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Testing/', |
|
65
|
|
|
]); |
|
66
|
|
|
|
|
67
|
1 |
|
$output = $this->writer->getOutput(); |
|
68
|
|
|
|
|
69
|
1 |
|
if (method_exists($test, 'getName')) { |
|
70
|
|
|
$output->writeln("\n"); |
|
71
|
|
|
$name = "\e[2m" . get_class($test) . "\e[22m"; |
|
72
|
|
|
$output->writeln(sprintf( |
|
73
|
|
|
' <bg=red;options=bold> FAIL </> <fg=red;options=bold></><fg=default>%s <fg=red;options=bold>➜</> %s</>', |
|
74
|
|
|
$name, |
|
75
|
|
|
$test->getName(false) |
|
|
|
|
|
|
76
|
|
|
)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
if ($throwable instanceof ExceptionWrapper && $throwable->getOriginalException() !== null) { |
|
80
|
|
|
$throwable = $throwable->getOriginalException(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
$inspector = new Inspector($throwable); |
|
84
|
|
|
|
|
85
|
1 |
|
$this->writer->write($inspector); |
|
86
|
|
|
|
|
87
|
1 |
|
if ($throwable instanceof ExpectationFailedException && $comparisionFailure = $throwable->getComparisonFailure()) { |
|
88
|
|
|
$output->write($comparisionFailure->getDiff()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
$this->terminate(); |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function addError(Test $test, \Throwable $throwable, float $time): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->render($test, $throwable); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function addWarning(Test $test, Warning $t, float $time): void |
|
106
|
|
|
{ |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function addFailure(Test $test, AssertionFailedError $throwable, float $time): void |
|
113
|
|
|
{ |
|
114
|
1 |
|
$reflector = new ReflectionObject($throwable); |
|
115
|
|
|
|
|
116
|
1 |
|
if ($reflector->hasProperty('message')) { |
|
117
|
1 |
|
$message = trim((string) preg_replace("/\r|\n/", ' ', $throwable->getMessage())); |
|
118
|
1 |
|
$property = $reflector->getProperty('message'); |
|
119
|
1 |
|
$property->setAccessible(true); |
|
120
|
1 |
|
$property->setValue($throwable, $message); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
$this->render($test, $throwable); |
|
124
|
1 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function addIncompleteTest(Test $test, \Throwable $t, float $time): void |
|
130
|
|
|
{ |
|
131
|
1 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function addRiskyTest(Test $test, \Throwable $t, float $time): void |
|
137
|
|
|
{ |
|
138
|
1 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function addSkippedTest(Test $test, \Throwable $t, float $time): void |
|
144
|
|
|
{ |
|
145
|
1 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritdoc} |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function startTestSuite(TestSuite $suite): void |
|
151
|
|
|
{ |
|
152
|
1 |
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
|
|
*/ |
|
157
|
1 |
|
public function endTestSuite(TestSuite $suite): void |
|
158
|
|
|
{ |
|
159
|
1 |
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* {@inheritdoc} |
|
163
|
|
|
*/ |
|
164
|
1 |
|
public function startTest(Test $test): void |
|
165
|
|
|
{ |
|
166
|
1 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* {@inheritdoc} |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function endTest(Test $test, float $time): void |
|
172
|
|
|
{ |
|
173
|
1 |
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Builds an Writer. |
|
177
|
|
|
* |
|
178
|
|
|
* @return \NunoMaduro\Collision\Contracts\Writer |
|
179
|
|
|
*/ |
|
180
|
1 |
|
protected function buildWriter(): WriterContract |
|
181
|
|
|
{ |
|
182
|
1 |
|
$writer = new Writer(); |
|
183
|
|
|
|
|
184
|
1 |
|
$application = new Application(); |
|
185
|
1 |
|
$reflector = new ReflectionObject($application); |
|
186
|
1 |
|
$method = $reflector->getMethod('configureIO'); |
|
187
|
1 |
|
$method->setAccessible(true); |
|
188
|
1 |
|
$method->invoke($application, new ArgvInput, $output = new ConsoleOutput); |
|
189
|
|
|
|
|
190
|
1 |
|
return $writer->setOutput($output); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Terminates the test. |
|
195
|
|
|
*/ |
|
196
|
|
|
public function terminate(): void |
|
197
|
|
|
{ |
|
198
|
|
|
exit(1); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: