|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BVCR\Behat\EventDispatcher; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Behat\EventDispatcher\Event\BeforeOutlineTested; |
|
6
|
|
|
use Behat\Behat\EventDispatcher\Event\ExampleTested; |
|
7
|
|
|
use Behat\Behat\EventDispatcher\Event\OutlineTested; |
|
8
|
|
|
use Behat\Gherkin\Node\OutlineNode; |
|
9
|
|
|
use Symfony\Component\EventDispatcher\Event; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
11
|
|
|
use Behat\Behat\EventDispatcher\Event\ScenarioTested; |
|
12
|
|
|
use VCR\Videorecorder; |
|
13
|
|
|
use BVCR\Behat\Resolver\ConfigurationResolver; |
|
14
|
|
|
use BVCR\Behat\Resolver\Configuration; |
|
15
|
|
|
|
|
16
|
|
|
class VCRSubscriber implements EventSubscriberInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var \VCR\Videorecorder */ |
|
19
|
|
|
private $videorecorder; |
|
20
|
|
|
/** @var \BVCR\Behat\Resolver\ConfigurationResolver */ |
|
21
|
|
|
private $configurationResolver; |
|
22
|
|
|
/** @var OutlineNode */ |
|
23
|
|
|
private $outline; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
public function __construct(Videorecorder $videorecorder, ConfigurationResolver $configurationResolver) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
$this->videorecorder = $videorecorder; |
|
29
|
|
|
$this->configurationResolver = $configurationResolver; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function getSubscribedEvents() |
|
33
|
|
|
{ |
|
34
|
|
|
return array( |
|
35
|
|
|
ScenarioTested::BEFORE => 'turnOnVCR', |
|
36
|
|
|
ScenarioTested::AFTER => 'turnOffVCR', |
|
37
|
|
|
ExampleTested::BEFORE => 'turnOnVCR', |
|
38
|
|
|
ExampleTested::AFTER => 'turnOffVCR', |
|
39
|
|
|
OutlineTested::BEFORE => 'captureOutlineOnBeforeOutlineEvent', |
|
40
|
|
|
OutlineTested::AFTER => 'forgetOutlineOnAfterOutlineEvent', |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function turnOnVCR(ScenarioTested $event) |
|
45
|
|
|
{ |
|
46
|
|
|
$configuration = $this->configurationResolver->resolve($event->getScenario()); |
|
47
|
|
|
|
|
48
|
|
|
if ($configuration) { |
|
49
|
|
|
$this->videorecorder->turnOn(); |
|
50
|
|
|
$this->configureVCR($configuration, $event); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Captures outline into the ivar on outline BEFORE event. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Event $event |
|
58
|
|
|
*/ |
|
59
|
|
|
public function captureOutlineOnBeforeOutlineEvent(Event $event) |
|
60
|
|
|
{ |
|
61
|
|
|
if (!$event instanceof BeforeOutlineTested) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->outline = $event->getOutline(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Removes outline from the ivar on outline AFTER event. |
|
70
|
|
|
* |
|
71
|
|
|
* @param Event $event |
|
72
|
|
|
*/ |
|
73
|
|
|
public function forgetOutlineOnAfterOutlineEvent(Event $event) |
|
74
|
|
|
{ |
|
75
|
|
|
if (!$event instanceof BeforeOutlineTested) { |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->outline = null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function configureVCR(Configuration $configuration, ScenarioTested $event) |
|
83
|
|
|
{ |
|
84
|
|
|
$currentVcrConfiguration = $this->videorecorder->configure(); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
if ($configuration->getCassettePath()) { |
|
87
|
|
|
$currentVcrConfiguration->setCassettePath($configuration->getCassettePath()); |
|
88
|
|
|
} |
|
89
|
|
|
if ($configuration->getCassetteStorage()) { |
|
90
|
|
|
$currentVcrConfiguration->setStorage($configuration->getCassetteStorage()); |
|
91
|
|
|
} |
|
92
|
|
|
if ($configuration->getLibraryHooks()) { |
|
93
|
|
|
$currentVcrConfiguration->enableLibraryHooks($configuration->getLibraryHooks()); |
|
94
|
|
|
} |
|
95
|
|
|
if ($configuration->getRequestMatchers()) { |
|
96
|
|
|
$currentVcrConfiguration->enableRequestMatchers($configuration->getRequestMatchers()); |
|
97
|
|
|
} |
|
98
|
|
|
if ($configuration->getMode()) { |
|
99
|
|
|
$currentVcrConfiguration->setMode($configuration->getMode()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$filename = $configuration->getFileNamingStrategy()->createFilename( |
|
103
|
|
|
$event->getFeature(), |
|
104
|
|
|
$event->getScenario(), |
|
105
|
|
|
$this->outline |
|
106
|
|
|
); |
|
107
|
|
|
$this->videorecorder->insertCassette($filename); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function turnOffVCR() |
|
111
|
|
|
{ |
|
112
|
|
|
$this->videorecorder->turnOff(); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.