From ada9c20db14efe91dbec6a1cf8f472dc5194ad25 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 8 Mar 2012 00:11:52 +0100 Subject: [PATCH] Update Git/Hg downloaders tests, making sure they never use a live filesystem instance --- src/Composer/Downloader/VcsDownloader.php | 4 +- .../Test/Downloader/GitDownloaderTest.php | 70 +++++++++++++++---- .../Test/Downloader/HgDownloaderTest.php | 21 ++++-- .../Test/Installer/LibraryInstallerTest.php | 9 +-- 4 files changed, 75 insertions(+), 29 deletions(-) diff --git a/src/Composer/Downloader/VcsDownloader.php b/src/Composer/Downloader/VcsDownloader.php index 7843dcfde..8c264f7bb 100644 --- a/src/Composer/Downloader/VcsDownloader.php +++ b/src/Composer/Downloader/VcsDownloader.php @@ -47,7 +47,7 @@ abstract class VcsDownloader implements DownloaderInterface public function download(PackageInterface $package, $path) { if (!$package->getSourceReference()) { - throw new \InvalidArgumentException('The given package is missing reference information'); + throw new \InvalidArgumentException('Package '.$package->getPrettyName().' is missing reference information'); } $this->io->write(" - Package " . $package->getName() . " (" . $package->getPrettyVersion() . ")"); @@ -62,7 +62,7 @@ abstract class VcsDownloader implements DownloaderInterface public function update(PackageInterface $initial, PackageInterface $target, $path) { if (!$target->getSourceReference()) { - throw new \InvalidArgumentException('The given package is missing reference information'); + throw new \InvalidArgumentException('Package '.$target->getPrettyName().' is missing reference information'); } $this->io->write(" - Package " . $target->getName() . " (" . $target->getPrettyVersion() . ")"); diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php index 885a80d35..389f4ddb7 100644 --- a/tests/Composer/Test/Downloader/GitDownloaderTest.php +++ b/tests/Composer/Test/Downloader/GitDownloaderTest.php @@ -16,6 +16,15 @@ use Composer\Downloader\GitDownloader; class GitDownloaderTest extends \PHPUnit_Framework_TestCase { + protected function getDownloaderMock($io = null, $executor = null, $filesystem = null) + { + $io = $io ?: $this->getMock('Composer\IO\IOInterface'); + $executor = $executor ?: $this->getMock('Composer\Util\ProcessExecutor'); + $filesystem = $filesystem ?: $this->getMock('Composer\Util\Filesystem'); + + return new GitDownloader($io, $executor, $filesystem); + } + /** * @expectedException \InvalidArgumentException */ @@ -26,27 +35,60 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase ->method('getSourceReference') ->will($this->returnValue(null)); - $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface')); + $downloader = $this->getDownloaderMock(); $downloader->download($packageMock, '/path'); } public function testDownload() { - $expectedGitCommand = $this->getCmd('git clone \'https://github.com/l3l0/composer\' \'composerPath\' && cd \'composerPath\' && git checkout \'ref\' && git reset --hard \'ref\''); + $expectedGitCommand = $this->getCmd('git clone \'https://example.com/composer/composer\' \'composerPath\' && cd \'composerPath\' && git checkout \'ref\' && git reset --hard \'ref\''); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $packageMock->expects($this->any()) ->method('getSourceReference') ->will($this->returnValue('ref')); - $packageMock->expects($this->once()) + $packageMock->expects($this->any()) ->method('getSourceUrl') - ->will($this->returnValue('https://github.com/l3l0/composer')); + ->will($this->returnValue('https://example.com/composer/composer')); $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); $processExecutor->expects($this->once()) ->method('execute') ->with($this->equalTo($expectedGitCommand)) ->will($this->returnValue(0)); - $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor); + $downloader = $this->getDownloaderMock(null, $processExecutor); + $downloader->download($packageMock, 'composerPath'); + } + + public function testDownloadUsesVariousProtocolsForGithub() + { + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->any()) + ->method('getSourceReference') + ->will($this->returnValue('ref')); + $packageMock->expects($this->once()) + ->method('getSourceUrl') + ->will($this->returnValue('https://github.com/composer/composer')); + $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); + + $expectedGitCommand = $this->getCmd('git clone \'git://github.com/composer/composer\' \'composerPath\' && cd \'composerPath\' && git checkout \'ref\' && git reset --hard \'ref\''); + $processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedGitCommand)) + ->will($this->returnValue(1)); + + $expectedGitCommand = $this->getCmd('git clone \'https://github.com/composer/composer\' \'composerPath\' && cd \'composerPath\' && git checkout \'ref\' && git reset --hard \'ref\''); + $processExecutor->expects($this->at(1)) + ->method('execute') + ->with($this->equalTo($expectedGitCommand)) + ->will($this->returnValue(1)); + + $expectedGitCommand = $this->getCmd('git clone \'http://github.com/composer/composer\' \'composerPath\' && cd \'composerPath\' && git checkout \'ref\' && git reset --hard \'ref\''); + $processExecutor->expects($this->at(2)) + ->method('execute') + ->with($this->equalTo($expectedGitCommand)) + ->will($this->returnValue(0)); + + $downloader = $this->getDownloaderMock(null, $processExecutor); $downloader->download($packageMock, 'composerPath'); } @@ -55,21 +97,21 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase */ public function testDownloadThrowsRuntimeExceptionIfGitCommandFails() { - $expectedGitCommand = $this->getCmd('git clone \'https://github.com/l3l0/composer\' \'composerPath\' && cd \'composerPath\' && git checkout \'ref\' && git reset --hard \'ref\''); + $expectedGitCommand = $this->getCmd('git clone \'https://example.com/composer/composer\' \'composerPath\' && cd \'composerPath\' && git checkout \'ref\' && git reset --hard \'ref\''); $packageMock = $this->getMock('Composer\Package\PackageInterface'); $packageMock->expects($this->any()) ->method('getSourceReference') ->will($this->returnValue('ref')); - $packageMock->expects($this->once()) + $packageMock->expects($this->any()) ->method('getSourceUrl') - ->will($this->returnValue('https://github.com/l3l0/composer')); + ->will($this->returnValue('https://example.com/composer/composer')); $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); $processExecutor->expects($this->once()) ->method('execute') ->with($this->equalTo($expectedGitCommand)) ->will($this->returnValue(1)); - $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor); + $downloader = $this->getDownloaderMock(null, $processExecutor); $downloader->download($packageMock, 'composerPath'); } @@ -84,7 +126,7 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase ->method('getSourceReference') ->will($this->returnValue(null)); - $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface')); + $downloader = $this->getDownloaderMock(); $downloader->update($initialPackageMock, $sourcePackageMock, '/path'); } @@ -110,7 +152,7 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase ->with($this->equalTo($expectedGitUpdateCommand)) ->will($this->returnValue(0)); - $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor); + $downloader = $this->getDownloaderMock(null, $processExecutor); $downloader->update($packageMock, $packageMock, 'composerPath'); } @@ -139,7 +181,7 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase ->with($this->equalTo($expectedGitUpdateCommand)) ->will($this->returnValue(1)); - $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor); + $downloader = $this->getDownloaderMock(null, $processExecutor); $downloader->update($packageMock, $packageMock, 'composerPath'); } @@ -158,13 +200,13 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase ->method('removeDirectory') ->with($this->equalTo('composerPath')); - $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor, $filesystem); + $downloader = $this->getDownloaderMock(null, $processExecutor, $filesystem); $downloader->remove($packageMock, 'composerPath'); } public function testGetInstallationSource() { - $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface')); + $downloader = $this->getDownloaderMock(); $this->assertEquals('source', $downloader->getInstallationSource()); } diff --git a/tests/Composer/Test/Downloader/HgDownloaderTest.php b/tests/Composer/Test/Downloader/HgDownloaderTest.php index a7c246394..39e1efb17 100644 --- a/tests/Composer/Test/Downloader/HgDownloaderTest.php +++ b/tests/Composer/Test/Downloader/HgDownloaderTest.php @@ -16,6 +16,15 @@ use Composer\Downloader\HgDownloader; class HgDownloaderTest extends \PHPUnit_Framework_TestCase { + protected function getDownloaderMock($io = null, $executor = null, $filesystem = null) + { + $io = $io ?: $this->getMock('Composer\IO\IOInterface'); + $executor = $executor ?: $this->getMock('Composer\Util\ProcessExecutor'); + $filesystem = $filesystem ?: $this->getMock('Composer\Util\Filesystem'); + + return new HgDownloader($io, $executor, $filesystem); + } + /** * @expectedException \InvalidArgumentException */ @@ -26,7 +35,7 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase ->method('getSourceReference') ->will($this->returnValue(null)); - $downloader = new HgDownloader($this->getMock('Composer\IO\IOInterface')); + $downloader = $this->getDownloaderMock(); $downloader->download($packageMock, '/path'); } @@ -45,7 +54,7 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedGitCommand)); - $downloader = new HgDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor); + $downloader = $this->getDownloaderMock(null, $processExecutor); $downloader->download($packageMock, 'composerPath'); } @@ -60,7 +69,7 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase ->method('getSourceReference') ->will($this->returnValue(null)); - $downloader = new HgDownloader($this->getMock('Composer\IO\IOInterface')); + $downloader = $this->getDownloaderMock(); $downloader->update($initialPackageMock, $sourcePackageMock, '/path'); } @@ -84,7 +93,7 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase ->method('execute') ->with($this->equalTo($expectedUpdateCommand)); - $downloader = new HgDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor); + $downloader = $this->getDownloaderMock(null, $processExecutor); $downloader->update($packageMock, $packageMock, 'composerPath'); } @@ -102,13 +111,13 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase ->method('removeDirectory') ->with($this->equalTo('composerPath')); - $downloader = new HgDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor, $filesystem); + $downloader = $this->getDownloaderMock(null, $processExecutor, $filesystem); $downloader->remove($packageMock, 'composerPath'); } public function testGetInstallationSource() { - $downloader = new HgDownloader($this->getMock('Composer\IO\IOInterface')); + $downloader = $this->getDownloaderMock(null); $this->assertEquals('source', $downloader->getInstallationSource()); } diff --git a/tests/Composer/Test/Installer/LibraryInstallerTest.php b/tests/Composer/Test/Installer/LibraryInstallerTest.php index b89bf9b9a..100ebb60e 100644 --- a/tests/Composer/Test/Installer/LibraryInstallerTest.php +++ b/tests/Composer/Test/Installer/LibraryInstallerTest.php @@ -49,13 +49,8 @@ class LibraryInstallerTest extends TestCase protected function tearDown() { - if (is_dir($this->vendorDir)) { - $this->fs->removeDirectory($this->vendorDir); - } - - if (is_dir($this->binDir)) { - $this->fs->removeDirectory($this->binDir); - } + $this->fs->removeDirectory($this->vendorDir); + $this->fs->removeDirectory($this->binDir); } public function testInstallerCreationShouldNotCreateVendorDirectory()