WyriHaximus /
php-test-utilities
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace WyriHaximus\TestUtilities; |
||
| 6 | |||
| 7 | use FilesystemIterator; |
||
| 8 | use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
||
| 9 | use PHPUnit\Framework\TestCase as PHPUnitTestCase; |
||
| 10 | use RecursiveDirectoryIterator; |
||
| 11 | use RecursiveIteratorIterator; |
||
| 12 | use SplFileInfo; |
||
| 13 | |||
| 14 | use function file_exists; |
||
| 15 | use function is_dir; |
||
| 16 | use function is_file; |
||
| 17 | use function mkdir; |
||
| 18 | use function rmdir; |
||
| 19 | use function strtoupper; |
||
| 20 | use function substr; |
||
| 21 | use function sys_get_temp_dir; |
||
| 22 | use function time; |
||
| 23 | use function uniqid; |
||
| 24 | use function unlink; |
||
| 25 | use function usleep; |
||
| 26 | |||
| 27 | use const DIRECTORY_SEPARATOR; |
||
| 28 | use const PHP_OS; |
||
| 29 | |||
| 30 | abstract class TestCase extends PHPUnitTestCase |
||
| 31 | { |
||
| 32 | use MockeryPHPUnitIntegration; |
||
| 33 | |||
| 34 | public const string WINDOWS_TEMP_DIR_PREFIX = 'C:\\t\\'; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 35 | public const int WIN_START = 0; |
||
| 36 | public const int WIN_END = 2; |
||
| 37 | 74 | public const int USLEEP = 50; |
|
| 38 | public const int DEFAULT_MODE = 0777; |
||
| 39 | 74 | ||
| 40 | 74 | private string $baseTmpDir; |
|
| 41 | 74 | ||
| 42 | 74 | private string $tmpDir; |
|
| 43 | 74 | ||
| 44 | 74 | private string $tmpNamespace; |
|
| 45 | 74 | ||
| 46 | 74 | protected function setUp(): void |
|
| 47 | { |
||
| 48 | $this->baseTmpDir = $this->getSysTempDir() . |
||
| 49 | 74 | DIRECTORY_SEPARATOR . |
|
| 50 | 74 | 'w-h-p-t-u-' . |
|
| 51 | uniqid() . |
||
| 52 | 74 | DIRECTORY_SEPARATOR; |
|
| 53 | $this->tmpDir = $this->baseTmpDir . |
||
| 54 | 74 | uniqid() . |
|
| 55 | 68 | DIRECTORY_SEPARATOR; |
|
| 56 | |||
| 57 | 74 | $this->tmpNamespace = uniqid('WHPTU'); |
|
| 58 | } |
||
| 59 | |||
| 60 | protected function tearDown(): void |
||
| 61 | { |
||
| 62 | if (! file_exists($this->baseTmpDir)) { |
||
| 63 | return; |
||
| 64 | } |
||
| 65 | |||
| 66 | $this->rmdir($this->baseTmpDir); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** @return iterable<array<bool>> */ |
||
| 70 | final public static function provideTrueFalse(): iterable |
||
| 71 | { |
||
| 72 | yield 'true' => [true]; |
||
| 73 | yield 'false' => [false]; |
||
| 74 | } |
||
| 75 | |||
| 76 | final protected function getSysTempDir(): string |
||
| 77 | 74 | { |
|
| 78 | if (strtoupper(substr(PHP_OS, self::WIN_START, self::WIN_END)) === 'WIN') { |
||
| 79 | 74 | return self::WINDOWS_TEMP_DIR_PREFIX; |
|
| 80 | } |
||
| 81 | |||
| 82 | return sys_get_temp_dir(); |
||
| 83 | 74 | } |
|
| 84 | |||
| 85 | final protected function rmdir(string $dir): void |
||
| 86 | { |
||
| 87 | $directory = new FilesystemIterator($dir); |
||
| 88 | |||
| 89 | 69 | foreach ($directory as $node) { |
|
| 90 | if (! $node instanceof SplFileInfo) { |
||
| 91 | 69 | continue; |
|
| 92 | } |
||
| 93 | |||
| 94 | 69 | if (is_dir($node->getPathname())) { |
|
| 95 | 68 | $this->rmdir($node->getPathname()); |
|
| 96 | 68 | continue; |
|
| 97 | 68 | } |
|
| 98 | |||
| 99 | if (! is_file($node->getPathname())) { |
||
| 100 | 67 | continue; |
|
| 101 | 67 | } |
|
| 102 | 67 | ||
| 103 | if (! unlink($node->getPathname())) { |
||
| 104 | throw ErrorExceptionFactory::create('Error deleting file: ' . $node->getPathname()); |
||
| 105 | } |
||
| 106 | 69 | } |
|
| 107 | 69 | ||
| 108 | if (! @rmdir($dir)) { |
||
| 109 | throw ErrorExceptionFactory::create('Error deleting directory: ' . $dir); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | 68 | ||
| 113 | final protected function getTmpDir(): string |
||
| 114 | 68 | { |
|
| 115 | 68 | if (! file_exists($this->tmpDir) && ! @mkdir($this->tmpDir, self::DEFAULT_MODE, true)) { |
|
| 116 | throw ErrorExceptionFactory::create('Error creating directory: ' . $this->tmpDir); |
||
| 117 | } |
||
| 118 | 68 | ||
| 119 | return $this->tmpDir; |
||
| 120 | } |
||
| 121 | |||
| 122 | final protected function getRandomNameSpace(): string |
||
| 123 | { |
||
| 124 | 67 | return $this->tmpNamespace; |
|
| 125 | } |
||
| 126 | 67 | ||
| 127 | /** @return list<string> */ |
||
| 128 | final protected function getFilesInDirectory(string $path): array |
||
| 129 | { |
||
| 130 | $files = []; |
||
| 131 | |||
| 132 | /** @var iterable<SplFileInfo> $directory */ |
||
| 133 | 67 | $directory = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); |
|
| 134 | |||
| 135 | 67 | foreach ($directory as $node) { |
|
| 136 | if (! is_file($node->getPathname())) { |
||
| 137 | 67 | continue; |
|
| 138 | 67 | } |
|
| 139 | |||
| 140 | 67 | $files[] = $node->getPathname(); |
|
| 141 | 67 | } |
|
| 142 | 67 | ||
| 143 | return $files; |
||
| 144 | } |
||
| 145 | 67 | ||
| 146 | final protected static function waitUntilTheNextSecond(): void |
||
| 147 | { |
||
| 148 | 67 | $now = time(); |
|
| 149 | do { |
||
| 150 | // @codeCoverageIgnoreStart |
||
| 151 | 1 | usleep(self::USLEEP); |
|
| 152 | // @codeCoverageIgnoreEnd |
||
| 153 | 1 | } while ($now === time()); |
|
| 154 | } |
||
| 155 | } |
||
| 156 |