1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Behatch\Notifier; |
4
|
|
|
|
5
|
|
|
use Behat\Behat\Context\Context; |
6
|
|
|
use Behat\Behat\Tester\Result\StepResult; |
7
|
|
|
|
8
|
|
|
abstract class Notifier implements Context |
9
|
|
|
{ |
10
|
|
|
protected static $spamTimeout; |
11
|
|
|
|
12
|
|
|
public function __construct($spamTimeout = 60) |
13
|
|
|
{ |
14
|
|
|
self::$spamTimeout = $spamTimeout; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
protected static function notify($status, $title, $message) |
18
|
|
|
{ |
19
|
|
|
throw new \LogicException('You should implement notify() method in your class'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @AfterStep |
24
|
|
|
*/ |
25
|
|
|
public function afterStep($event) |
26
|
|
|
{ |
27
|
|
|
$result = $event->getTestResult(); |
28
|
|
|
|
29
|
|
|
if ($result->getResultCode() === StepResult::FAILED) { |
30
|
|
|
$definition = $result->getStepDefinition(); |
31
|
|
|
|
32
|
|
|
$message = "$definition\n"; |
33
|
|
|
$message .= '> ' . $result->getException()->getMessage(); |
34
|
|
|
$message = str_replace("'", "`", $message); |
35
|
|
|
|
36
|
|
|
self::spamNotify('error', 'Behat step failure', $message); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private static function spamNotify($status, $title, $message) |
41
|
|
|
{ |
42
|
|
|
static $lastTimeError = null; |
43
|
|
|
|
44
|
|
|
if ($lastTimeError === null) { |
45
|
|
|
$lastTimeError = time() - self::$spamTimeout - 100; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (time() - $lastTimeError > self::$spamTimeout) { |
49
|
|
|
static::notify($status, $title, $message); |
50
|
|
|
$lastTimeError = time(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @AfterSuite |
56
|
|
|
*/ |
57
|
|
|
public static function afterSuite($event) |
58
|
|
|
{ |
59
|
|
|
$result = $event->getTestResult(); |
60
|
|
|
|
61
|
|
|
if ($result->getResultCode() === StepResult::FAILED) { |
62
|
|
|
static::notify('sad', 'Behat suite ended', 'Failure'); |
63
|
|
|
} else { |
64
|
|
|
static::notify('smile', 'Behat suite ended', 'Success'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|