-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathAbstractUnitTestCase.php
More file actions
229 lines (201 loc) · 5.69 KB
/
Copy pathAbstractUnitTestCase.php
File metadata and controls
229 lines (201 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Phalcon\Tests;
use FilesystemIterator;
use PHPUnit\Framework\SkippedTestSuiteError;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ReflectionClass;
use ReflectionException;
use function array_slice;
use function array_unshift;
use function call_user_func_array;
use function extension_loaded;
use function file_exists;
use function func_get_args;
use function gc_collect_cycles;
use function is_dir;
use function is_file;
use function is_object;
use function rmdir;
use function rtrim;
use function sprintf;
use function uniqid;
use function unlink;
use const DIRECTORY_SEPARATOR;
abstract class AbstractUnitTestCase extends TestCase
{
/**
* @param string $fileName
* @param string $stream
*
* @return void
*/
public function assertFileContentsContains(string $fileName, string $stream): void
{
$contents = file_get_contents($fileName);
$this->assertStringContainsString($stream, $contents);
}
/**
* @param string $fileName
* @param string $stream
*
* @return void
*/
public function assertFileContentsEqual(string $fileName, string $stream): void
{
$contents = file_get_contents($fileName);
$this->assertEquals($contents, $stream);
}
/**
* Calls private or protected method.
*
* @param string|object $obj
* @param string $method
*
* @throws ReflectionException
* @return mixed
*/
public function callProtectedMethod(
string | object $obj,
string $method
): mixed {
$reflectionClass = new ReflectionClass($obj);
$reflectionMethod = $reflectionClass->getMethod($method);
$reflectionMethod->setAccessible(true);
if (!is_object($obj)) {
$obj = $reflectionClass->newInstanceWithoutConstructor();
}
// $obj, $method
$args = array_slice(func_get_args(), 2);
array_unshift($args, $obj);
return call_user_func_array(
[$reflectionMethod, 'invoke'],
$args
);
}
/**
* Checks if an extension is loaded and if not, skips the test
*
* @param string $extension The extension to check
*
* @return void
*/
public function checkExtensionIsLoaded(string $extension): void
{
if (true !== extension_loaded($extension)) {
throw new SkippedTestSuiteError(
sprintf("Extension '%s' is not loaded. Skipping test", $extension)
);
}
}
/**
* Returns a directory string with the trailing directory separator
*
* @param string $directory
*
* @return string
*/
public function getDirSeparator(string $directory): string
{
return rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
/**
* Returns a unique file name
*
* @param string $prefix A prefix for the file
* @param string $suffix A suffix for the file
*
* @return string
*/
public function getNewFileName(string $prefix = '', string $suffix = 'log')
{
$prefix = ($prefix) ? $prefix . '_' : '';
$suffix = ($suffix) ?: 'log';
return uniqid($prefix, true) . '.' . $suffix;
}
/**
* Returns the value of a protected property
*
* @param object|string $obj
* @param string $property
*
* @throws ReflectionException
* @return mixed
*/
public function getProtectedProperty(
object | string $obj,
string $property
): mixed {
$reflection = new ReflectionClass($obj);
$property = $reflection->getProperty($property);
$property->setAccessible(true);
return $property->getValue($obj);
}
/**
* Deletes a directory recursively
*
* @param string $directory
*/
public function safeDeleteDirectory(string $directory): void
{
$dirIterator = new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $fileInfo) {
if ($fileInfo->isDir() === true) {
$this->safeDeleteDirectory($fileInfo->getRealPath());
continue;
}
if (
empty($fileInfo->getRealPath()) === false &&
file_exists($fileInfo->getRealPath())
) {
unlink($fileInfo->getRealPath());
}
}
if (is_dir($directory)) {
rmdir($directory);
}
}
/**
* Deletes a file if it exists
*
* @param string $filename
*/
public function safeDeleteFile(string $filename)
{
if (file_exists($filename) && is_file($filename)) {
gc_collect_cycles();
unlink($filename);
}
}
/**
* Sets a protected property
*
* @param object|string $obj
* @param string $property
* @param mixed $value
*
* @throws ReflectionException
* @return void
*/
public function setProtectedProperty(
object | string $obj,
string $property,
mixed $value
): void {
$reflection = new ReflectionClass($obj);
$property = $reflection->getProperty($property);
$property->setAccessible(true);
$property->setValue($obj, $value);
}
}