2018-11-12 22:37:30 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2018-11-12 22:37:30 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\Visit;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-11-12 23:30:30 +03:00
|
|
|
use Prophecy\Argument;
|
2018-11-12 22:37:30 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Shlinkio\Shlink\CLI\Command\Visit\UpdateDbCommand;
|
2019-03-16 13:08:12 +03:00
|
|
|
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
2019-08-11 00:30:47 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Exception\RuntimeException;
|
2019-08-10 14:42:37 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdaterInterface;
|
2018-11-12 22:37:30 +03:00
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
|
|
|
|
class UpdateDbCommandTest extends TestCase
|
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var CommandTester */
|
2018-11-12 22:37:30 +03:00
|
|
|
private $commandTester;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-12 22:37:30 +03:00
|
|
|
private $dbUpdater;
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2018-11-12 22:37:30 +03:00
|
|
|
{
|
|
|
|
$this->dbUpdater = $this->prophesize(DbUpdaterInterface::class);
|
|
|
|
|
2018-11-18 18:02:52 +03:00
|
|
|
$command = new UpdateDbCommand($this->dbUpdater->reveal());
|
2018-11-12 22:37:30 +03:00
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-03-16 13:08:12 +03:00
|
|
|
public function successMessageIsPrintedIfEverythingWorks(): void
|
2018-11-12 22:37:30 +03:00
|
|
|
{
|
2018-11-12 23:30:30 +03:00
|
|
|
$download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->will(function () {
|
2018-11-12 22:37:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->commandTester->execute([]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
2019-03-16 13:08:12 +03:00
|
|
|
$exitCode = $this->commandTester->getStatusCode();
|
2018-11-12 22:37:30 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
$this->assertStringContainsString('GeoLite2 database properly updated', $output);
|
2019-03-16 13:08:12 +03:00
|
|
|
$this->assertEquals(ExitCodes::EXIT_SUCCESS, $exitCode);
|
2018-11-12 22:37:30 +03:00
|
|
|
$download->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-03-16 13:08:12 +03:00
|
|
|
public function errorMessageIsPrintedIfAnExceptionIsThrown(): void
|
2018-11-12 22:37:30 +03:00
|
|
|
{
|
2018-11-12 23:30:30 +03:00
|
|
|
$download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->willThrow(RuntimeException::class);
|
2018-11-12 22:37:30 +03:00
|
|
|
|
|
|
|
$this->commandTester->execute([]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
2019-03-16 13:08:12 +03:00
|
|
|
$exitCode = $this->commandTester->getStatusCode();
|
2018-11-12 22:37:30 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
$this->assertStringContainsString('An error occurred while updating GeoLite2 database', $output);
|
2019-03-16 13:08:12 +03:00
|
|
|
$this->assertEquals(ExitCodes::EXIT_FAILURE, $exitCode);
|
|
|
|
$download->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function warningMessageIsPrintedIfAnExceptionIsThrownAndErrorsAreIgnored(): void
|
|
|
|
{
|
|
|
|
$download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->willThrow(RuntimeException::class);
|
|
|
|
|
|
|
|
$this->commandTester->execute(['--ignoreErrors' => true]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
$exitCode = $this->commandTester->getStatusCode();
|
|
|
|
|
|
|
|
$this->assertStringContainsString('ignored', $output);
|
|
|
|
$this->assertEquals(ExitCodes::EXIT_SUCCESS, $exitCode);
|
2018-11-12 22:37:30 +03:00
|
|
|
$download->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
}
|