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