1
0
mirror of https://github.com/danog/file.git synced 2024-12-06 05:28:53 +01:00
file/test/Fixture.php

88 lines
2.7 KiB
PHP
Raw Normal View History

<?php
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;
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
{
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
{
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"
);
}
if (!\file_put_contents($fixtureDir . "/file", "small")) {
2015-08-13 01:02:41 +02:00
throw new \RuntimeException(
"Failed creating temporary test fixture file"
);
}
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"
);
}
}
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
{
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
}
}