TestCase   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 78.18%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 12
dl 0
loc 182
ccs 43
cts 55
cp 0.7818
rs 10
c 0
b 0
f 0

13 Methods

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