|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bex\Behat\ScreenshotExtension\ServiceContainer; |
|
4
|
|
|
|
|
5
|
|
|
use Bex\Behat\ExtensionDriverLocator\DriverLocator; |
|
6
|
|
|
use Bex\Behat\ScreenshotExtension\Driver\ImageDriverInterface; |
|
7
|
|
|
use Symfony\Component\Config\FileLocator; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
10
|
|
|
|
|
11
|
|
|
class Config |
|
12
|
|
|
{ |
|
13
|
|
|
const EXTENSION_CONFIG_KEY = 'screenshot'; |
|
14
|
|
|
const CONFIG_CONTAINER_ID = 'bex.screenshot_extension.config'; |
|
15
|
|
|
|
|
16
|
|
|
const IMAGE_DRIVER_NAMESPACE = 'Bex\\Behat\\ScreenshotExtension\\Driver'; |
|
17
|
|
|
const IMAGE_DRIVER_PARENT = 'Bex\\Behat\\ScreenshotExtension\\Driver\\ImageDriverInterface'; |
|
18
|
|
|
|
|
19
|
|
|
const CONFIG_PARAM_EXTENSTION_ENABLED = 'enabled'; |
|
20
|
|
|
const CONFIG_PARAM_ACTIVE_IMAGE_DRIVERS = 'active_image_drivers'; |
|
21
|
|
|
const CONFIG_PARAM_IMAGE_DRIVER_CONFIGS = 'image_drivers'; |
|
22
|
|
|
const CONFIG_PARAM_SCREENSHOT_TAKING_MODE = 'screenshot_taking_mode'; |
|
23
|
|
|
const CONFIG_PARAM_SCREENSHOT_FILENAME_PATTERN = 'screenshot_filename_pattern'; |
|
24
|
|
|
|
|
25
|
|
|
const SCREENSHOT_TAKING_MODE_FAILED_STEPS = 'failed_steps'; |
|
26
|
|
|
const SCREENSHOT_TAKING_MODE_FAILED_SCENARIOS = 'failed_scenarios'; |
|
27
|
|
|
const SCREENSHOT_TAKING_MODE_ALL_SCENARIOS = 'all_scenarios'; |
|
28
|
|
|
|
|
29
|
|
|
const DEFAULT_IMAGE_DRIVER_KEY = 'local'; |
|
30
|
|
|
const DEFAULT_SCREENSHOT_TAKING_MODE = self::SCREENSHOT_TAKING_MODE_FAILED_STEPS; |
|
31
|
|
|
|
|
32
|
|
|
const DEFAULT_SCREENSHOT_FILENAME_PATTERN = '%FEATURE_FILE_PATH%_%SCENARIO_LINE_NUMBER%'; |
|
33
|
|
|
|
|
34
|
|
|
const ERROR_MESSAGE_IMAGICK_NOT_FOUND = 'Imagemagick PHP extension is required, but not installed.'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var DriverLocator |
|
38
|
|
|
*/ |
|
39
|
|
|
private $driverLocator; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
private $imageDrivers; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var boolean |
|
48
|
|
|
*/ |
|
49
|
|
|
private $enabled; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
private $screenshotTakingMode; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var string[] |
|
58
|
|
|
*/ |
|
59
|
|
|
private $imageDriverKeys; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var array |
|
63
|
|
|
*/ |
|
64
|
|
|
private $imageDriverConfigs; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
private $screenshotFilenamePattern; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param array $config |
|
73
|
|
|
*/ |
|
74
|
|
|
public function __construct(array $config) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->driverLocator = DriverLocator::getInstance(self::IMAGE_DRIVER_NAMESPACE, self::IMAGE_DRIVER_PARENT); |
|
77
|
|
|
$this->enabled = $config[self::CONFIG_PARAM_EXTENSTION_ENABLED]; |
|
78
|
|
|
$this->screenshotTakingMode = $config[self::CONFIG_PARAM_SCREENSHOT_TAKING_MODE]; |
|
79
|
|
|
$this->imageDriverKeys = $config[self::CONFIG_PARAM_ACTIVE_IMAGE_DRIVERS]; |
|
80
|
|
|
$this->imageDriverConfigs = $config[self::CONFIG_PARAM_IMAGE_DRIVER_CONFIGS]; |
|
81
|
|
|
$this->screenshotFilenamePattern = $config[self::CONFIG_PARAM_SCREENSHOT_FILENAME_PATTERN]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return boolean |
|
86
|
|
|
*/ |
|
87
|
|
|
public function isEnabled() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->enabled; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getImageDrivers() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->imageDrivers; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return boolean |
|
102
|
|
|
*/ |
|
103
|
|
|
public function shouldCombineImages() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->screenshotTakingMode !== self::SCREENSHOT_TAKING_MODE_FAILED_STEPS; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return boolean |
|
110
|
|
|
*/ |
|
111
|
|
|
public function shouldRecordAllScenarios() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->screenshotTakingMode == self::SCREENSHOT_TAKING_MODE_ALL_SCENARIOS; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return boolean |
|
118
|
|
|
*/ |
|
119
|
|
|
public function shouldRecordAllSteps() |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->screenshotTakingMode != self::SCREENSHOT_TAKING_MODE_FAILED_STEPS; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getScreenshotFilenamePattern() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->screenshotFilenamePattern; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Init service container and load image drivers |
|
134
|
|
|
* |
|
135
|
|
|
* @param ContainerBuilder $container |
|
136
|
|
|
*/ |
|
137
|
|
|
public function loadServices(ContainerBuilder $container) |
|
138
|
|
|
{ |
|
139
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/config')); |
|
140
|
|
|
$loader->load('services.xml'); |
|
141
|
|
|
$this->imageDrivers = $this->driverLocator->findDrivers( |
|
142
|
|
|
$container, |
|
143
|
|
|
$this->imageDriverKeys, |
|
144
|
|
|
$this->imageDriverConfigs |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return string[] |
|
150
|
|
|
*/ |
|
151
|
|
|
public static function getScreenshotTakingModes() |
|
152
|
|
|
{ |
|
153
|
|
|
return [ |
|
154
|
|
|
self::SCREENSHOT_TAKING_MODE_FAILED_STEPS, |
|
155
|
|
|
self::SCREENSHOT_TAKING_MODE_FAILED_SCENARIOS, |
|
156
|
|
|
self::SCREENSHOT_TAKING_MODE_ALL_SCENARIOS |
|
157
|
|
|
]; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return \Closure |
|
162
|
|
|
*/ |
|
163
|
|
|
public static function getScreenshotTakingModeValidator() |
|
164
|
|
|
{ |
|
165
|
|
|
return function ($mode) { |
|
166
|
|
|
return ($mode !== 'failed_steps') && !class_exists('\Imagick'); |
|
167
|
|
|
}; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|