Completed
Push — master ( 9e5cd8...7c6364 )
by Kevin
02:56
created

Initializer::configureDi()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 53
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 53
rs 7.1199
cc 8
eloc 33
nc 16
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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