Skip to content

Commit

Permalink
Merge pull request #17 from svenjungnickel/fallback-url
Browse files Browse the repository at this point in the history
Add possibility to set a fallback url
  • Loading branch information
tommy-muehle authored May 15, 2019
2 parents efd6809 + 6e258bd commit dbc5778
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 6 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
},
"phpmd": {
"url": "http://static.phpmd.org/php/latest/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
5 changes: 5 additions & 0 deletions src/Factory/ToolFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static function createTool($name, $directory, array $parameters)
'only-dev' => true,
'force-replace' => false,
'rename' => false,
'fallback-url' => null,
];

$parameters = array_merge($defaults, $parameters);
Expand All @@ -47,6 +48,10 @@ public static function createTool($name, $directory, array $parameters)
$tool->setNameToToolKey();
}

if (null !== $parameters['fallback-url']) {
$tool->setFallbackUrl($parameters['fallback-url']);
}

return $tool;
}

Expand Down
24 changes: 23 additions & 1 deletion src/Model/Tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ class Tool
*/
private $rename = false;

/**
* @var string
*/
private $fallbackUrl;

/**
* @param string $name
* @param string $filename
* @param string $url
* @param string $signUrl
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct($name, $filename, $url, $signUrl = null)
Expand Down Expand Up @@ -137,4 +141,22 @@ public function renameToConfigKey()
{
return $this->rename;
}

/**
* @param string $url
*
* @return void
*/
public function setFallbackUrl($url)
{
$this->fallbackUrl = $url;
}

/**
* @return string
*/
public function getFallbackUrl()
{
return $this->fallbackUrl;
}
}
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);
}
}
17 changes: 15 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\Helper;
use Tooly\Model\Tool;

/**
Expand Down Expand Up @@ -80,7 +79,7 @@ public function process(Tool $tool)
return;
}

$data = $this->helper->getDownloader()->download($tool->getUrl());
$data = $this->helper->getDownloader()->download($this->getDownloadUrl($tool));
$filename = $tool->getFilename();

$this->helper->getFilesystem()->createFile($filename, $data);
Expand Down Expand Up @@ -166,4 +165,18 @@ private function removeFromDir($dir, array $excludeToolNames = [])
$this->helper->getFilesystem()->remove($path);
}
}

/**
* @param Tool $tool
*
* @return string
*/
private function getDownloadUrl(Tool $tool)
{
if (false === $this->helper->getDownloader()->isAccessible($tool->getUrl())) {
return $tool->getFallbackUrl();
}

return $tool->getUrl();
}
}
9 changes: 7 additions & 2 deletions tests/Factory/ToolFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ public function testCanCreateATool()
$tool = ToolFactory::createTool('test', 'vfs://root', [
'url' => 'my-url',
'sign-url' => 'my-sign-url',
'force-replace' => true
'force-replace' => true,
'only-dev' => false,
'rename' => true,
'fallback-url' => 'fallback-url'
]);

$this->assertInstanceOf(Tool::class, $tool);
$this->assertEquals('my-url', $tool->getUrl());
$this->assertEquals('my-sign-url', $tool->getSignUrl());
$this->assertTrue($tool->forceReplace());
$this->assertTrue($tool->isOnlyDev());
$this->assertFalse($tool->isOnlyDev());
$this->assertTrue($tool->renameToConfigKey());
$this->assertEquals('fallback-url', $tool->getFallbackUrl());
}

public function testCanCreateMultipleTools()
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
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 dbc5778

Please sign in to comment.