Skip to content

Commit

Permalink
Fix file already exist decision for not accessible url using fall back
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Jungnickel committed May 24, 2019
1 parent 6e258bd commit 5c0cbca
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/Script/Decision/FileAlreadyExistDecision.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
98 changes: 95 additions & 3 deletions tests/Script/Decision/FileAlreadyExistDecisionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions tests/Script/Processor/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -148,7 +148,7 @@ public function testCanSuccessfullyDownloadAToolViaFallbackUrl()
->willReturn($filesystem);

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

Expand Down

0 comments on commit 5c0cbca

Please sign in to comment.