Completed
Push — master ( 16ddfb...0ea243 )
by Henry
09:18
created

tests/unit/TestCaseAbstract.php (1 issue)

Check for classes that have been defined more than once.

Best Practice Comprehensibility Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Tests;
3
4
use PHPUnitProviderAutoloader;
5
use Redaxscript\Config;
6
use Redaxscript\Db;
7
use Redaxscript\Installer;
8
use Redaxscript\Language;
9
use Redaxscript\Model;
10
use Redaxscript\Modules\TestDummy;
11
use Redaxscript\Registry;
12
use Redaxscript\Request;
13
use ReflectionClass;
14
use function chr;
15
use function file_exists;
16
use function file_get_contents;
17
use function function_exists;
18
use function getenv;
19
use function json_decode;
20
use function str_replace;
21
use function xdebug_get_headers;
22
23
/**
24
 * TestCaseAbstract
25
 *
26
 * @since 2.2.0
27
 *
28
 * @package Redaxscript
29
 * @category Tests
30
 * @author Henry Ruhs
31
 */
32
33
abstract class TestCaseAbstract extends PHPUnitProviderAutoloader\TestCaseAbstract
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Redaxscript\Tests\TestCaseAbstract has been defined more than once; this definition is ignored, only the first definition in tests/acceptance/TestCaseAbstract.php (L24-108) is considered.

This check looks for classes that have been defined more than once.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
34
{
35
	/**
36
	 * instance of the registry class
37
	 *
38
	 * @var Registry
39
	 */
40
41
	protected $_registry;
42
43
	/**
44
	 * instance of the request class
45
	 *
46
	 * @var Request
47
	 */
48
49
	protected $_request;
50
51
	/**
52
	 * instance of the language class
53
	 *
54
	 * @var Language
55
	 */
56
57
	protected $_language;
58
59
	/**
60
	 * instance of the config class
61
	 *
62
	 * @var Config
63
	 */
64
65
	protected $_config;
66
67
	/**
68
	 * namespace of the testing suite
69
	 *
70
	 * @var string
71
	 */
72
73
	protected $_testNamespace = __NAMESPACE__;
74
75
	/**
76
	 * setUp
77
	 *
78
	 * @since 3.1.0
79
	 */
80
81
	public function setUp() : void
82
	{
83
		Db::clearCache();
84
		$this->_registry = Registry::getInstance();
85
		$this->_request = Request::getInstance();
86
		$this->_language = Language::getInstance();
87
		$this->_config = Config::getInstance();
88
	}
89
90
	/**
91
	 * installerFactory
92
	 *
93
	 * @since 3.1.0
94
	 *
95
	 * @return Installer
96
	 */
97
98
	public function installerFactory() : Installer
99
	{
100
		return new Installer($this->_registry, $this->_request, $this->_language, $this->_config);
101
	}
102
103
	/**
104
	 * settingFactory
105
	 *
106
	 * @since 3.3.0
107
	 *
108
	 * @return Model\Setting
109
	 */
110
111
	public function settingFactory() : Model\Setting
112
	{
113
		return new Model\Setting();
114
	}
115
116
	/**
117
	 * createDatabase
118
	 *
119
	 * @since 4.0.0
120
	 */
121
122
	public function createDatabase() : void
123
	{
124
		$installer = $this->installerFactory();
125
		$installer->init();
126
		$installer->rawCreate();
127
	}
128
129
	/**
130
	 * dropDatabase
131
	 *
132
	 * @since 4.0.0
133
	 */
134
135
	public function dropDatabase() : void
136
	{
137
		$installer = $this->installerFactory();
138
		$installer->init();
139
		$installer->rawDrop();
140
	}
141
142
	/**
143
	 * installTestDummy
144
	 *
145
	 * @since 4.0.0
146
	 */
147
148
	public function installTestDummy() : void
149
	{
150
		$testDummy = new TestDummy\TestDummy($this->_registry, $this->_request, $this->_language, $this->_config);
151
		$testDummy->install();
152
	}
153
154
	/**
155
	 * uninstallTestDummy
156
	 *
157
	 * @since 4.0.0
158
	 */
159
160
	public function uninstallTestDummy() : void
161
	{
162
		$testDummy = new TestDummy\TestDummy($this->_registry, $this->_request, $this->_language, $this->_config);
163
		$testDummy->clearNotification('success');
164
		$testDummy->clearNotification('warning');
165
		$testDummy->clearNotification('error');
166
		$testDummy->clearNotification('info');
167
		$testDummy->uninstall();
168
	}
169
170
	/**
171
	 * getJSON
172
	 *
173
	 * @since 4.0.0
174
	 *
175
	 * @param string $file
176
	 *
177
	 * @return array|null
178
	 */
179
180
	public function getJSON(string $file = null) : ?array
181
	{
182
		if (file_exists($file))
183
		{
184
			$content = file_get_contents($file);
185
			return json_decode($content, true);
186
		}
187
	}
188
189
	/**
190
	 * getProperty
191
	 *
192
	 * @since 3.0.0
193
	 *
194
	 * @param object $object
195
	 * @param string $property
196
	 *
197
	 * @return array|object
198
	 */
199
200
	public function getProperty(object $object = null, string $property = null)
201
	{
202
		$reflection = new ReflectionClass($object);
203
		$reflectionProperty = $reflection->getProperty($property);
204
		$reflectionProperty->setAccessible(true);
205
		return $reflectionProperty->getValue($object);
206
	}
207
208
	/**
209
	 * callMethod
210
	 *
211
	 * @since 3.0.0
212
	 *
213
	 * @param object $object
214
	 * @param string $method
215
	 * @param array $argumentArray
216
	 *
217
	 * @return array|object
218
	 */
219
220
	public function callMethod(object $object = null, string $method = null, array $argumentArray = [])
221
	{
222
		$reflection = new ReflectionClass($object);
223
		$reflectionMethod = $reflection->getMethod($method);
224
		$reflectionMethod->setAccessible(true);
225
		return $reflectionMethod->invokeArgs($object, $argumentArray);
226
	}
227
228
	/**
229
	 * normalizeSeparator
230
	 *
231
	 * @since 3.2.0
232
	 *
233
	 * @param string $actual
234
	 *
235
	 * @return string
236
	 */
237
238
	public function normalizeSeparator(string $actual = null) : string
239
	{
240
		return str_replace(DIRECTORY_SEPARATOR, chr(47), $actual);
241
	}
242
243
	/**
244
	 * normalizeNewline
245
	 *
246
	 * @since 3.0.0
247
	 *
248
	 * @param string $actual
249
	 *
250
	 * @return string
251
	 */
252
253
	public function normalizeNewline(string $actual = null) : string
254
	{
255
		return str_replace(PHP_EOL, chr(10), $actual);
256
	}
257
258
	/**
259
	 * getHeaderArray
260
	 *
261
	 * @since 3.0.0
262
	 *
263
	 * @return array|null
264
	 */
265
266
	public function getHeaderArray() : ?array
267
	{
268
		if (function_exists('xdebug_get_headers'))
269
		{
270
			return xdebug_get_headers();
271
		}
272
		$this->markTestSkipped();
273
		return null;
274
	}
275
276
	/**
277
	 * skipOnEnv
278
	 *
279
	 * @since 4.0.0
280
	 *
281
	 * @param string|null $env
282
	 */
283
284
	public function skipOnEnv(string $env = null) : void
285
	{
286
		if (getenv($env))
287
		{
288
			$this->markTestSkipped();
289
		}
290
	}
291
}
292