Completed
Push — master ( 7243b0...3eb70f )
by Haralan
32:35 queued 30:47
created

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