Completed
Push — master ( 9ad6b1...7b0007 )
by Kevin
05:22 queued 02:37
created

Initializer::initialize()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 45
rs 8.439
cc 5
eloc 29
nc 8
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 $testCaseConfigurationObject;
26
    protected $testCaseConfiguration;
27
28
    public function __construct(
29
        $testCaseConfigurationType,
30
        TestCaseConfiguration $object = null
31
    )
32
    {
33
        $this->testCaseConfiguration = $testCaseConfigurationType;
34
        $this->testCaseConfigurationObject = $object;
35
    }
36
37
    public function initialize(AbstractTestCase $testCase)
38
    {
39
        $this->configureDi($testCase);
40
        $testCase->getDi()->instanceManager()->addSharedInstance(AbstractTestCase::getMasterListener(), 'Magium\Util\Phpunit\MasterListener');
41
42
        $rc = new \ReflectionClass($testCase);
43
        while ($rc->getParentClass()) {
44
            $class = $rc->getParentClass()->getName();
45
            $testCase->getDi()->instanceManager()->addSharedInstance($testCase, $class);
46
            $rc = new \ReflectionClass($class);
47
        }
48
        $webDriver = $testCase->getDi()->get('Magium\WebDriver\WebDriver');
49
        if ($webDriver instanceof WebDriver) {
50
            $testCase->setWebdriver($webDriver);
51
        } else {
52
            throw new InvalidConfigurationException('DIC has misconfigured WebDriver object');
53
        }
54
55
        $remote = $testCase->getDi()->get('Magium\WebDriver\LoggingRemoteExecuteMethod');
56
        if ($remote instanceof LoggingRemoteExecuteMethod) {
57
            $testCase->getWebdriver()->setRemoteExecuteMethod($remote);
58
        } else {
59
            throw new InvalidConfigurationException('DIC has invalid logger configured');
60
        }
61
62
        // This is going to be refactored in a completely backwards compatible way.  Currently, because the DiC is
63
        // rebuilt for each request it doesn't maintain state between tests.  This is a good thing... except when
64
        // something that understands it (the MasterListener) does restain state.
65
66
        $clairvoyant = $this->initClairvoyant($testCase);
67
68
        /* @var $clairvoyant \Magium\Util\Api\Clairvoyant\Clairvoyant */
69
        $request = $testCase->get('Magium\Util\Api\Request');
70
        if ($request instanceof Request) {
71
            $clairvoyant->setApiRequest($request);
72
        }
73
        $clairvoyant->reset();
74
        $clairvoyant->setSessionId($testCase->getWebdriver()->getSessionID());
75
        $clairvoyant->setCapability($this->testCaseConfigurationObject->getCapabilities());
76
        $testCase->getLogger()->addWriter($clairvoyant);
77
        $testCase->getLogger()->addCharacteristic(Logger::CHARACTERISTIC_BROWSER, $testCase->getWebdriver()->getBrowser());
78
        $testCase->getLogger()->addCharacteristic(Logger::CHARACTERISTIC_OPERATING_SYSTEM, $testCase->getWebdriver()->getPlatform());
79
80
        RegistrationListener::executeCallbacks($testCase);
81
    }
82
83
84
    protected function initClairvoyant(AbstractTestCase $testCase)
85
    {
86
        $clairvoyant = AbstractTestCase::getMasterListener()->getListener('Magium\Util\Api\Clairvoyant\Clairvoyant');
87
        if ($clairvoyant instanceof Clairvoyant) {
88
            $testCase->getDi()->instanceManager()->addSharedInstance($clairvoyant, get_class($clairvoyant));
89
        } else {
90
            $clairvoyant = $testCase->get('Magium\Util\Api\Clairvoyant\Clairvoyant');
91
            if ($clairvoyant instanceof Clairvoyant) {
92
                AbstractTestCase::getMasterListener()->addListener($clairvoyant);
93
            } else {
94
                throw new InvalidConfigurationException('Invalid Clairvoyant preference');
95
            }
96
        }
97
        return $clairvoyant;
98
    }
99
100
    protected function getDefaultConfiguration()
101
    {
102
        return [
103
            'definition' => [
104
                'class' => [
105
                    'Magium\WebDriver\WebDriver' => [
106
                        'instantiator' => 'Magium\WebDriver\WebDriverFactory::create'
107
                    ],
108
109
                    'Magium\WebDriver\WebDriverFactory' => [
110
                        'create'       => $this->testCaseConfigurationObject->getWebDriverConfiguration()
111
                    ]
112
                ]
113
            ],
114
            'instance'  => [
115
                'preference' => [
116
                    'Zend\I18n\Translator\Translator' => ['Magium\Util\Translator\Translator']
117
                ],
118
                'Magium\Util\Translator\Translator' => [
119
                    'parameters'    => [
120
                        'locale'    => 'en_US'
121
                    ]
122
                ],
123
                'Magium\Util\Log\Logger'   => [
124
                    'parameters'    => [
125
                        'options'   => [
126
                            'writers' => [
127
                                [
128
                                    'name' => 'Zend\Log\Writer\Noop',
129
                                    'options' => []
130
                                ]
131
                            ]
132
                        ]
133
                    ]
134
                ]
135
            ]
136
        ];
137
    }
138
139
    public function configureDi(AbstractTestCase $testCase)
140
    {
141
        if (!$this->testCaseConfigurationObject instanceof TestCaseConfiguration) {
142
            if ($testCase->getDi() instanceof Di) {
143
                $testCaseConfiguration = $this->get($this->testCaseConfiguration);
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Magium\TestCase\Initializer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
                if ($testCaseConfiguration instanceof TestCaseConfiguration) {
145
                    $this->testCaseConfigurationObject = $testCaseConfiguration;
146
                }
147
            } else {
148
                $this->testCaseConfigurationObject = new $this->testCaseConfiguration(
149
                    new StandardConfigurationProvider(
150
                        new ConfigurationReader(),
151
                        new ClassConfigurationReader(),
152
                        new EnvironmentConfigurationReader()
153
                    )
154
                    , new DefaultPropertyCollector()
155
                );
156
            }
157
        }
158
        if ($testCase->getDi() instanceof Di) {
159
            return;
160
        }
161
        /* @var $configuration TestCaseConfiguration */
162
        $configArray = $this->getDefaultConfiguration();
163
164
        $count = 0;
165
166
        $path = realpath(__DIR__ . '/../');
167
168
        while ($count++ < 5) {
169
            $dir = "{$path}/configuration/";
170
            if (is_dir($dir)) {
171
                foreach (glob($dir . '*.php') as $file) {
172
                    $configArray = array_merge_recursive($configArray, include $file);
173
                }
174
                break;
175
            }
176
            $path .= '/../';
177
        }
178
179
180
        $configArray = $this->testCaseConfigurationObject->reprocessConfiguration($configArray);
181
        $configuration = new Config($configArray);
182
183
        $di = new Di();
184
        $configuration->configure($di);
185
        $testCase->setDi($di);
186
    }
187
}