1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\TestCase; |
4
|
|
|
|
5
|
|
|
use Magium\AbstractTestCase; |
6
|
|
|
use Magium\InvalidConfigurationException; |
7
|
|
|
use Magium\TestCaseConfiguration; |
8
|
|
|
use Magium\Util\Api\Clairvoyant\Clairvoyant; |
9
|
|
|
use Magium\Util\Api\Request; |
10
|
|
|
use Magium\Util\Configuration\ClassConfigurationReader; |
11
|
|
|
use Magium\Util\Configuration\ConfigurationCollector\DefaultPropertyCollector; |
12
|
|
|
use Magium\Util\Configuration\ConfigurationProviderInterface; |
13
|
|
|
use Magium\Util\Configuration\ConfigurationReader; |
14
|
|
|
use Magium\Util\Configuration\EnvironmentConfigurationReader; |
15
|
|
|
use Magium\Util\Configuration\StandardConfigurationProvider; |
16
|
|
|
use Magium\Util\Log\Logger; |
17
|
|
|
use Magium\Util\TestCase\RegistrationListener; |
18
|
|
|
use Magium\WebDriver\LoggingRemoteExecuteMethod; |
19
|
|
|
use Magium\WebDriver\WebDriver; |
20
|
|
|
use Zend\Di\Config; |
21
|
|
|
use Zend\Di\Di; |
22
|
|
|
|
23
|
|
|
class Initializer |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
protected $testCaseConfiguration = 'Magium\TestCaseConfiguration'; |
27
|
|
|
protected $testCaseConfigurationObject; |
28
|
|
|
protected $initialized; |
29
|
|
|
protected $configurationProvider; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
$testCaseConfigurationType = null, |
33
|
|
|
TestCaseConfiguration $object = null, |
34
|
|
|
ConfigurationProviderInterface $configurationProvider = null |
35
|
|
|
) |
36
|
|
|
{ |
37
|
|
|
if ($testCaseConfigurationType !== null) { |
38
|
|
|
$this->testCaseConfiguration = $testCaseConfigurationType; |
39
|
|
|
} |
40
|
|
|
if ($object instanceof TestCaseConfiguration) { |
41
|
|
|
$this->testCaseConfigurationObject = $object; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->configurationProvider = $configurationProvider; |
45
|
|
|
if (!$this->configurationProvider instanceof ConfigurationProviderInterface) { |
46
|
|
|
$this->configurationProvider = new StandardConfigurationProvider( |
47
|
|
|
new ConfigurationReader(), |
48
|
|
|
new ClassConfigurationReader(), |
49
|
|
|
new EnvironmentConfigurationReader() |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function initialize(AbstractTestCase $testCase, $force = false) |
56
|
|
|
{ |
57
|
|
|
if ($this->initialized === $testCase && !$force) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
$this->configureDi($testCase); |
61
|
|
|
$testCase->getDi()->instanceManager()->addSharedInstance(AbstractTestCase::getMasterListener(), 'Magium\Util\Phpunit\MasterListener'); |
62
|
|
|
|
63
|
|
|
$rc = new \ReflectionClass($testCase); |
64
|
|
|
while ($rc->getParentClass()) { |
65
|
|
|
$class = $rc->getParentClass()->getName(); |
66
|
|
|
$testCase->getDi()->instanceManager()->addSharedInstance($testCase, $class); |
67
|
|
|
$rc = new \ReflectionClass($class); |
68
|
|
|
} |
69
|
|
|
$webDriver = $testCase->getDi()->get('Magium\WebDriver\WebDriver'); |
70
|
|
|
if ($webDriver instanceof WebDriver) { |
71
|
|
|
$testCase->setWebdriver($webDriver); |
72
|
|
|
$testCase->setTypePreference( |
73
|
|
|
'Facebook\WebDriver\WebDriver', |
74
|
|
|
'Magium\WebDriver\WebDriver' |
75
|
|
|
); |
76
|
|
|
$testCase->setTypePreference( |
77
|
|
|
'Facebook\WebDriver\RemoteWebDriver', |
78
|
|
|
'Magium\WebDriver\WebDriver' |
79
|
|
|
); |
80
|
|
|
} else { |
81
|
|
|
throw new InvalidConfigurationException('DIC has misconfigured WebDriver object'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$remote = $testCase->getDi()->get('Magium\WebDriver\LoggingRemoteExecuteMethod'); |
85
|
|
|
if ($remote instanceof LoggingRemoteExecuteMethod) { |
86
|
|
|
$testCase->getWebdriver()->setRemoteExecuteMethod($remote); |
87
|
|
|
} else { |
88
|
|
|
throw new InvalidConfigurationException('DIC has invalid logger configured'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// This is going to be refactored in a completely backwards compatible way. Currently, because the DiC is |
92
|
|
|
// rebuilt for each request it doesn't maintain state between tests. This is a good thing... except when |
93
|
|
|
// something that understands it (the MasterListener) does restain state. |
94
|
|
|
|
95
|
|
|
$clairvoyant = $this->initClairvoyant($testCase); |
96
|
|
|
|
97
|
|
|
/* @var $clairvoyant \Magium\Util\Api\Clairvoyant\Clairvoyant */ |
98
|
|
|
$request = $testCase->get('Magium\Util\Api\Request'); |
99
|
|
|
if ($request instanceof Request) { |
100
|
|
|
$clairvoyant->setApiRequest($request); |
101
|
|
|
} |
102
|
|
|
$clairvoyant->reset(); |
103
|
|
|
$clairvoyant->setSessionId($testCase->getWebdriver()->getSessionID()); |
104
|
|
|
$clairvoyant->setCapability($this->testCaseConfigurationObject->getCapabilities()); |
105
|
|
|
$testCase->getLogger()->addWriter($clairvoyant); |
106
|
|
|
$testCase->getLogger()->addCharacteristic(Logger::CHARACTERISTIC_BROWSER, $testCase->getWebdriver()->getBrowser()); |
107
|
|
|
$testCase->getLogger()->addCharacteristic(Logger::CHARACTERISTIC_OPERATING_SYSTEM, $testCase->getWebdriver()->getPlatform()); |
108
|
|
|
|
109
|
|
|
RegistrationListener::executeCallbacks($testCase); |
110
|
|
|
$this->initialized = $testCase; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
protected function initClairvoyant(AbstractTestCase $testCase) |
115
|
|
|
{ |
116
|
|
|
$clairvoyant = AbstractTestCase::getMasterListener()->getListener('Magium\Util\Api\Clairvoyant\Clairvoyant'); |
117
|
|
|
if ($clairvoyant instanceof Clairvoyant) { |
118
|
|
|
$testCase->getDi()->instanceManager()->addSharedInstance($clairvoyant, get_class($clairvoyant)); |
119
|
|
|
} else { |
120
|
|
|
$clairvoyant = $testCase->get('Magium\Util\Api\Clairvoyant\Clairvoyant'); |
121
|
|
|
if ($clairvoyant instanceof Clairvoyant) { |
122
|
|
|
AbstractTestCase::getMasterListener()->addListener($clairvoyant); |
123
|
|
|
} else { |
124
|
|
|
throw new InvalidConfigurationException('Invalid Clairvoyant preference'); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
return $clairvoyant; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function getDefaultConfiguration() |
131
|
|
|
{ |
132
|
|
|
return [ |
133
|
|
|
'definition' => [ |
134
|
|
|
'class' => [ |
135
|
|
|
'Magium\WebDriver\WebDriver' => [ |
136
|
|
|
'instantiator' => 'Magium\WebDriver\WebDriverFactory::create' |
137
|
|
|
], |
138
|
|
|
|
139
|
|
|
'Magium\WebDriver\WebDriverFactory' => [ |
140
|
|
|
'create' => $this->testCaseConfigurationObject->getWebDriverConfiguration() |
141
|
|
|
] |
142
|
|
|
] |
143
|
|
|
], |
144
|
|
|
'instance' => [ |
145
|
|
|
'preference' => [ |
146
|
|
|
'Zend\I18n\Translator\Translator' => ['Magium\Util\Translator\Translator'] |
147
|
|
|
], |
148
|
|
|
'Magium\Util\Translator\Translator' => [ |
149
|
|
|
'parameters' => [ |
150
|
|
|
'locale' => 'en_US' |
151
|
|
|
] |
152
|
|
|
], |
153
|
|
|
'Magium\Util\Log\Logger' => [ |
154
|
|
|
'parameters' => [ |
155
|
|
|
'options' => [ |
156
|
|
|
'writers' => [ |
157
|
|
|
[ |
158
|
|
|
'name' => 'Zend\Log\Writer\Noop', |
159
|
|
|
'options' => [] |
160
|
|
|
] |
161
|
|
|
] |
162
|
|
|
] |
163
|
|
|
] |
164
|
|
|
] |
165
|
|
|
] |
166
|
|
|
]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function configureDi(AbstractTestCase $testCase) |
170
|
|
|
{ |
171
|
|
|
|
172
|
|
|
if (!$this->testCaseConfigurationObject instanceof TestCaseConfiguration) { |
173
|
|
|
if ($testCase->getDi() instanceof Di) { |
174
|
|
|
$testCaseConfiguration = $testCase->get($this->testCaseConfiguration); |
175
|
|
|
if ($testCaseConfiguration instanceof TestCaseConfiguration) { |
176
|
|
|
$this->testCaseConfigurationObject = $testCaseConfiguration; |
177
|
|
|
} |
178
|
|
|
} else { |
179
|
|
|
$this->testCaseConfigurationObject = new $this->testCaseConfiguration( |
180
|
|
|
$this->configurationProvider |
181
|
|
|
, new DefaultPropertyCollector() |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
if ($testCase->getDi() instanceof Di) { |
186
|
|
|
return; |
187
|
|
|
} |
188
|
|
|
/* @var $configuration TestCaseConfiguration */ |
189
|
|
|
$configArray = $this->getDefaultConfiguration(); |
190
|
|
|
|
191
|
|
|
$count = 0; |
192
|
|
|
|
193
|
|
|
$path = realpath(__DIR__ . '/../'); |
194
|
|
|
|
195
|
|
|
while ($count++ < 5) { |
196
|
|
|
$dir = "{$path}/configuration/"; |
197
|
|
|
if (is_dir($dir)) { |
198
|
|
|
foreach (glob($dir . '*.php') as $file) { |
199
|
|
|
$configArray = array_merge_recursive($configArray, include $file); |
200
|
|
|
} |
201
|
|
|
break; |
202
|
|
|
} |
203
|
|
|
$path .= '/../'; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
$configArray = $this->testCaseConfigurationObject->reprocessConfiguration($configArray); |
208
|
|
|
$configuration = new Config($configArray); |
209
|
|
|
|
210
|
|
|
$di = new Di(); |
211
|
|
|
$configuration->configure($di); |
212
|
|
|
$testCase->setDi($di); |
213
|
|
|
|
214
|
|
|
$this->setConfigurationProvider($testCase); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function setConfigurationProvider(AbstractTestCase $testCase) |
218
|
|
|
{ |
219
|
|
|
$testCase->setTypePreference( |
220
|
|
|
'Magium\Util\Configuration\ConfigurationProviderInterface', |
221
|
|
|
'Magium\Util\Configuration\StandardConfigurationProvider' |
222
|
|
|
); |
223
|
|
|
$this->configurationProvider->configureDi($testCase->getDi()); |
224
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
} |