From c09cf70584ad9c8990aa4498f39a677d528981e2 Mon Sep 17 00:00:00 2001 From: Reinier Kip Date: Wed, 22 Feb 2017 18:02:38 +0100 Subject: [PATCH] Test that installer fails when release cannot be downloaded Fixes #108 --- README.md | 2 +- installer.php | 2 +- tests/integration/Installer/InstallerTest.php | 35 +++++++++++++++---- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 91619d4..66c0417 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ The recommended way to install the QA Tools is by using our installer: ``` php -r "copy('https://raw.githubusercontent.com/ibuildingsnl/qa-tools/master/installer.php', 'qa-tools-setup.php');" -php -r "if (hash_file('SHA384', 'qa-tools-setup.php') === 'cbe46561b2dabe44c600f0ebdf262d73857c7e912bc4c891ee0a571f1df809b0c7881ee34ad13e598e606e35ea36c7f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('qa-tools-setup.php'); } echo PHP_EOL;" +php -r "if (hash_file('SHA384', 'qa-tools-setup.php') === '2f83e895f1fda9e44334f452e9abedfde9152ba1aa95d1ea411eef0d1c96086e8d8c68909ffeaa3ec7d72de420c85287') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('qa-tools-setup.php'); } echo PHP_EOL;" php qa-tools-setup.php php -r "unlink('qa-tools-setup.php');" ``` diff --git a/installer.php b/installer.php index dba95d9..7731638 100755 --- a/installer.php +++ b/installer.php @@ -537,7 +537,7 @@ private function install($version) } } - return true; + return $success; } /** diff --git a/tests/integration/Installer/InstallerTest.php b/tests/integration/Installer/InstallerTest.php index c6a1e9b..6cb7018 100644 --- a/tests/integration/Installer/InstallerTest.php +++ b/tests/integration/Installer/InstallerTest.php @@ -8,6 +8,7 @@ use Mockery; use PharValidator; use PHPUnit\Framework\TestCase; +use RuntimeException; use Symfony\Component\Filesystem\Filesystem; final class InstallerTest extends TestCase @@ -73,9 +74,7 @@ public function tearDown() $this->filesystem->remove($this->tempDirectory); } - /** - * @test - */ + /** @test */ public function download_correct_files() { /** @var Mockery\Mock|HttpClient $httpClient */ @@ -118,18 +117,42 @@ public function download_correct_files() $installer = new Installer(false, $httpClient, $pharValidator, self::REPOSITORY_OWNER, self::REPOSITORY_NAME); ob_start(); - $installer->run(false, $this->tempDirectory, 'qa-tools'); + $success = $installer->run(false, $this->tempDirectory, 'qa-tools'); $output = ob_get_clean(); + $this->assertTrue($success); $this->assertRegExp( '~^QA Tools \(version '.preg_quote(self::VERSION, '~').'\) successfully installed~sm', $output ); - $this->assertFileExists($this->tempDirectory.'/qa-tools'); $this->assertEquals(self::PHAR_ASSET_CONTENTS, file_get_contents($this->tempDirectory.'/qa-tools')); - $this->assertFileExists($this->tempDirectory.'/qa-tools.pubkey'); $this->assertEquals(self::PUBKEY_ASSET_CONTENTS, file_get_contents($this->tempDirectory.'/qa-tools.pubkey')); } + + /** @test */ + public function fails_when_files_cannot_be_downloaded() + { + /** @var Mockery\Mock|HttpClient $httpClient */ + $httpClient = Mockery::mock(HttpClient::class); + $httpClient->shouldReceive('get')->andThrow(new RuntimeException()); + + /** @var Mockery\Mock|PharValidator $pharValidator */ + $pharValidator = Mockery::mock(PharValidator::class); + $pharValidator->shouldReceive('assertPharValid'); + + $installer = new Installer(false, $httpClient, $pharValidator, self::REPOSITORY_OWNER, self::REPOSITORY_NAME); + + ob_start(); + $success = $installer->run(false, $this->tempDirectory, 'qa-tools'); + $output = ob_get_clean(); + + $this->assertFalse($success); + $this->assertContains('Unable to download version information from https://', $output); + $this->assertContains('The download failed repeatedly, aborting.', $output); + + $this->assertFileNotExists($this->tempDirectory.'/qa-tools'); + $this->assertFileNotExists($this->tempDirectory.'/qa-tools.pubkey'); + } }