This repository has been archived by the owner on Apr 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractTestCase.php
99 lines (84 loc) · 2.8 KB
/
AbstractTestCase.php
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
<?php
namespace PetrKnap\Php\FileStorage\Test;
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
{
/**
* @var string
*/
private static $tempDir;
/**
* @var string
*/
private static $tempPrefix;
/**
* @param string $tempDir
* @throws \Exception
*/
public static function setTempDir($tempDir)
{
if (self::$tempDir) {
throw new \Exception("\$tempDir is read-only.");
}
self::$tempDir = $tempDir;
}
/**
* @param string $tempPrefix
* @throws \Exception
*/
public static function setTempPrefix($tempPrefix)
{
if (self::$tempPrefix) {
throw new \Exception("\$tempPrefix is read-only.");
}
self::$tempPrefix = $tempPrefix;
}
/**
* @return string
*/
protected static function getTemporaryDirectory()
{
$temporaryDirectory = tempnam(self::$tempDir, self::$tempPrefix);
unlink($temporaryDirectory);
return $temporaryDirectory;
}
private static function removeDirectory($directory)
{
$removeDirectoryRecursively = function ($directory) use (&$removeDirectoryRecursively) {
chmod($directory, 0777);
$directoryIterator = new \DirectoryIterator($directory);
$itemIterator = new \IteratorIterator($directoryIterator);
foreach ($itemIterator as $item) {
if ($item->isDir() && !$item->isDot()) {
$removeDirectoryRecursively($item->getPathname());
} elseif ($item->isFile()) {
unlink($item->getPathname());
}
}
rmdir($directory);
};
$directoryIterator = new \DirectoryIterator($directory);
$itemIterator = new \IteratorIterator($directoryIterator);
foreach ($itemIterator as $item) {
if ($item->isDir() && preg_match('|^' . self::$tempPrefix . '|', $item->getBaseName())) {
fwrite(STDERR, PHP_EOL . "Removing '{$item->getPathname()}'...");
$removeDirectoryRecursively($item->getPathname());
fwrite(STDERR, " done");
}
}
fwrite(STDERR, PHP_EOL);
}
public static function tearDownAfterClass()
{
self::removeDirectory(self::$tempDir);
parent::tearDownAfterClass();
}
protected static function invokePrivateMethod($object, $method, array $arguments = [])
{
$objectReflection = new \ReflectionClass($object);
$methodReflection = $objectReflection->getMethod($method);
$methodReflection->setAccessible(true);
return $methodReflection->invokeArgs($object, $arguments);
}
}
AbstractTestCase::setTempDir(__DIR__ . "/../temp");
AbstractTestCase::setTempPrefix("test_");