Completed
Pull Request — master (#8)
by Haralan
01:58
created

TestCase::driver_selenium()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Openbuildings\PHPUnitSpiderling;
4
5
/**
6
 * Base TestCase.
7
 */
8
abstract class TestCase extends \PHPUnit\Framework\TestCase
9
{
10
    /**
11
     * Holds drivers fixtures.
12
     *
13
     * @var array
14
     */
15
    protected static $_drivers = [];
16
17
    /**
18
     * Current Driver for this testcase.
19
     *
20
     * @var \Openbuildings\Spiderling\Driver
21
     */
22
    protected $_driver;
23
24
    /**
25
     * The type of the spiderling driver (kohana, selenium ...).
26
     *
27
     * @var string
28
     */
29
    protected $_driver_type;
30
31
    /**
32
     * The Environment object making sure you can set env variables and restore them after the test.
33
     *
34
     * @var \Openbuildings\EnvironmentBackup\Environment
35
     */
36
    protected $_environment;
37
38
    /**
39
     * Restore environment and clear the specific driver if its active.
40
     */
41 18
    public function tearDown()
42
    {
43 18
        if ($this->is_driver_active()) {
44 8
            $this->driver()->clear();
45
        }
46
47 18
        if ($this->is_environment_active()) {
48
            $this->environment()->restore();
49
        }
50
51 18
        parent::tearDown();
52 18
    }
53
54
    /**
55
     * Return the current driver. This will use driver_simple, driver_kohana ... methods
56
     * You can override them yourself in order to have custom configs.
57
     *
58
     * Drivers are cached as fixtured for the whole testrun and is shared between tests.
59
     *
60
     * @return \Openbuildings\Spiderling\Driver
61
     */
62 8
    public function driver()
63
    {
64 8
        if (!$this->_driver) {
65 8
            $type = $this->driver_type();
66
67 8
            if (isset(self::$_drivers[$type])) {
68 4
                $this->_driver = self::$_drivers[$type];
69
            } else {
70
                switch ($type) {
71 4
                    case 'simple':
72 1
                        $driver = $this->driver_simple();
73 1
                    break;
74
75 3
                    case 'simplexml':
76
                        $driver = $this->driver_simple_xml();
77
                    break;
78
79 3
                    case 'kohana':
80 1
                        $driver = $this->driver_kohana();
81 1
                    break;
82
83 2
                    case 'phantomjs':
84 1
                        $driver = $this->driver_phantomjs();
85 1
                    break;
86
87 1
                    case 'selenium':
88 1
                        $driver = $this->driver_selenium();
89 1
                    break;
90
91
                    default:
92
                        throw new \Exception("Driver '{$type}' does not exist");
93
                }
94 4
                $this->_driver = self::$_drivers[$type] = $driver;
95
            }
96
        }
97
98 8
        return $this->_driver;
99
    }
100
101
    /**
102
     * Return Openbuildings\Spiderling\Driver_Simple
103
     * override this to configure.
104
     *
105
     * @return \Openbuildings\Spiderling\Driver_Simple
106
     */
107 1
    public function driver_simple()
108
    {
109 1
        return new \Openbuildings\Spiderling\Driver_Simple();
110
    }
111
112
    /**
113
     * Return Openbuildings\Spiderling\Driver_SimpleXML
114
     * override this to configure.
115
     *
116
     * @return \Openbuildings\Spiderling\Driver_SimpleXML
117
     */
118
    public function driver_simple_xml()
119
    {
120
        return new \Openbuildings\Spiderling\Driver_SimpleXML();
121
    }
122
123
    /**
124
     * Return Openbuildings\Spiderling\Driver_Kohana
125
     * override this to configure.
126
     *
127
     * @return \Openbuildings\Spiderling\Driver_Kohana
128
     */
129 1
    public function driver_kohana()
130
    {
131 1
        return new \Openbuildings\Spiderling\Driver_Kohana();
132
    }
133
134
    /**
135
     * Return Openbuildings\Spiderling\Driver_Selenium
136
     * override this to configure.
137
     *
138
     * @return \Openbuildings\Spiderling\Driver_Selenium
139
     */
140 1
    public function driver_selenium()
141
    {
142 1
        return new \Openbuildings\Spiderling\Driver_Selenium();
143
    }
144
145
    /**
146
     * Return Openbuildings\Spiderling\Driver_Phantomjs
147
     * override this to configure.
148
     *
149
     * @return \Openbuildings\Spiderling\Driver_Phantomjs
150
     */
151 1
    public function driver_phantomjs()
152
    {
153 1
        return new \Openbuildings\Spiderling\Driver_Phantomjs();
154
    }
155
156
    /**
157
     * Get the type of the driver for the current test.
158
     * Use annotations to change the driver type e.g. @driver selenium.
159
     *
160
     * @return string
161
     */
162 8
    public function driver_type()
163
    {
164 8
        if ($this->_driver_type === null) {
165 8
            $annotations = $this->getAnnotations();
166
167 8
            $this->_driver_type = $annotations['method']['driver'][0] ?? false;
168
        }
169
170 8
        return $this->_driver_type;
171
    }
172
173
    /**
174
     * return the environment object that handles setting / restoring env variables.
175
     *
176
     * @return \Openbuildings\EnvironmentBackup\Environment
177
     */
178
    public function environment()
179
    {
180
        if ($this->_environment === null) {
181
            $this->_environment = new \Openbuildings\EnvironmentBackup\Environment([
182
                'globals' => new \Openbuildings\EnvironmentBackup\Environment_Group_Globals(),
183
                'server' => new \Openbuildings\EnvironmentBackup\Environment_Group_Server(),
184
                'static' => new \Openbuildings\EnvironmentBackup\Environment_Group_Static(),
185
            ]);
186
        }
187
188
        return $this->_environment;
189
    }
190
191
    /**
192
     * Return true if the driver has been invoked in some way.
193
     *
194
     * @return bool
195
     */
196 18
    public function is_driver_active()
197
    {
198 18
        return (bool) $this->_driver;
199
    }
200
201
    /**
202
     * Return true if the environment has been modified / accessed.
203
     *
204
     * @return bool
205
     */
206 18
    public function is_environment_active()
207
    {
208 18
        return (bool) $this->_environment;
209
    }
210
211
    /**
212
     * Return the root node of the current page, opened by the driver
213
     * Extend it with custom assertions from Assert.
214
     *
215
     * @return \Openbuildings\Spiderling\Page
216
     */
217 7
    public function page()
218
    {
219 7
        $page = $this->driver()->page();
220 7
        $page->extension('Openbuildings\PHPUnitSpiderling\Assert');
221
222 7
        return $page;
223
    }
224
225
    /**
226
     * All other methods are handled by the root node of the page.
227
     *
228
     * @param string $method
229
     * @param array  $args
230
     *
231
     * @return mixed
232
     */
233 6
    public function __call($method, $args)
234
    {
235 6
        return call_user_func_array([$this->page(), $method], $args);
236
    }
237
}
238