diff --git a/src/Test/TestUtil.php b/src/Test/TestUtil.php new file mode 100644 index 0000000..841d27a --- /dev/null +++ b/src/Test/TestUtil.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Webmozart\Glob\Test; + +/** + * Contains utility methods for testing. + * + * @since 3.1 + * + * @author Bernhard Schussek + */ +final class TestUtil +{ + /** + * Creates a temporary directory. + * + * @param string $namespace The directory path in the system's temporary + * directory. + * @param string $className The name of the test class. + * + * @return string The path to the created directory. + */ + public static function makeTempDir($namespace, $className) + { + if (false !== ($pos = strrpos($className, '\\'))) { + $shortClass = substr($className, $pos + 1); + } else { + $shortClass = $className; + } + + // Usage of realpath() is important if the temporary directory is a + // symlink to another directory (e.g. /var => /private/var on some Macs) + // We want to know the real path to avoid comparison failures with + // code that uses real paths only + $basePath = realpath(sys_get_temp_dir()).'/'.$namespace.'/'.$shortClass; + + while (false === @mkdir($tempDir = $basePath.rand(10000, 99999), 0777, true)) { + // Run until we are able to create a directory + } + + return $tempDir; + } + + private function __construct() + { + } +} diff --git a/tests/GlobTest.php b/tests/GlobTest.php index 173ece2..349f1c8 100644 --- a/tests/GlobTest.php +++ b/tests/GlobTest.php @@ -14,6 +14,7 @@ use PHPUnit_Framework_TestCase; use Symfony\Component\Filesystem\Filesystem; use Webmozart\Glob\Glob; +use Webmozart\Glob\Test\TestUtil; /** * @since 1.0 @@ -26,8 +27,7 @@ class GlobTest extends PHPUnit_Framework_TestCase protected function setUp() { - while (false === @mkdir($this->tempDir = sys_get_temp_dir().'/webmozart-glob/GlobTest'.rand(10000, 99999), 0777, true)) { - } + $this->tempDir = TestUtil::makeTempDir('webmozart-glob', __CLASS__); $filesystem = new Filesystem(); $filesystem->mirror(__DIR__.'/Fixtures', $this->tempDir); diff --git a/tests/Iterator/GlobIteratorTest.php b/tests/Iterator/GlobIteratorTest.php index 303c3e1..a7580a2 100644 --- a/tests/Iterator/GlobIteratorTest.php +++ b/tests/Iterator/GlobIteratorTest.php @@ -15,6 +15,7 @@ use Symfony\Component\Filesystem\Filesystem; use Webmozart\Glob\Glob; use Webmozart\Glob\Iterator\GlobIterator; +use Webmozart\Glob\Test\TestUtil; /** * @since 1.0 @@ -29,8 +30,7 @@ class GlobIteratorTest extends PHPUnit_Framework_TestCase protected function setUp() { - while (false === @mkdir($this->tempDir = sys_get_temp_dir().'/webmozart-glob/GlobIteratorTest'.rand(10000, 99999), 0777, true)) { - } + $this->tempDir = TestUtil::makeTempDir('webmozart-glob', __CLASS__); $filesystem = new Filesystem(); $filesystem->mirror(__DIR__.'/../Fixtures', $this->tempDir); diff --git a/tests/Iterator/RecursiveDirectoryIteratorTest.php b/tests/Iterator/RecursiveDirectoryIteratorTest.php index 3c2470b..022c976 100644 --- a/tests/Iterator/RecursiveDirectoryIteratorTest.php +++ b/tests/Iterator/RecursiveDirectoryIteratorTest.php @@ -15,6 +15,7 @@ use RecursiveIteratorIterator; use Symfony\Component\Filesystem\Filesystem; use Webmozart\Glob\Iterator\RecursiveDirectoryIterator; +use Webmozart\Glob\Test\TestUtil; /** * @since 1.0 @@ -27,8 +28,7 @@ class RecursiveDirectoryIteratorTest extends PHPUnit_Framework_TestCase protected function setUp() { - while (false === mkdir($this->tempDir = sys_get_temp_dir().'/webmozart-glob/RecursiveDirectoryIteratorTest'.rand(10000, 99999), 0777, true)) { - } + $this->tempDir = TestUtil::makeTempDir('webmozart-glob', __CLASS__); $filesystem = new Filesystem(); $filesystem->mirror(__DIR__.'/../Fixtures', $this->tempDir);