1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2011-present Mediasift Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package Storyplayer/Console |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://datasift.github.io/storyplayer |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace DataSift\Storyplayer\Console; |
45
|
|
|
|
46
|
|
|
use DataSift\Storyplayer\PlayerLib\Phase_Result; |
47
|
|
|
use DataSift\Storyplayer\PlayerLib\PhaseGroup_Result; |
48
|
|
|
use DataSift\Storyplayer\PlayerLib\Story; |
49
|
|
|
use DataSift\Storyplayer\PlayerLib\Story_Result; |
50
|
|
|
use DataSift\Storyplayer\OutputLib\CodeFormatter; |
51
|
|
|
use DataSift\Storyplayer\OutputLib\OutputPlugin; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* the API for console plugins |
55
|
|
|
* |
56
|
|
|
* @category Libraries |
57
|
|
|
* @package Storyplayer/Console |
58
|
|
|
* @author Stuart Herbert <[email protected]> |
59
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
60
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
61
|
|
|
* @link http://datasift.github.io/storyplayer |
62
|
|
|
*/ |
63
|
|
|
abstract class Console extends OutputPlugin |
64
|
|
|
{ |
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
* @var array(PhaseGroup_Result) |
68
|
|
|
*/ |
69
|
|
|
protected $results; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* are we running totally silently? |
73
|
|
|
* @var boolean |
74
|
|
|
*/ |
75
|
|
|
private $silentActivity = false; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* are we in verbose mode? |
79
|
|
|
* @var boolean |
80
|
|
|
*/ |
81
|
|
|
private $isVerbose = false; |
82
|
|
|
|
83
|
|
|
public function resetSilentMode() |
84
|
|
|
{ |
85
|
|
|
$this->silentActivity = false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function setSilentMode() |
89
|
|
|
{ |
90
|
|
|
$this->silentActivity = true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function isSilent() |
94
|
|
|
{ |
95
|
|
|
return $this->silentActivity; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getIsVerbose() |
99
|
|
|
{ |
100
|
|
|
return $this->isVerbose; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function setIsVerbose($isVerbose) |
104
|
|
|
{ |
105
|
|
|
$this->isVerbose = $isVerbose; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* called when Storyplayer exits |
110
|
|
|
* |
111
|
|
|
* @return void |
|
|
|
|
112
|
|
|
*/ |
113
|
|
|
public function writeFinalReport($duration, $summary) |
114
|
|
|
{ |
115
|
|
|
// keep count of what the final results are |
116
|
|
|
$succeededGroups = []; |
117
|
|
|
$skippedGroups = []; |
118
|
|
|
$failedGroups = []; |
119
|
|
|
|
120
|
|
|
// do we have any results? |
121
|
|
|
if (!isset($this->results)) { |
122
|
|
|
// huh - nothing happened at all |
123
|
|
|
$this->write("HUH - nothing appears to have happened. Time taken: ", $this->writer->puzzledSummaryStyle); |
124
|
|
|
$this->writeDuration($duration, $this->writer->puzzledSummaryStyle); |
125
|
|
|
$this->write(PHP_EOL . PHP_EOL); |
126
|
|
|
return 1; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// this is our opportunity to tell the user how our story(ies) |
130
|
|
|
// went in detail |
131
|
|
|
foreach ($this->results as $result) |
132
|
|
|
{ |
133
|
|
|
// so what happened? |
134
|
|
|
switch ($result->resultCode) |
135
|
|
|
{ |
136
|
|
|
case PhaseGroup_Result::OKAY: |
137
|
|
|
// this is a good result |
138
|
|
|
$succeededGroups[] = $result; |
139
|
|
|
break; |
140
|
|
|
|
141
|
|
|
case PhaseGroup_Result::SKIPPED: |
142
|
|
|
case PhaseGroup_Result::BLACKLISTED: |
143
|
|
|
// this can legitimately happen |
144
|
|
|
$skippedGroups[] = $result; |
145
|
|
|
break; |
146
|
|
|
|
147
|
|
|
default: |
148
|
|
|
// everything else is an error of some kind |
149
|
|
|
$failedGroups[] = $result; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// what's the final tally? |
154
|
|
|
$this->write(PHP_EOL); |
155
|
|
|
if (empty($succeededGroups) && empty($skippedGroups) && empty($failedGroups)) { |
156
|
|
|
// huh - nothing happened at all |
157
|
|
|
$this->write("HUH - nothing appears to have happened. Time taken: ", $this->writer->puzzledSummaryStyle); |
158
|
|
|
$this->writeDuration($duration, $this->writer->puzzledSummaryStyle); |
159
|
|
|
$this->write(PHP_EOL . PHP_EOL); |
160
|
|
|
return 1; |
161
|
|
|
} |
162
|
|
|
if (empty($failedGroups)) { |
163
|
|
|
// nothing failed |
164
|
|
|
$this->write("SUCCESS - " . count($succeededGroups) . ' PASSED, ' . count($skippedGroups) . ' SKIPPED. Time taken: ', $this->writer->successSummaryStyle); |
165
|
|
|
$this->writeDuration($duration, $this->writer->successSummaryStyle); |
166
|
|
|
$this->write(PHP_EOL . PHP_EOL); |
167
|
|
|
return 0; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// if we get here, then at least one thing failed |
171
|
|
|
$this->write("FAILURE - " |
172
|
|
|
. count($succeededGroups) . ' PASSED, ' |
173
|
|
|
. count($skippedGroups) . ' SKIPPED, ' |
174
|
|
|
. count($failedGroups) . ' FAILED :( Time taken: ', |
175
|
|
|
$this->writer->failSummaryStyle); |
176
|
|
|
$this->writeDuration($duration, $this->writer->failSummaryStyle); |
177
|
|
|
$this->write(PHP_EOL . PHP_EOL); |
178
|
|
|
|
179
|
|
|
// write out a list of failed tests - someone will want to look |
180
|
|
|
// at them in detail |
181
|
|
|
$this->write("Here's the list of everything that failed:" . PHP_EOL . PHP_EOL); |
182
|
|
|
// foreach ($skippedGroups as $skippedGroup) { |
183
|
|
|
// $this->writePhaseGroupSkipped(); |
184
|
|
|
// $this->write(' ' . $skippedGroup->activity, $this->writer->activityStyle); |
185
|
|
|
// $this->write(' ' . $skippedGroup->name . PHP_EOL, $this->writer->nameStyle); |
186
|
|
|
|
187
|
|
|
// if (isset($skippedGroup->filename)) { |
188
|
|
|
// $this->write(' (', $this->writer->punctuationStyle); |
189
|
|
|
// $this->write($skippedGroup->filename, $this->writer->punctuationStyle); |
190
|
|
|
// $this->write(')' . PHP_EOL, $this->writer->punctuationStyle); |
191
|
|
|
// } |
192
|
|
|
// } |
193
|
|
|
foreach ($failedGroups as $failedGroup) { |
194
|
|
|
$this->writePhaseGroupFailed(); |
195
|
|
|
$this->write(' ' . $failedGroup->activity, $this->writer->activityStyle); |
196
|
|
|
$this->write(' ' . $failedGroup->name . PHP_EOL, $this->writer->nameStyle); |
197
|
|
|
|
198
|
|
|
if (isset($failedGroup->filename)) { |
199
|
|
|
$this->write(' (', $this->writer->punctuationStyle); |
200
|
|
|
$this->write($failedGroup->filename, $this->writer->punctuationStyle); |
201
|
|
|
$this->write(')' . PHP_EOL, $this->writer->punctuationStyle); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
$this->write(PHP_EOL); |
205
|
|
|
|
206
|
|
|
// do we stop here? |
207
|
|
|
if (!$summary) { |
208
|
|
|
// we're in dev mode, which means the error reports have |
209
|
|
|
// already been shown where they happened |
210
|
|
|
return 1; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
// are we being run by hand? |
214
|
|
|
if (function_exists("posix_isatty") && posix_isatty(STDOUT)) { |
215
|
|
|
$this->write("See "); |
216
|
|
|
$this->write("storyplayer.log", $this->writer->argStyle); |
217
|
|
|
$this->write(" for details on what went wrong." . PHP_EOL . PHP_EOL); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// all done |
221
|
|
|
return 1; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
protected function writeDetailedErrorReport($result) |
225
|
|
|
{ |
226
|
|
|
// did anything go wrong? |
227
|
|
|
if ($result->getPhaseGroupSucceeded() || $result->getPhaseGroupSkipped()) { |
228
|
|
|
// everything is fine |
229
|
|
|
return; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
// sanity check: we should always have a failedPhase |
233
|
|
|
if (!$result->failedPhase instanceof Phase_Result) { |
234
|
|
|
throw new E5xx_MissingFailedPhase(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
// is it a story? |
238
|
|
|
if ($result instanceof Story_Result) { |
239
|
|
|
$this->showActivityForPhase($result->story, $result->failedPhase); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
protected function writeActivity($message, $codeLine = null, $timestamp = null) |
244
|
|
|
{ |
245
|
|
|
// when did this happen? |
246
|
|
|
if (!$timestamp) { |
247
|
|
|
$timestamp = time(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// prepare date/time for output |
251
|
|
|
$now = date('Y-m-d H:i:s', $timestamp); |
252
|
|
|
|
253
|
|
|
// do we have a codeLine to output? |
254
|
|
|
if ($codeLine) { |
255
|
|
|
$this->write('[', $this->writer->punctuationStyle); |
256
|
|
|
$this->write($now, $this->writer->timeStyle); |
257
|
|
|
$this->write('] ', $this->writer->punctuationStyle); |
258
|
|
|
|
259
|
|
|
// prepare the code for output |
260
|
|
|
// |
261
|
|
|
// if the code is longer than a single line, we want to display |
262
|
|
|
// it on its own line, to make the output more readable |
263
|
|
|
$parts = explode("\n", $codeLine['code']); |
264
|
|
|
if (count($parts) > 1) { |
265
|
|
|
$codeLine['code'] = PHP_EOL . $codeLine['code']; |
266
|
|
|
} |
267
|
|
|
else { |
268
|
|
|
$codeLine['code'] = ' ' . $codeLine['code']; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
// how many spaces do we need to write? |
272
|
|
|
$shorterMsg = trim($message); |
273
|
|
|
$indent = strlen($message) - strlen($shorterMsg); |
274
|
|
|
$this->write(str_repeat(" ", $indent)); |
275
|
|
|
$this->write("code --", $this->writer->commentStyle); |
276
|
|
|
$this->writeCodePointCode($codeLine, $this->writer->commentStyle); |
|
|
|
|
277
|
|
|
$this->write(PHP_EOL); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// output date/time |
281
|
|
|
$this->write('[', $this->writer->punctuationStyle); |
282
|
|
|
$this->write($now, $this->writer->timeStyle); |
283
|
|
|
$this->write('] ', $this->writer->punctuationStyle); |
284
|
|
|
|
285
|
|
|
// output the message |
286
|
|
|
$this->write(rtrim($message) . PHP_EOL); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
protected function writePhaseGroupSucceeded($msg = 'OKAY') |
290
|
|
|
{ |
291
|
|
|
$this->write('[', $this->writer->punctuationStyle); |
292
|
|
|
$this->write($msg, $this->writer->successStyle); |
293
|
|
|
$this->write(']', $this->writer->punctuationStyle); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
protected function writePhaseGroupFailed($msg = 'FAIL') |
297
|
|
|
{ |
298
|
|
|
$this->write('[', $this->writer->punctuationStyle); |
299
|
|
|
$this->write($msg, $this->writer->failStyle); |
300
|
|
|
$this->write(']', $this->writer->punctuationStyle); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
protected function writePhaseGroupSkipped($msg = 'SKIP') |
304
|
|
|
{ |
305
|
|
|
$this->write('[', $this->writer->punctuationStyle); |
306
|
|
|
$this->write($msg, $this->writer->skippedStyle); |
307
|
|
|
$this->write(']', $this->writer->punctuationStyle); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @param Phase_Result $phaseResult |
312
|
|
|
*/ |
313
|
|
|
protected function showActivityForPhase(Story $story, Phase_Result $phaseResult) |
|
|
|
|
314
|
|
|
{ |
315
|
|
|
// what is the phase that we are dealing with? |
316
|
|
|
$phaseName = $phaseResult->getPhaseName(); |
317
|
|
|
$activityLog = $phaseResult->activityLog; |
318
|
|
|
|
319
|
|
|
// our final messages to show the user |
320
|
|
|
$codePoints = []; |
|
|
|
|
321
|
|
|
$trace = null; |
322
|
|
|
|
323
|
|
|
// does the phase have an exception? |
324
|
|
|
$e = $phaseResult->getException(); |
325
|
|
|
if ($e) |
326
|
|
|
{ |
327
|
|
|
$stackTrace = $e->getTrace(); |
|
|
|
|
328
|
|
|
$trace = $e->getTraceAsString(); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
// let's tell the user what we found |
332
|
|
|
$this->write("=============================================================" . PHP_EOL, $this->writer->commentStyle); |
333
|
|
|
$this->write("DETAILED ERROR REPORT" . PHP_EOL); |
334
|
|
|
$this->write("----------------------------------------" . PHP_EOL . PHP_EOL, $this->writer->commentStyle); |
335
|
|
|
|
336
|
|
|
$this->write("The story failed in the "); |
337
|
|
|
$this->write($phaseName, $this->writer->failedPhaseStyle); |
338
|
|
|
$this->write(" phase." . PHP_EOL); |
339
|
|
|
|
340
|
|
|
if (!empty($activityLog)) { |
341
|
|
|
$this->write(PHP_EOL . "-----" . PHP_EOL, $this->writer->commentStyle); |
342
|
|
|
$this->write("This is the detailed output from the "); |
343
|
|
|
$this->write($phaseName, $this->writer->failedPhaseStyle); |
344
|
|
|
$this->write(" phase:" . PHP_EOL . PHP_EOL); |
345
|
|
|
|
346
|
|
|
foreach ($activityLog as $msg) { |
347
|
|
|
$this->writeActivity($msg['text'], $msg['codeLine'], $msg['ts']); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
if ($trace !== null) { |
352
|
|
|
$this->write(PHP_EOL . "-----" . PHP_EOL, $this->writer->commentStyle); |
353
|
|
|
$this->write("This is the stack trace for this failure:" |
354
|
|
|
. PHP_EOL . PHP_EOL |
355
|
|
|
. CodeFormatter::indentBySpaces($trace, 4) . PHP_EOL . PHP_EOL); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
// all done |
359
|
|
|
$this->write("----------------------------------------" . PHP_EOL, $this->writer->commentStyle); |
360
|
|
|
$this->write("END OF ERROR REPORT" . PHP_EOL); |
361
|
|
|
$this->write("=============================================================" . PHP_EOL . PHP_EOL, $this->writer->commentStyle); |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.