diff --git a/README.md b/README.md index 7491af5..f382f87 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,13 @@ You can add the requirement with this command: This check often fails if you dont has the public key from the tool author in your GPG keychain. +### fallback-url (optional, default none) + +This option is useful if you want to add an extra layer of stability to your daily build processes. + +In case the required url is not accessible and a `fallback-url` is set, tooly uses the fallback url to download the phar file. +The fallback url can be a link to a specific version, such as x.y.z, or a link to the latest version for this phar. + ### force-replace (optional, default false) Every time you update or install with composer the phar tools are checked. You are asked if you want to overwrite diff --git a/src/Script/Decision/FileAlreadyExistDecision.php b/src/Script/Decision/FileAlreadyExistDecision.php index cb91e2f..7d081b2 100644 --- a/src/Script/Decision/FileAlreadyExistDecision.php +++ b/src/Script/Decision/FileAlreadyExistDecision.php @@ -16,7 +16,13 @@ class FileAlreadyExistDecision extends AbstractDecision */ public function canProceed(Tool $tool) { - if (false === $this->helper->isFileAlreadyExist($tool->getFilename(), $tool->getUrl())) { + $url = $tool->getUrl(); + + if (false === $this->helper->getDownloader()->isAccessible($url)) { + $url = $tool->getFallbackUrl(); + } + + if (false === $this->helper->isFileAlreadyExist($tool->getFilename(), $url)) { return true; } diff --git a/tests/Script/Decision/FileAlreadyExistDecisionTest.php b/tests/Script/Decision/FileAlreadyExistDecisionTest.php index 609496d..2119fa2 100644 --- a/tests/Script/Decision/FileAlreadyExistDecisionTest.php +++ b/tests/Script/Decision/FileAlreadyExistDecisionTest.php @@ -2,17 +2,63 @@ namespace Tooly\Tests\Script\Decision; -use Tooly\Factory\ToolFactory; use Tooly\Model\Tool; use Tooly\Script\Decision\FileAlreadyExistDecision; +use Tooly\Script\Helper\Downloader; /** * @package Tooly\Tests\Script\Decision */ class FileAlreadyExistDecisionTest extends DecisionTestCase { - public function testIfFileNotAlreadyExistReturnsTrue() + public function testIfFileIsAccessibleAndFileNotAlreadyExistReturnsTrue() { + $downloader = $this + ->getMockBuilder(Downloader::class) + ->disableOriginalConstructor() + ->getMock(); + + $downloader + ->expects($this->once()) + ->method('isAccessible') + ->willReturn(true); + + $this->helper + ->expects($this->once()) + ->method('getDownloader') + ->willReturn($downloader); + + $this->helper + ->expects($this->once()) + ->method('isFileAlreadyExist') + ->willReturn(false); + + $tool = $this + ->getMockBuilder(Tool::class) + ->disableOriginalConstructor() + ->getMock(); + + $decision = new FileAlreadyExistDecision($this->configuration, $this->helper); + $this->assertTrue($decision->canProceed($tool)); + } + + public function testIfFileNotAccessibleAndFileNotAlreadyExistReturnsTrue() + { + $downloader = $this + ->getMockBuilder(Downloader::class) + ->disableOriginalConstructor() + ->getMock(); + + $downloader + ->expects($this->once()) + ->method('isAccessible') + ->willReturn(false); + + $this->helper + ->expects($this->once()) + ->method('getDownloader') + ->willReturn($downloader); + $this->helper ->expects($this->once()) ->method('isFileAlreadyExist') @@ -27,8 +73,54 @@ public function testIfFileNotAlreadyExistReturnsTrue() $this->assertTrue($decision->canProceed($tool)); } - public function testIfFileAlreadyExistReturnsFalse() + public function testIfFileIsAccessibleAndFileAlreadyExistReturnsFalse() { + $downloader = $this + ->getMockBuilder(Downloader::class) + ->disableOriginalConstructor() + ->getMock(); + + $downloader + ->expects($this->once()) + ->method('isAccessible') + ->willReturn(true); + + $this->helper + ->expects($this->once()) + ->method('getDownloader') + ->willReturn($downloader); + + $this->helper + ->expects($this->once()) + ->method('isFileAlreadyExist') + ->willReturn(true); + + $tool = $this + ->getMockBuilder(Tool::class) + ->disableOriginalConstructor() + ->getMock(); + + $decision = new FileAlreadyExistDecision($this->configuration, $this->helper); + $this->assertFalse($decision->canProceed($tool)); + } + + public function testIfFileNotAccessibleAndFileAlreadyExistReturnsFalse() + { + $downloader = $this + ->getMockBuilder(Downloader::class) + ->disableOriginalConstructor() + ->getMock(); + + $downloader + ->expects($this->once()) + ->method('isAccessible') + ->willReturn(false); + + $this->helper + ->expects($this->once()) + ->method('getDownloader') + ->willReturn($downloader); + $this->helper ->expects($this->once()) ->method('isFileAlreadyExist') diff --git a/tests/Script/Processor/ProcessTest.php b/tests/Script/Processor/ProcessTest.php index 1d09996..05ee03e 100644 --- a/tests/Script/Processor/ProcessTest.php +++ b/tests/Script/Processor/ProcessTest.php @@ -131,7 +131,7 @@ public function testCanSuccessfullyDownloadAToolViaFallbackUrl() ->getMock(); $downloader - ->expects($this->exactly(3)) + ->expects($this->exactly(4)) ->method('isAccessible') ->will($this->onConsecutiveCalls(false, true, false)); @@ -148,7 +148,7 @@ public function testCanSuccessfullyDownloadAToolViaFallbackUrl() ->willReturn($filesystem); $this->helper - ->expects($this->exactly(4)) + ->expects($this->exactly(5)) ->method('getDownloader') ->willReturn($downloader);