Completed
Push — update ( 3c072c...67888e )
by Haralan
06:13
created

TestCase::driver_kohana()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Openbuildings\PHPUnitSpiderling;
4
5
/**
6
 * Base TestCase
7
 */
8
abstract class TestCase extends \PHPUnit\Framework\TestCase {
9
10
	/**
11
	 * Holds drivers fixtures
12
	 * @var array
13
	 */
14
	protected static $_drivers = array();
15
16
	/**
17
	 * Current Driver for this testcase
18
	 * @var Openbuildings\Spiderling\Driver
19
	 */
20
	protected $_driver;
21
22
	/**
23
	 * The type of the spiderling driver (kohana, selenium ...)
24
	 * @var string
25
	 */
26
	protected $_driver_type;
27
28
	/**
29
	 * The Environment object making sure you can set env variables and restore them after the test
30
	 * @var Openbuildings\EnvironmentBackup\Environment
31
	 */
32
	protected $_environment;
33
34
	/**
35
	 * Restore environment and clear the specific driver if its active
36
	 */
37
	public function tearDown()
38
	{
39
		if ($this->is_driver_active())
40
		{
41
			$this->driver()->clear();
42
		}
43
44
		if ($this->is_environment_active())
45
		{
46
			$this->environment()->restore();
47
		}
48
49
		parent::tearDown();
50
	}
51
52
	/**
53
	 * Return the current driver. This will use driver_simple, driver_kohana ... methods
54
	 * You can override them yourself in order to have custom configs
55
	 *
56
	 * Drivers are cached as fixtured for the whole testrun and is shared between tests.
57
	 * @return Openbuildings\Spiderling\Driver
58
	 */
59
	public function driver()
60
	{
61
		if ( ! $this->_driver)
62
		{
63
			$type = $this->driver_type();
64
65
			if (isset(self::$_drivers[$type]))
66
			{
67
				$this->_driver = self::$_drivers[$type];
68
			}
69
			else
70
			{
71
				switch ($type)
72
				{
73
					case 'simple':
74
						$driver = $this->driver_simple();
75
					break;
76
77
					case 'simplexml':
78
						$driver = $this->driver_simple_xml();
79
					break;
80
81
					case 'kohana':
82
						$driver = $this->driver_kohana();
83
					break;
84
85
					case 'phantomjs':
86
						$driver = $this->driver_phantomjs();
87
					break;
88
89
					case 'selenium':
90
						$driver = $this->driver_selenium();
91
					break;
92
93
					default:
94
						throw new \Exception("Driver '{$type}' does not exist");
95
				}
96
				$this->_driver = self::$_drivers[$type] = $driver;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::$_drivers[$type] = $driver of type object<Openbuildings\Spiderling\Driver_Phantomjs> or object<Openbuildings\Spiderling\Driver_Selenium> or object<Openbuildings\Spiderling\Driver_Simple> is incompatible with the declared type object<Openbuildings\PHP...ings\Spiderling\Driver> of property $_driver.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
97
			}
98
		}
99
100
		return $this->_driver;
101
	}
102
103
	/**
104
	 * Return Openbuildings\Spiderling\Driver_Simple
105
	 * override this to configure
106
	 *
107
	 * @return Openbuildings\Spiderling\Driver_Simple
108
	 */
109
	public function driver_simple()
110
	{
111
		return new \Openbuildings\Spiderling\Driver_Simple();
112
	}
113
114
	/**
115
	 * Return Openbuildings\Spiderling\Driver_SimpleXML
116
	 * override this to configure
117
	 *
118
	 * @return Openbuildings\Spiderling\Driver_SimpleXML
119
	 */
120
	public function driver_simple_xml()
121
	{
122
		return new \Openbuildings\Spiderling\Driver_SimpleXML();
123
	}
124
125
	/**
126
	 * Return Openbuildings\Spiderling\Driver_Kohana
127
	 * override this to configure
128
	 *
129
	 * @return Openbuildings\Spiderling\Driver_Kohana
130
	 */
131
	public function driver_kohana()
132
	{
133
		return new \Openbuildings\Spiderling\Driver_Kohana();
134
	}
135
136
	/**
137
	 * Return Openbuildings\Spiderling\Driver_Selenium
138
	 * override this to configure
139
	 *
140
	 * @return Openbuildings\Spiderling\Driver_Selenium
141
	 */
142
	public function driver_selenium()
143
	{
144
		return new \Openbuildings\Spiderling\Driver_Selenium();
145
	}
146
147
	/**
148
	 * Return Openbuildings\Spiderling\Driver_Phantomjs
149
	 * override this to configure
150
	 *
151
	 * @return Openbuildings\Spiderling\Driver_Phantomjs
152
	 */
153
	public function driver_phantomjs()
154
	{
155
		return new \Openbuildings\Spiderling\Driver_Phantomjs();
156
	}
157
158
	/**
159
	 * Get the type of the driver for the current test.
160
	 * Use annotations to change the driver type e.g. @driver selenium
161
	 *
162
	 * @return string
163
	 */
164
	public function driver_type()
165
	{
166
		if ($this->_driver_type === NULL)
167
		{
168
			$annotations = $this->getAnnotations();
169
170
			$this->_driver_type = isset($annotations['method']['driver'][0]) ? $annotations['method']['driver'][0] : FALSE;
171
		}
172
173
		return $this->_driver_type;
174
	}
175
176
	/**
177
	 * return the environment object that handles setting / restoring env variables
178
	 * @return Openbuildings\EnvrionmentBackup\Envrionment
179
	 */
180
	public function environment()
181
	{
182
		if ($this->_environment === NULL)
183
		{
184
			$this->_environment = new \Openbuildings\EnvironmentBackup\Environment(array(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Openbuildings\Envir...onment_Group_Static())) of type object<Openbuildings\Env...mentBackup\Environment> is incompatible with the declared type object<Openbuildings\PHP...mentBackup\Environment> of property $_environment.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
185
				'globals' => new \Openbuildings\EnvironmentBackup\Environment_Group_Globals(),
186
				'server' => new \Openbuildings\EnvironmentBackup\Environment_Group_Server(),
187
				'static' => new \Openbuildings\EnvironmentBackup\Environment_Group_Static(),
188
			));
189
		}
190
		return $this->_environment;
191
	}
192
193
	/**
194
	 * Return true if the driver has been invoked in some way
195
	 * @return boolean
196
	 */
197
	public function is_driver_active()
198
	{
199
		return (bool) $this->_driver;
200
	}
201
202
	/**
203
	 * Return true if the environment has been modified / accessed
204
	 * @return boolean
205
	 */
206
	public function is_environment_active()
207
	{
208
		return (bool) $this->_environment;
209
	}
210
211
	/**
212
	 * Return the root node of the current page, opened by the driver
213
	 * Extend it with custom assertions from Assert
214
	 * @return Openbuildings\Spiderling\Page
215
	 */
216
	public function page()
217
	{
218
		$page = $this->driver()->page();
219
		$page->extension('Openbuildings\PHPUnitSpiderling\Assert');
220
221
		return $page;
222
	}
223
224
	/**
225
	 * All other methods are handled by the root node of the page
226
	 * @param  string $method
227
	 * @param  array $args
228
	 * @return mixed
229
	 */
230
	public function __call($method, $args)
231
	{
232
		return call_user_func_array(array($this->page(), $method), $args);
233
	}
234
}
235