|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\Visit; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Prophecy\Argument; |
|
8
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
9
|
|
|
use Shlinkio\Shlink\CLI\Command\Visit\UpdateDbCommand; |
|
10
|
|
|
use Shlinkio\Shlink\Common\Exception\RuntimeException; |
|
11
|
|
|
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdaterInterface; |
|
12
|
|
|
use Symfony\Component\Console\Application; |
|
13
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
14
|
|
|
use Zend\I18n\Translator\Translator; |
|
15
|
|
|
|
|
16
|
|
|
class UpdateDbCommandTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var CommandTester |
|
20
|
|
|
*/ |
|
21
|
|
|
private $commandTester; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var ObjectProphecy |
|
24
|
|
|
*/ |
|
25
|
|
|
private $dbUpdater; |
|
26
|
|
|
|
|
27
|
|
|
public function setUp() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->dbUpdater = $this->prophesize(DbUpdaterInterface::class); |
|
30
|
|
|
|
|
31
|
|
|
$command = new UpdateDbCommand($this->dbUpdater->reveal(), Translator::factory([])); |
|
32
|
|
|
$app = new Application(); |
|
33
|
|
|
$app->add($command); |
|
34
|
|
|
|
|
35
|
|
|
$this->commandTester = new CommandTester($command); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @test |
|
40
|
|
|
*/ |
|
41
|
|
|
public function successMessageIsPrintedIfEverythingWorks() |
|
42
|
|
|
{ |
|
43
|
|
|
$download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->will(function () { |
|
44
|
|
|
}); |
|
45
|
|
|
|
|
46
|
|
|
$this->commandTester->execute([]); |
|
47
|
|
|
$output = $this->commandTester->getDisplay(); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertContains('GeoLite2 database properly updated', $output); |
|
50
|
|
|
$download->shouldHaveBeenCalledOnce(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @test |
|
55
|
|
|
*/ |
|
56
|
|
|
public function errorMessageIsPrintedIfAnExceptionIsThrown() |
|
57
|
|
|
{ |
|
58
|
|
|
$download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->willThrow(RuntimeException::class); |
|
59
|
|
|
|
|
60
|
|
|
$this->commandTester->execute([]); |
|
61
|
|
|
$output = $this->commandTester->getDisplay(); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertContains('An error occurred while updating GeoLite2 database', $output); |
|
64
|
|
|
$download->shouldHaveBeenCalledOnce(); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|