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