Completed
Push — phpunit-7 ( 567d41...24e90d )
by Haralan
06:30
created

TestCase::getDriverFromType()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.1502

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 11
cp 0.8182
rs 9.3222
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5.1502
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_Simple;
13
use Openbuildings\Spiderling\Driver_SimpleXML;
14
use Openbuildings\Spiderling\Page;
15
use PHPUnit\Framework\TestCase as BaseTestCase;
16
17
/**
18
 * Base TestCase.
19
 */
20
abstract class TestCase extends BaseTestCase
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, phantomjs ...).
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
    protected function tearDown(): void
54 16
    {
55
        if ($this->is_driver_active()) {
56 16
            $this->driver()->clear();
57 6
        }
58
59
        if ($this->is_environment_active()) {
60 16
            $this->environment()->restore();
61
        }
62
63
        parent::tearDown();
64 16
    }
65 16
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
    public function driver(): Driver
73 6
    {
74
        if (!$this->_driver) {
75 6
            $type = $this->driver_type();
76 6
77
            if (!isset(self::$_drivers[$type])) {
78 6
                self::$_drivers[$type] = $this->getDriverFromType($type);
79 2
            }
80
81
            $this->_driver = self::$_drivers[$type];
82 6
        }
83
84
        return $this->_driver;
85 6
    }
86
87
    public function driver_simple(): Driver_Simple
88 1
    {
89
        return new Driver_Simple();
90 1
    }
91
92
    public function driver_simple_xml(): Driver_SimpleXML
93
    {
94
        return new Driver_SimpleXML();
95
    }
96
97
    public function driver_kohana(): Driver_Kohana
98
    {
99
        return new Driver_Kohana();
100
    }
101
102
    public function driver_phantomjs(): Driver_Phantomjs
103
    {
104
        return new Driver_Phantomjs();
105
    }
106
107
    /**
108 1
     * Get the type of the driver for the current test.
109
     * Use annotations to change the driver type e.g. @driver phantomjs.
110 1
     */
111
    public function driver_type(): string
112
    {
113
        if ($this->_driver_type === null) {
114
            $annotations = $this->getAnnotations();
115
116
            $this->_driver_type = $annotations['method']['driver'][0] ?? false;
117 6
        }
118
119 6
        return $this->_driver_type;
120 6
    }
121
122 6
    /**
123
     * Return the environment object that handles setting / restoring env variables.
124
     */
125 6
    public function environment(): Environment
126
    {
127
        if ($this->_environment === null) {
128
            $this->_environment = new Environment([
129
                'globals' => new Environment_Group_Globals(),
130
                'server' => new Environment_Group_Server(),
131
                'static' => new Environment_Group_Static(),
132
            ]);
133
        }
134
135
        return $this->_environment;
136
    }
137
138
    /**
139
     * Return true if the driver has been invoked in some way.
140
     */
141
    public function is_driver_active(): bool
142
    {
143
        return (bool) $this->_driver;
144
    }
145
146
    /**
147 16
     * Return true if the environment has been modified / accessed.
148
     */
149 16
    public function is_environment_active(): bool
150
    {
151
        return (bool) $this->_environment;
152
    }
153
154
    /**
155 16
     * Return the root node of the current page, opened by the driver
156
     * Extend it with custom assertions from Assert.
157 16
     */
158
    public function page(): Page
159
    {
160
        $page = $this->driver()->page();
161
        $page->extension('Openbuildings\PHPUnitSpiderling\Assert');
162
163
        return $page;
164 5
    }
165
166 5
    /**
167 5
     * All other methods are handled by the root node of the page.
168
     *
169 5
     * @param string $method
170
     * @param array  $args
171
     *
172
     * @return mixed
173
     */
174
    public function __call($method, $args)
175
    {
176
        return call_user_func_array([$this->page(), $method], $args);
177
    }
178
179
    private function getDriverFromType(string $type): Driver
180 4
    {
181
        switch ($type) {
182 4
            case 'simple':
183
                return $this->driver_simple();
184
185 2
            case 'simplexml':
186
                return $this->driver_simple_xml();
187 2
188 2
            case 'kohana':
189 1
                return $this->driver_kohana();
190
191 1
            case 'phantomjs':
192
                return $this->driver_phantomjs();
193
194 1
            default:
195
                throw new \Exception("Driver '{$type}' does not exist");
196
        }
197 1
    }
198
}
199