From 230e1450536759c47aff54f95487a0573a3e6d10 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 18 Feb 2012 16:59:39 +0100 Subject: [PATCH] Fix downloader tests on windows and mock Filesystem properly --- src/Composer/Downloader/VcsDownloader.php | 7 +++--- .../Test/Downloader/GitDownloaderTest.php | 25 ++++++++++++++----- .../Test/Downloader/HgDownloaderTest.php | 25 ++++++++++++++----- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/Composer/Downloader/VcsDownloader.php b/src/Composer/Downloader/VcsDownloader.php index 5fa8c9e4b..5a30dacc5 100644 --- a/src/Composer/Downloader/VcsDownloader.php +++ b/src/Composer/Downloader/VcsDownloader.php @@ -24,11 +24,13 @@ abstract class VcsDownloader implements DownloaderInterface { protected $io; protected $process; + protected $filesystem; - public function __construct(IOInterface $io, ProcessExecutor $process = null) + public function __construct(IOInterface $io, ProcessExecutor $process = null, Filesystem $fs = null) { $this->io = $io; $this->process = $process ?: new ProcessExecutor; + $this->filesystem = $fs ?: new Filesystem; } /** @@ -74,8 +76,7 @@ abstract class VcsDownloader implements DownloaderInterface public function remove(PackageInterface $package, $path) { $this->enforceCleanDirectory($path); - $fs = new Filesystem(); - $fs->removeDirectory($path); + $this->filesystem->removeDirectory($path); } /** diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php index 7d563d2e4..ff1c3ac07 100644 --- a/tests/Composer/Test/Downloader/GitDownloaderTest.php +++ b/tests/Composer/Test/Downloader/GitDownloaderTest.php @@ -32,7 +32,7 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase public function testDownload() { - $expectedGitCommand = 'git clone \'https://github.com/l3l0/composer\' \'composerPath\' && cd \'composerPath\' && git checkout \'ref\' && git reset --hard \'ref\''; + $expectedGitCommand = $this->getCmd('git clone \'https://github.com/l3l0/composer\' \'composerPath\' && cd \'composerPath\' && git checkout \'ref\' && git reset --hard \'ref\''); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $packageMock->expects($this->any()) ->method('getSourceReference') @@ -66,8 +66,8 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase public function testUpdate() { - $expectedGitUpdateCommand = 'cd \'composerPath\' && git fetch && git checkout \'ref\' && git reset --hard \'ref\''; - $expectedGitResetCommand = 'cd \'composerPath\' && git status --porcelain'; + $expectedGitUpdateCommand = $this->getCmd('cd \'composerPath\' && git fetch && git checkout \'ref\' && git reset --hard \'ref\''); + $expectedGitResetCommand = $this->getCmd('cd \'composerPath\' && git status --porcelain'); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $packageMock->expects($this->any()) @@ -90,22 +90,35 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase public function testRemove() { - $expectedGitResetCommand = 'cd \'composerPath\' && git status --porcelain'; + $expectedGitResetCommand = $this->getCmd('cd \'composerPath\' && git status --porcelain'); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); $processExecutor->expects($this->any()) ->method('execute') ->with($this->equalTo($expectedGitResetCommand)); + $filesystem = $this->getMock('Composer\Util\Filesystem'); + $filesystem->expects($this->any()) + ->method('removeDirectory') + ->with($this->equalTo('composerPath')); - $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor); + $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor, $filesystem); $downloader->remove($packageMock, 'composerPath'); } public function testGetInstallationSource() { $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface')); - + $this->assertEquals('source', $downloader->getInstallationSource()); } + + private function getCmd($cmd) + { + if (defined('PHP_WINDOWS_VERSION_BUILD')) { + return strtr($cmd, "'", '"'); + } + + return $cmd; + } } diff --git a/tests/Composer/Test/Downloader/HgDownloaderTest.php b/tests/Composer/Test/Downloader/HgDownloaderTest.php index 81429194d..a7c246394 100644 --- a/tests/Composer/Test/Downloader/HgDownloaderTest.php +++ b/tests/Composer/Test/Downloader/HgDownloaderTest.php @@ -32,7 +32,7 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase public function testDownload() { - $expectedGitCommand = 'hg clone \'https://mercurial.dev/l3l0/composer\' \'composerPath\' && cd \'composerPath\' && hg up \'ref\''; + $expectedGitCommand = $this->getCmd('hg clone \'https://mercurial.dev/l3l0/composer\' \'composerPath\' && cd \'composerPath\' && hg up \'ref\''); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $packageMock->expects($this->any()) ->method('getSourceReference') @@ -66,8 +66,8 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase public function testUpdate() { - $expectedUpdateCommand = 'cd \'composerPath\' && hg pull && hg up \'ref\''; - $expectedResetCommand = 'cd \'composerPath\' && hg st'; + $expectedUpdateCommand = $this->getCmd('cd \'composerPath\' && hg pull && hg up \'ref\''); + $expectedResetCommand = $this->getCmd('cd \'composerPath\' && hg st'); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $packageMock->expects($this->any()) @@ -90,22 +90,35 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase public function testRemove() { - $expectedResetCommand = 'cd \'composerPath\' && hg st'; + $expectedResetCommand = $this->getCmd('cd \'composerPath\' && hg st'); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); $processExecutor->expects($this->any()) ->method('execute') ->with($this->equalTo($expectedResetCommand)); + $filesystem = $this->getMock('Composer\Util\Filesystem'); + $filesystem->expects($this->any()) + ->method('removeDirectory') + ->with($this->equalTo('composerPath')); - $downloader = new HgDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor); + $downloader = new HgDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor, $filesystem); $downloader->remove($packageMock, 'composerPath'); } public function testGetInstallationSource() { $downloader = new HgDownloader($this->getMock('Composer\IO\IOInterface')); - + $this->assertEquals('source', $downloader->getInstallationSource()); } + + private function getCmd($cmd) + { + if (defined('PHP_WINDOWS_VERSION_BUILD')) { + return strtr($cmd, "'", '"'); + } + + return $cmd; + } }