1
0
mirror of https://github.com/danog/file.git synced 2024-11-30 04:19:39 +01:00
file/test/Fixture.php

52 lines
1.4 KiB
PHP
Raw Permalink Normal View History

<?php
2015-08-13 01:02:41 +02:00
namespace Amp\File\Test;
final class Fixture
{
2015-08-13 01:02:41 +02:00
private static $fixtureId;
public static function path()
{
2015-08-13 01:02:41 +02:00
if (empty(self::$fixtureId)) {
self::$fixtureId = \uniqid();
}
return \sys_get_temp_dir() . "/amphp_file_fixture/" . \strtr(__CLASS__, "\\", ".") . self::$fixtureId;
2015-08-13 01:02:41 +02:00
}
public static function init()
{
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 . "/small.txt", "small")) {
throw new \RuntimeException(
"Failed creating temporary test fixture file"
);
}
}
public static function clear()
{
2015-08-13 01:02:41 +02:00
$fixtureDir = self::path();
if (!\file_exists($fixtureDir)) {
return;
}
if (\stripos(\PHP_OS, "win") === 0) {
\system('rd /Q /S "' . $fixtureDir . '"');
} else {
\system('/bin/rm -rf ' . \escapeshellarg($fixtureDir));
}
}
}