Skip to content

Commit

Permalink
Added TestUtil class and fixed path issue on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Aug 21, 2015
1 parent 5cfba1e commit c5be1d8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
56 changes: 56 additions & 0 deletions src/Test/TestUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of the webmozart/glob package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* 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 <bschussek@gmail.com>
*/
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()
{
}
}
4 changes: 2 additions & 2 deletions tests/GlobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/Iterator/GlobIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/Iterator/RecursiveDirectoryIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use RecursiveIteratorIterator;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\Glob\Iterator\RecursiveDirectoryIterator;
use Webmozart\Glob\Test\TestUtil;

/**
* @since 1.0
Expand All @@ -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);
Expand Down

0 comments on commit c5be1d8

Please sign in to comment.