2022-12-17 22:52:21 +01:00
|
|
|
<?php declare(strict_types=1);
|
2015-08-13 01:02:41 +02:00
|
|
|
|
|
|
|
namespace Amp\File\Test;
|
|
|
|
|
2022-02-08 21:42:35 +01:00
|
|
|
use const Amp\Process\IS_WINDOWS;
|
|
|
|
|
2018-10-27 17:57:31 +02:00
|
|
|
final class Fixture
|
|
|
|
{
|
2020-10-08 16:23:44 +02:00
|
|
|
private static string $fixtureId;
|
2015-08-13 01:02:41 +02:00
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
public static function path(): string
|
2018-10-27 17:57:31 +02:00
|
|
|
{
|
2015-08-13 01:02:41 +02:00
|
|
|
if (empty(self::$fixtureId)) {
|
2020-06-30 21:45:09 +02:00
|
|
|
self::$fixtureId = \uniqid('amphp-test-', true);
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 21:45:09 +02:00
|
|
|
return \sys_get_temp_dir() . "/amphp_file_fixture/" . \str_replace("\\", ".", __CLASS__) . self::$fixtureId;
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
public static function init(): void
|
2018-10-27 17:57:31 +02:00
|
|
|
{
|
2015-08-13 01:02:41 +02:00
|
|
|
$fixtureDir = self::path();
|
|
|
|
self::clear();
|
|
|
|
if (!\mkdir($fixtureDir, $mode = 0777, $recursive = true)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
"Failed creating temporary test fixture directory: {$fixtureDir}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!\mkdir($fixtureDir . "/dir", $mode = 0777, $recursive = true)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
"Failed creating temporary test fixture directory"
|
|
|
|
);
|
|
|
|
}
|
2020-06-23 22:40:48 +02:00
|
|
|
if (!\file_put_contents($fixtureDir . "/file", "small")) {
|
2015-08-13 01:02:41 +02:00
|
|
|
throw new \RuntimeException(
|
|
|
|
"Failed creating temporary test fixture file"
|
|
|
|
);
|
|
|
|
}
|
2020-06-23 22:40:48 +02:00
|
|
|
if (!\symlink($fixtureDir . "/dir", $fixtureDir . "/dirlink")) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
"Failed creating temporary test fixture symlink to directory"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!\symlink($fixtureDir . "/file", $fixtureDir . "/filelink")) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
"Failed creating temporary test fixture symlink to file"
|
|
|
|
);
|
|
|
|
}
|
2022-02-08 21:42:35 +01:00
|
|
|
if (!IS_WINDOWS) {
|
|
|
|
if (!\symlink($fixtureDir . "/linkloop", $fixtureDir . "/linkloop")) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
"Failed creating temporary test fixture symlink loop"
|
|
|
|
);
|
|
|
|
}
|
2020-06-23 22:40:48 +02:00
|
|
|
}
|
|
|
|
if (\extension_loaded('posix')) {
|
|
|
|
if (!\posix_mkfifo($fixtureDir . "/fifo", 0777)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
"Failed creating temporary test fixture fifo"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!\symlink($fixtureDir . "/fifo", $fixtureDir . "/fifolink")) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
"Failed creating temporary test fixture symlink to file"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
public static function clear(): void
|
2018-10-27 17:57:31 +02:00
|
|
|
{
|
2020-10-21 23:17:22 +02:00
|
|
|
\clearstatcache(true);
|
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
$fixtureDir = self::path();
|
|
|
|
if (!\file_exists($fixtureDir)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-21 23:17:22 +02:00
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
if (\stripos(\PHP_OS, "win") === 0) {
|
|
|
|
\system('rd /Q /S "' . $fixtureDir . '"');
|
|
|
|
} else {
|
|
|
|
\system('/bin/rm -rf ' . \escapeshellarg($fixtureDir));
|
|
|
|
}
|
2020-10-21 23:17:22 +02:00
|
|
|
|
|
|
|
\clearstatcache(true);
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
}
|