Completed
Push — master ( f9b947...7cc206 )
by Kevin
09:20 queued 06:32
created

Initializer::configureDi()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 50
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

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