1 | <?php |
||
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 |
|
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 |
|
86 | |||
87 | 1 | public function driver_simple(): Driver_Simple |
|
91 | |||
92 | public function driver_simple_xml(): Driver_SimpleXML |
||
96 | |||
97 | 1 | public function driver_kohana(): Driver_Kohana |
|
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 |
|
198 | } |
||
199 |