shlink/module/CLI/test/Command/Visit/UpdateDbCommandTest.php

78 lines
2.7 KiB
PHP
Raw Normal View History

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;
use Prophecy\Argument;
2018-11-12 22:37:30 +03:00
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\Visit\UpdateDbCommand;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\IpGeolocation\Exception\RuntimeException;
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
{
/** @var CommandTester */
2018-11-12 22:37:30 +03:00
private $commandTester;
/** @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 */
public function successMessageIsPrintedIfEverythingWorks(): void
2018-11-12 22:37: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();
$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);
$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 */
public function errorMessageIsPrintedIfAnExceptionIsThrown(): void
2018-11-12 22:37: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();
$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);
$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();
}
}