Skip to content

Commit

Permalink
Fix download from a fallback url
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Jungnickel committed May 8, 2019
1 parent c7aadd3 commit 6e258bd
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 148 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
},
"phpmd": {
"url": "http://static.phpmd.org/php/latest/phpmd.phar",
"fallback-url": "https://tooly.me-dresden.de/phpmd/2.6.0/phpmd.phar",
"fallback-url": "https://github.com/jakzal/phpmd/releases/download/2.6.0-jakzal-2/phpmd.phar",
"force-replace": true
},
"security-checker": {
Expand Down
14 changes: 13 additions & 1 deletion src/Script/Decision/IsAccessibleDecision.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class IsAccessibleDecision extends AbstractDecision
public function canProceed(Tool $tool)
{
if (false === $this->helper->getDownloader()->isAccessible($tool->getUrl())) {
return false;
return $this->fallbackUrlIsAccessible($tool);
}

if (empty($tool->getSignUrl())) {
Expand All @@ -38,4 +38,16 @@ public function getReason()
{
return '<error>At least one given URL are not accessible!</error>';
}

/**
* @param Tool $tool
*
* @return bool
*/
private function fallbackUrlIsAccessible(Tool $tool)
{
$fallbackUrl = $tool->getFallbackUrl();

return false === empty($fallbackUrl) && true === $this->helper->getDownloader()->isAccessible($fallbackUrl);
}
}
41 changes: 0 additions & 41 deletions src/Script/Decision/UseFallbackURLDecision.php

This file was deleted.

2 changes: 0 additions & 2 deletions src/Script/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Tooly\Script\Decision\IsAccessibleDecision;
use Tooly\Script\Decision\IsVerifiedDecision;
use Tooly\Script\Decision\OnlyDevDecision;
use Tooly\Script\Decision\UseFallbackURLDecision;
use Tooly\Model\Tool;

/**
Expand Down Expand Up @@ -124,7 +123,6 @@ private function getDecisions()
return [
new OnlyDevDecision($this->configuration, $this->helper),
new IsAccessibleDecision($this->configuration, $this->helper),
new UseFallbackURLDecision($this->configuration, $this->helper),
new FileAlreadyExistDecision($this->configuration, $this->helper),
new IsVerifiedDecision($this->configuration, $this->helper),
new DoReplaceDecision($this->configuration, $this->helper, $this->io),
Expand Down
22 changes: 22 additions & 0 deletions tests/Script/Decision/IsAccessibleDecisionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ public function testNotAccessibleToolSignUrlReturnsFalse()
])));
}

public function testNotAccessibleToolUrlButAccessibleFallbackUrlReturnsTrue()
{
$downloader = $this
->getMockBuilder(Downloader::class)
->getMock();

$downloader
->expects($this->exactly(2))
->method('isAccessible')
->will($this->onConsecutiveCalls(false, true));

$this->helper
->expects($this->exactly(2))
->method('getDownloader')
->willReturn($downloader);

$decision = new IsAccessibleDecision($this->configuration, $this->helper);
$this->assertTrue($decision->canProceed(ToolFactory::createTool('tool', __DIR__, [
'fallback-url' => 'fallback-url'
])));
}

public function testAccessibleUrlsWillReturnTrue()
{
$downloader = $this
Expand Down
103 changes: 0 additions & 103 deletions tests/Script/Decision/UseFallbackUrlDecisionTest.php

This file was deleted.

52 changes: 52 additions & 0 deletions tests/Script/Processor/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,56 @@ public function testCanSuccessfullyDownloadATool()
$processor = new Processor($this->io, $this->helper, $this->configuration);
$processor->process($tool);
}

public function testCanSuccessfullyDownloadAToolViaFallbackUrl()
{
vfsStream::setup('bin');

$downloader = $this
->getMockBuilder(Downloader::class)
->getMock();

$downloader
->expects($this->exactly(3))
->method('isAccessible')
->will($this->onConsecutiveCalls(false, true, false));

$filesystem = $this
->getMockBuilder(Filesystem::class)
->getMock();

$filesystem
->method('isFileAlreadyExist')
->willReturn(false);

$this->helper
->method('getFilesystem')
->willReturn($filesystem);

$this->helper
->expects($this->exactly(4))
->method('getDownloader')
->willReturn($downloader);

$this->helper
->method('isFileAlreadyExist')
->willReturn(false);

$this->io
->expects($this->exactly(2))
->method('write');

$tool = $this
->getMockBuilder(Tool::class)
->disableOriginalConstructor()
->getMock();

$tool
->expects($this->exactly(2))
->method('getFallbackUrl')
->willReturn('//test.html');

$processor = new Processor($this->io, $this->helper, $this->configuration);
$processor->process($tool);
}
}

0 comments on commit 6e258bd

Please sign in to comment.