diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index ce33801ac..502a483ab 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -42,6 +42,11 @@ class AutoloadGeneratorTest extends TestCase */ private $workingDir; + /** + * @var string + */ + private $origDir; + /** * @var InstallationManager|MockObject */ diff --git a/tests/Composer/Test/CacheTest.php b/tests/Composer/Test/CacheTest.php index b01dbfd89..fe3e76dad 100644 --- a/tests/Composer/Test/CacheTest.php +++ b/tests/Composer/Test/CacheTest.php @@ -12,15 +12,20 @@ namespace Composer\Test; -use Composer\Cache; use Composer\TestCase; +use Composer\Util\Filesystem; class CacheTest extends TestCase { private $files, $root, $finder, $cache; + /** + * @var Filesystem + */ + private $fs; public function setUp() { + $this->fs = new Filesystem; if (getenv('TRAVIS')) { $this->markTestSkipped('Test causes intermittent failures on Travis'); } @@ -48,6 +53,13 @@ class CacheTest extends TestCase ->will($this->returnValue($this->finder)); } + protected function tearDown() + { + if (is_dir($this->root)) { + $this->fs->removeDirectory($this->root); + } + } + public function testRemoveOutdatedFiles() { $outdated = array_slice($this->files, 1); diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php index 7521ac51a..75b5ab46c 100644 --- a/tests/Composer/Test/Downloader/GitDownloaderTest.php +++ b/tests/Composer/Test/Downloader/GitDownloaderTest.php @@ -18,6 +18,25 @@ use Composer\Util\Filesystem; class GitDownloaderTest extends \PHPUnit_Framework_TestCase { + /** @var Filesystem */ + private $fs; + /** @var string */ + private $workingDir; + + protected function setUp() + { + $this->fs = new Filesystem; + $this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true)); + $this->fs->ensureDirectoryExists($this->workingDir); + } + + protected function tearDown() + { + if (is_dir($this->workingDir)) { + $this->fs->removeDirectory($this->workingDir); + } + } + protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null) { $io = $io ?: $this->getMock('Composer\IO\IOInterface'); @@ -234,9 +253,6 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase { $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer"); - $tmpDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true)); - $fs = new Filesystem; - $fs->ensureDirectoryExists($tmpDir.'/.git'); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $packageMock->expects($this->any()) ->method('getSourceReference') @@ -266,23 +282,22 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue(0)); $processExecutor->expects($this->at(4)) ->method('execute') - ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($tmpDir))) + ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir))) ->will($this->returnValue(0)); + $this->fs->ensureDirectoryExists($this->workingDir.'/.git'); $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor); - $downloader->update($packageMock, $packageMock, $tmpDir); + $downloader->update($packageMock, $packageMock, $this->workingDir); } /** + * @group failing * @expectedException \RuntimeException */ public function testUpdateThrowsRuntimeExceptionIfGitCommandFails() { $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer"); - $tmpDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true)); - $fs = new Filesystem; - $fs->ensureDirectoryExists($tmpDir.'/.git'); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $packageMock->expects($this->any()) ->method('getSourceReference') @@ -303,9 +318,10 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedGitUpdateCommand)) ->will($this->returnValue(1)); - + + $this->fs->ensureDirectoryExists($this->workingDir.'/.git'); $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor); - $downloader->update($packageMock, $packageMock, $tmpDir); + $downloader->update($packageMock, $packageMock, $this->workingDir); } public function testRemove() diff --git a/tests/Composer/Test/Downloader/HgDownloaderTest.php b/tests/Composer/Test/Downloader/HgDownloaderTest.php index ab9ec28cd..504f9d2c4 100644 --- a/tests/Composer/Test/Downloader/HgDownloaderTest.php +++ b/tests/Composer/Test/Downloader/HgDownloaderTest.php @@ -17,6 +17,25 @@ use Composer\Util\Filesystem; class HgDownloaderTest extends \PHPUnit_Framework_TestCase { + /** @var Filesystem */ + private $fs; + /** @var string */ + private $workingDir; + + protected function setUp() + { + $this->fs = new Filesystem; + $this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true)); + $this->fs->ensureDirectoryExists($this->workingDir); + } + + protected function tearDown() + { + if (is_dir($this->workingDir)) { + $this->fs->removeDirectory($this->workingDir); + } + } + protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null) { $io = $io ?: $this->getMock('Composer\IO\IOInterface'); @@ -85,9 +104,8 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase public function testUpdate() { - $tmpDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true)); $fs = new Filesystem; - $fs->ensureDirectoryExists($tmpDir.'/.hg'); + $fs->ensureDirectoryExists($this->workingDir.'/.hg'); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $packageMock->expects($this->any()) ->method('getSourceReference') @@ -109,7 +127,7 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue(0)); $downloader = $this->getDownloaderMock(null, null, $processExecutor); - $downloader->update($packageMock, $packageMock, $tmpDir); + $downloader->update($packageMock, $packageMock, $this->workingDir); } public function testRemove() diff --git a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php index 2c113b5ae..2e01ddee2 100644 --- a/tests/Composer/Test/Downloader/PerforceDownloaderTest.php +++ b/tests/Composer/Test/Downloader/PerforceDownloaderTest.php @@ -16,6 +16,7 @@ use Composer\Downloader\PerforceDownloader; use Composer\Config; use Composer\Repository\VcsRepository; use Composer\IO\IOInterface; +use Composer\Util\Filesystem; /** * @author Matt Whittom @@ -30,10 +31,13 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase protected $repoConfig; protected $repository; protected $testPath; + protected $fs; protected function setUp() { + $this->fs = new Filesystem; $this->testPath = sys_get_temp_dir() . '/composer-test'; + $this->fs->ensureDirectoryExists($this->testPath); $this->repoConfig = $this->getRepoConfig(); $this->config = $this->getConfig(); $this->io = $this->getMockIoInterface(); @@ -51,7 +55,9 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase $this->io = null; $this->config = null; $this->repoConfig = null; - $this->testPath = null; + if (is_dir($this->testPath)) { + $this->fs->removeDirectory($this->testPath); + } } protected function getMockProcessExecutor() diff --git a/tests/Composer/Test/Downloader/XzDownloaderTest.php b/tests/Composer/Test/Downloader/XzDownloaderTest.php index 02e6701a0..900975694 100644 --- a/tests/Composer/Test/Downloader/XzDownloaderTest.php +++ b/tests/Composer/Test/Downloader/XzDownloaderTest.php @@ -13,14 +13,32 @@ namespace Composer\Test\Downloader; use Composer\Downloader\XzDownloader; +use Composer\Util\Filesystem; class XzDownloaderTest extends \PHPUnit_Framework_TestCase { + /** + * @var Filesystem + */ + private $fs; + + /** + * @var string + */ + private $testName; + public function setUp() { if (defined('PHP_WINDOWS_VERSION_BUILD')) { $this->markTestSkipped('Skip test on Windows'); } + $this->fs = new Filesystem; + $this->testName = sys_get_temp_dir().'/composer-xz-test-vendor'; + } + + public function tearDown() + { + $this->fs->removeDirectory($this->testName); } public function testErrorMessages() @@ -44,7 +62,7 @@ class XzDownloaderTest extends \PHPUnit_Framework_TestCase $config->expects($this->any()) ->method('get') ->with('vendor-dir') - ->will($this->returnValue(sys_get_temp_dir().'/composer-xz-test-vendor')); + ->will($this->returnValue($this->testName)); $downloader = new XzDownloader($io, $config); try { diff --git a/tests/Composer/Test/Downloader/ZipDownloaderTest.php b/tests/Composer/Test/Downloader/ZipDownloaderTest.php index 58e0078b0..b50072084 100644 --- a/tests/Composer/Test/Downloader/ZipDownloaderTest.php +++ b/tests/Composer/Test/Downloader/ZipDownloaderTest.php @@ -13,14 +13,32 @@ namespace Composer\Test\Downloader; use Composer\Downloader\ZipDownloader; +use Composer\Util\Filesystem; class ZipDownloaderTest extends \PHPUnit_Framework_TestCase { + /** + * @var Filesystem + */ + private $fs; + + /** + * @var string + */ + private $testName; + public function setUp() { if (!class_exists('ZipArchive')) { $this->markTestSkipped('zip extension missing'); } + $this->fs = new Filesystem; + $this->testName = sys_get_temp_dir().'/composer-zip-test-vendor'; + } + + public function tearDown() + { + $this->fs->removeDirectory($this->testName); } public function testErrorMessages() @@ -44,7 +62,7 @@ class ZipDownloaderTest extends \PHPUnit_Framework_TestCase $config->expects($this->any()) ->method('get') ->with('vendor-dir') - ->will($this->returnValue(sys_get_temp_dir().'/composer-zip-test-vendor')); + ->will($this->returnValue($this->testName)); $downloader = new ZipDownloader($io, $config); try { diff --git a/tests/Composer/Test/Util/FilesystemTest.php b/tests/Composer/Test/Util/FilesystemTest.php index 4607512b6..d7c986369 100644 --- a/tests/Composer/Test/Util/FilesystemTest.php +++ b/tests/Composer/Test/Util/FilesystemTest.php @@ -17,6 +17,21 @@ use Composer\TestCase; class FilesystemTest extends TestCase { + /** + * @var Filesystem + */ + private $fs; + + /** + * @var string + */ + private $workingDir; + + /** + * @var string + */ + private $testFile; + /** * @dataProvider providePathCouplesAsCode */ @@ -26,6 +41,23 @@ class FilesystemTest extends TestCase $this->assertEquals($expected, $fs->findShortestPathCode($a, $b, $directory)); } + public function setUp() + { + $this->fs = new Filesystem; + $this->workingDir = sys_get_temp_dir() . '/composer_testdir'; + $this->testFile = sys_get_temp_dir() . '/composer_test_file'; + } + + public function tearDown() + { + if (is_dir($this->workingDir)) { + $this->fs->removeDirectory($this->workingDir); + } + if (is_file($this->testFile)) { + $this->fs->remove($this->testFile); + } + } + public function providePathCouplesAsCode() { return array( @@ -121,33 +153,30 @@ class FilesystemTest extends TestCase */ public function testRemoveDirectoryPhp() { - $tmp = sys_get_temp_dir(); - @mkdir($tmp . "/composer_testdir/level1/level2", 0777, true); - file_put_contents($tmp . "/composer_testdir/level1/level2/hello.txt", "hello world"); + @mkdir($this->workingDir . "/level1/level2", 0777, true); + file_put_contents($this->workingDir . "/level1/level2/hello.txt", "hello world"); $fs = new Filesystem; - $this->assertTrue($fs->removeDirectoryPhp($tmp . "/composer_testdir")); - $this->assertFalse(file_exists($tmp . "/composer_testdir/level1/level2/hello.txt")); + $this->assertTrue($fs->removeDirectoryPhp($this->workingDir)); + $this->assertFalse(file_exists($this->workingDir . "/level1/level2/hello.txt")); } public function testFileSize() { - $tmp = sys_get_temp_dir(); - file_put_contents("$tmp/composer_test_file", 'Hello'); + file_put_contents($this->testFile, 'Hello'); $fs = new Filesystem; - $this->assertGreaterThanOrEqual(5, $fs->size("$tmp/composer_test_file")); + $this->assertGreaterThanOrEqual(5, $fs->size($this->testFile)); } public function testDirectorySize() { - $tmp = sys_get_temp_dir(); - @mkdir("$tmp/composer_testdir", 0777, true); - file_put_contents("$tmp/composer_testdir/file1.txt", 'Hello'); - file_put_contents("$tmp/composer_testdir/file2.txt", 'World'); + @mkdir($this->workingDir, 0777, true); + file_put_contents($this->workingDir."/file1.txt", 'Hello'); + file_put_contents($this->workingDir."/file2.txt", 'World'); $fs = new Filesystem; - $this->assertGreaterThanOrEqual(10, $fs->size("$tmp/composer_testdir")); + $this->assertGreaterThanOrEqual(10, $fs->size($this->workingDir)); } /** @@ -184,8 +213,7 @@ class FilesystemTest extends TestCase */ public function testUnlinkSymlinkedDirectory() { - $tmp = sys_get_temp_dir(); - $basepath = $tmp . "/composer_testdir"; + $basepath = $this->workingDir; $symlinked = $basepath . "/linked"; @mkdir($basepath . "/real", 0777, true); touch($basepath . "/real/FILE"); @@ -212,14 +240,12 @@ class FilesystemTest extends TestCase */ public function testRemoveSymlinkedDirectoryWithTrailingSlash() { - $tmp = sys_get_temp_dir(); - $basepath = $tmp . "/composer_testdir"; - @mkdir($basepath . "/real", 0777, true); - touch($basepath . "/real/FILE"); - $symlinked = $basepath . "/linked"; + @mkdir($this->workingDir . "/real", 0777, true); + touch($this->workingDir . "/real/FILE"); + $symlinked = $this->workingDir . "/linked"; $symlinkedTrailingSlash = $symlinked . "/"; - $result = @symlink($basepath . "/real", $symlinked); + $result = @symlink($this->workingDir . "/real", $symlinked); if (!$result) { $this->markTestSkipped('Symbolic links for directories not supported on this platform');