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
|
17 |
|
protected function tearDown(): void |
54
|
|
|
{ |
55
|
17 |
|
if ($this->is_driver_active()) { |
56
|
7 |
|
$this->driver()->clear(); |
57
|
|
|
} |
58
|
|
|
|
59
|
17 |
|
if ($this->is_environment_active()) { |
60
|
|
|
$this->environment()->restore(); |
61
|
|
|
} |
62
|
|
|
|
63
|
17 |
|
parent::tearDown(); |
64
|
17 |
|
} |
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
|
7 |
|
public function driver(): Driver |
73
|
|
|
{ |
74
|
7 |
|
if (!$this->_driver) { |
75
|
7 |
|
$type = $this->driver_type(); |
76
|
|
|
|
77
|
7 |
|
if (!isset(self::$_drivers[$type])) { |
78
|
3 |
|
self::$_drivers[$type] = $this->getDriverFromType($type); |
79
|
|
|
} |
80
|
|
|
|
81
|
7 |
|
$this->_driver = self::$_drivers[$type]; |
82
|
|
|
} |
83
|
|
|
|
84
|
7 |
|
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_phantomjs(): Driver_Phantomjs |
103
|
|
|
{ |
104
|
1 |
|
return new Driver_Phantomjs(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the type of the driver for the current test. |
109
|
|
|
* Use annotations to change the driver type e.g. @driver phantomjs. |
110
|
|
|
*/ |
111
|
7 |
|
public function driver_type(): string |
112
|
|
|
{ |
113
|
7 |
|
if ($this->_driver_type === null) { |
114
|
7 |
|
$annotations = $this->getAnnotations(); |
115
|
|
|
|
116
|
7 |
|
$this->_driver_type = $annotations['method']['driver'][0] ?? false; |
117
|
|
|
} |
118
|
|
|
|
119
|
7 |
|
return $this->_driver_type; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Return the environment object that handles setting / restoring env variables. |
124
|
|
|
*/ |
125
|
|
|
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
|
17 |
|
public function is_driver_active(): bool |
142
|
|
|
{ |
143
|
17 |
|
return (bool) $this->_driver; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Return true if the environment has been modified / accessed. |
148
|
|
|
*/ |
149
|
17 |
|
public function is_environment_active(): bool |
150
|
|
|
{ |
151
|
17 |
|
return (bool) $this->_environment; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Return the root node of the current page, opened by the driver |
156
|
|
|
* Extend it with custom assertions from Assert. |
157
|
|
|
*/ |
158
|
6 |
|
public function page(): Page |
159
|
|
|
{ |
160
|
6 |
|
$page = $this->driver()->page(); |
161
|
6 |
|
$page->extension('Openbuildings\PHPUnitSpiderling\Assert'); |
162
|
|
|
|
163
|
6 |
|
return $page; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* All other methods are handled by the root node of the page. |
168
|
|
|
* |
169
|
|
|
* @param string $method |
170
|
|
|
* @param array $args |
171
|
|
|
* |
172
|
|
|
* @return mixed |
173
|
|
|
*/ |
174
|
5 |
|
public function __call($method, $args) |
175
|
|
|
{ |
176
|
5 |
|
return \call_user_func_array([$this->page(), $method], $args); |
177
|
|
|
} |
178
|
|
|
|
179
|
3 |
|
private function getDriverFromType(string $type): Driver |
180
|
|
|
{ |
181
|
3 |
|
switch ($type) { |
182
|
3 |
|
case 'simple': |
183
|
1 |
|
return $this->driver_simple(); |
184
|
|
|
|
185
|
2 |
|
case 'simplexml': |
186
|
|
|
return $this->driver_simple_xml(); |
187
|
|
|
|
188
|
2 |
|
case 'kohana': |
189
|
1 |
|
return $this->driver_kohana(); |
190
|
|
|
|
191
|
1 |
|
case 'phantomjs': |
192
|
1 |
|
return $this->driver_phantomjs(); |
193
|
|
|
|
194
|
|
|
default: |
195
|
|
|
throw new \Exception("Driver '{$type}' does not exist"); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|