From f026e2a0eb58195e34355707a49beaea83cf9e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Tue, 28 Feb 2012 11:59:18 +0100 Subject: [PATCH] Added tests for FileDownloader and ArchiveDownloader --- src/Composer/Downloader/FileDownloader.php | 5 +- .../Test/Downloader/ArchiveDownloaderTest.php | 35 +++++ .../Test/Downloader/FileDownloaderTest.php | 145 ++++++++++++++++++ .../{Downloader => }/Util/FilesystemTest.php | 5 +- 4 files changed, 185 insertions(+), 5 deletions(-) create mode 100644 tests/Composer/Test/Downloader/ArchiveDownloaderTest.php create mode 100644 tests/Composer/Test/Downloader/FileDownloaderTest.php rename tests/Composer/Test/{Downloader => }/Util/FilesystemTest.php (91%) diff --git a/src/Composer/Downloader/FileDownloader.php b/src/Composer/Downloader/FileDownloader.php index 0e0f2cb46..698ebfd46 100644 --- a/src/Composer/Downloader/FileDownloader.php +++ b/src/Composer/Downloader/FileDownloader.php @@ -52,7 +52,9 @@ class FileDownloader implements DownloaderInterface public function download(PackageInterface $package, $path) { $url = $package->getDistUrl(); - $checksum = $package->getDistSha1Checksum(); + if (!$url) { + throw new \InvalidArgumentException('The given package is missing url information'); + } if (!is_dir($path)) { if (file_exists($path)) { @@ -78,6 +80,7 @@ class FileDownloader implements DownloaderInterface .' directory is writable and you have internet connectivity'); } + $checksum = $package->getDistSha1Checksum(); if ($checksum && hash_file('sha1', $fileName) !== $checksum) { throw new \UnexpectedValueException('The checksum verification of the file failed (downloaded from '.$url.')'); } diff --git a/tests/Composer/Test/Downloader/ArchiveDownloaderTest.php b/tests/Composer/Test/Downloader/ArchiveDownloaderTest.php new file mode 100644 index 000000000..fbbf2b269 --- /dev/null +++ b/tests/Composer/Test/Downloader/ArchiveDownloaderTest.php @@ -0,0 +1,35 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Downloader; + +use Composer\Util\Filesystem; + +class ArchiveDownloaderTest extends \PHPUnit_Framework_TestCase +{ + public function testGetFileName() + { + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->any()) + ->method('getDistUrl') + ->will($this->returnValue('http://example.com/script.js')) + ; + + $downloader = $this->getMockForAbstractClass('Composer\Downloader\ArchiveDownloader', array($this->getMock('Composer\IO\IOInterface'))); + $method = new \ReflectionMethod($downloader, 'getFileName'); + $method->setAccessible(true); + + $first = $method->invoke($downloader, $packageMock, '/path'); + $this->assertRegExp('#/path/[a-z0-9]+\.js#', $first); + $this->assertSame($first, $method->invoke($downloader, $packageMock, '/path')); + } +} diff --git a/tests/Composer/Test/Downloader/FileDownloaderTest.php b/tests/Composer/Test/Downloader/FileDownloaderTest.php new file mode 100644 index 000000000..cc0fb2f6a --- /dev/null +++ b/tests/Composer/Test/Downloader/FileDownloaderTest.php @@ -0,0 +1,145 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Downloader; + +use Composer\Downloader\FileDownloader; +use Composer\Util\Filesystem; + +class FileDownloaderTest extends \PHPUnit_Framework_TestCase +{ + /** + * @expectedException \InvalidArgumentException + */ + public function testDownloadForPackageWithoutDistReference() + { + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->once()) + ->method('getDistUrl') + ->will($this->returnValue(null)) + ; + + $downloader = new FileDownloader($this->getMock('Composer\IO\IOInterface')); + $downloader->download($packageMock, '/path'); + } + + public function testDownloadToExistFile() + { + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->once()) + ->method('getDistUrl') + ->will($this->returnValue('url')) + ; + + $path = tempnam(sys_get_temp_dir(), 'c'); + + $downloader = new FileDownloader($this->getMock('Composer\IO\IOInterface')); + try { + $downloader->download($packageMock, $path); + $this->fail(); + } catch (\Exception $e) { + if (file_exists($path)) { + unset($path); + } + $this->assertInstanceOf('UnexpectedValueException', $e); + $this->assertContains('exists and is not a directory', $e->getMessage()); + } + } + + public function testGetFileName() + { + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->once()) + ->method('getDistUrl') + ->will($this->returnValue('http://example.com/script.js')) + ; + + $downloader = new FileDownloader($this->getMock('Composer\IO\IOInterface')); + $method = new \ReflectionMethod($downloader, 'getFileName'); + $method->setAccessible(true); + + $this->assertEquals('/path/script.js', $method->invoke($downloader, $packageMock, '/path')); + } + + public function testDownloadButFileIsUnsaved() + { + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->any()) + ->method('getDistUrl') + ->will($this->returnValue('http://example.com/script.js')) + ; + + do { + $path = sys_get_temp_dir().'/'.md5(time().rand()); + } while (file_exists($path)); + + $ioMock = $this->getMock('Composer\IO\IOInterface'); + $ioMock->expects($this->any()) + ->method('write') + ->will($this->returnCallback(function($messages, $newline = true) use ($path) { + if (is_file($path.'/script.js')) { + unlink($path.'/script.js'); + } + return $messages; + })) + ; + + $downloader = new FileDownloader($ioMock); + try { + $downloader->download($packageMock, $path); + $this->fail(); + } catch (\Exception $e) { + if (is_dir($path)) { + $fs = new Filesystem(); + $fs->removeDirectory($path); + } else if (is_file($path)) { + unset($path); + } + + $this->assertInstanceOf('UnexpectedValueException', $e); + $this->assertContains('could not be saved to', $e->getMessage()); + } + } + + public function testDownloadFileWithInvalidChecksum() + { + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->any()) + ->method('getDistUrl') + ->will($this->returnValue('http://example.com/script.js')) + ; + $packageMock->expects($this->any()) + ->method('getDistSha1Checksum') + ->will($this->returnValue('invalid')) + ; + + do { + $path = sys_get_temp_dir().'/'.md5(time().rand()); + } while (file_exists($path)); + + $downloader = new FileDownloader($this->getMock('Composer\IO\IOInterface')); + try { + $downloader->download($packageMock, $path); + $this->fail(); + } catch (\Exception $e) { + if (is_dir($path)) { + $fs = new Filesystem(); + $fs->removeDirectory($path); + } else if (is_file($path)) { + unset($path); + } + + $this->assertInstanceOf('UnexpectedValueException', $e); + $this->assertContains('checksum verification', $e->getMessage()); + } + } +} diff --git a/tests/Composer/Test/Downloader/Util/FilesystemTest.php b/tests/Composer/Test/Util/FilesystemTest.php similarity index 91% rename from tests/Composer/Test/Downloader/Util/FilesystemTest.php rename to tests/Composer/Test/Util/FilesystemTest.php index 9605a111c..0db5549d2 100644 --- a/tests/Composer/Test/Downloader/Util/FilesystemTest.php +++ b/tests/Composer/Test/Util/FilesystemTest.php @@ -10,7 +10,7 @@ * file that was distributed with this source code. */ -namespace Composer\Test\Repository; +namespace Composer\Test\Util; use Composer\Util\Filesystem; use Composer\Test\TestCase; @@ -32,7 +32,6 @@ class FilesystemTest extends TestCase array('/foo/bar', '/foo/bar', false, "__FILE__"), array('/foo/bar', '/foo/baz', false, "__DIR__.'/baz'"), array('/foo/bin/run', '/foo/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"), - array('/foo/bin/run', '/foo/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"), array('/foo/bin/run', '/bar/bin/run', false, "'/bar/bin/run'"), array('c:/bin/run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"), array('c:\\bin\\run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"), @@ -41,7 +40,6 @@ class FilesystemTest extends TestCase array('/foo/bar', '/foo/bar', true, "__DIR__"), array('/foo/bar', '/foo/baz', true, "dirname(__DIR__).'/baz'"), array('/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"), - array('/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"), array('/foo/bin/run', '/bar/bin/run', true, "'/bar/bin/run'"), array('c:/bin/run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"), array('c:\\bin\\run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"), @@ -70,7 +68,6 @@ class FilesystemTest extends TestCase array('/foo/bar', '/foo/bar', "./bar"), array('/foo/bar', '/foo/baz', "./baz"), array('/foo/bin/run', '/foo/vendor/acme/bin/run', "../vendor/acme/bin/run"), - array('/foo/bin/run', '/foo/vendor/acme/bin/run', "../vendor/acme/bin/run"), array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run"), array('c:/bin/run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"), array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),