|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Behat MinkExtension. |
|
5
|
|
|
* (c) Konstantin Kudryashov <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Behat\MinkExtension\Listener; |
|
12
|
|
|
|
|
13
|
|
|
use Behat\Behat\EventDispatcher\Event\AfterStepTested; |
|
14
|
|
|
use Behat\Behat\EventDispatcher\Event\StepTested; |
|
15
|
|
|
use Behat\Testwork\Tester\Result\ExceptionResult; |
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
17
|
|
|
use Behat\Mink\Mink; |
|
18
|
|
|
use Behat\Mink\Exception\Exception as MinkException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Failed step response show listener. |
|
22
|
|
|
* Listens to failed Behat steps and shows last response in a browser. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class FailureShowListener implements EventSubscriberInterface |
|
27
|
|
|
{ |
|
28
|
|
|
private $mink; |
|
29
|
|
|
private $parameters; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Initializes initializer. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Mink $mink |
|
35
|
|
|
* @param array $parameters |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(Mink $mink, array $parameters) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->mink = $mink; |
|
40
|
|
|
$this->parameters = $parameters; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function getSubscribedEvents() |
|
47
|
|
|
{ |
|
48
|
|
|
return array( |
|
49
|
|
|
StepTested::AFTER => array('showFailedStepResponse', -10) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Shows last response of failed step with preconfigured command. |
|
55
|
|
|
* |
|
56
|
|
|
* Configuration is based on `behat.yml`: |
|
57
|
|
|
* |
|
58
|
|
|
* `show_auto` enable this listener (default to false) |
|
59
|
|
|
* `show_cmd` command to run (`open %s` to open default browser on Mac) |
|
60
|
|
|
* `show_tmp_dir` folder where to store temp files (default is system temp) |
|
61
|
|
|
* |
|
62
|
|
|
* @param AfterStepTested $event |
|
63
|
|
|
* |
|
64
|
|
|
* @throws \RuntimeException if show_cmd is not configured |
|
65
|
|
|
*/ |
|
66
|
|
|
public function showFailedStepResponse(AfterStepTested $event) |
|
67
|
|
|
{ |
|
68
|
|
|
$testResult = $event->getTestResult(); |
|
69
|
|
|
|
|
70
|
|
|
if (!$testResult instanceof ExceptionResult) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (!$testResult->getException() instanceof MinkException) { |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (null === $this->parameters['show_cmd']) { |
|
79
|
|
|
throw new \RuntimeException('Set "show_cmd" parameter in behat.yml to be able to open page in browser (ex.: "show_cmd: open %s")'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$filename = rtrim($this->parameters['show_tmp_dir'], DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.uniqid().'.html'; |
|
83
|
|
|
file_put_contents($filename, $this->mink->getSession()->getPage()->getContent()); |
|
84
|
|
|
system(sprintf($this->parameters['show_cmd'], escapeshellarg($filename))); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|