Passed
Pull Request — master (#381)
by Alejandro
06:51 queued 02:37
created

warningMessageIsPrintedIfAnExceptionIsThrownAndErrorsAreIgnored()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\CLI\Util\ExitCodes;
11
use Shlinkio\Shlink\Common\Exception\RuntimeException;
12
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdaterInterface;
13
use Symfony\Component\Console\Application;
14
use Symfony\Component\Console\Tester\CommandTester;
15
16
class UpdateDbCommandTest extends TestCase
17
{
18
    /** @var CommandTester */
19
    private $commandTester;
20
    /** @var ObjectProphecy */
21
    private $dbUpdater;
22
23
    public function setUp(): void
24
    {
25
        $this->dbUpdater = $this->prophesize(DbUpdaterInterface::class);
26
27
        $command = new UpdateDbCommand($this->dbUpdater->reveal());
28
        $app = new Application();
29
        $app->add($command);
30
31
        $this->commandTester = new CommandTester($command);
32
    }
33
34
    /** @test */
35
    public function successMessageIsPrintedIfEverythingWorks(): void
36
    {
37
        $download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->will(function () {
38
        });
39
40
        $this->commandTester->execute([]);
41
        $output = $this->commandTester->getDisplay();
42
        $exitCode = $this->commandTester->getStatusCode();
43
44
        $this->assertStringContainsString('GeoLite2 database properly updated', $output);
45
        $this->assertEquals(ExitCodes::EXIT_SUCCESS, $exitCode);
46
        $download->shouldHaveBeenCalledOnce();
47
    }
48
49
    /** @test */
50
    public function errorMessageIsPrintedIfAnExceptionIsThrown(): void
51
    {
52
        $download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->willThrow(RuntimeException::class);
53
54
        $this->commandTester->execute([]);
55
        $output = $this->commandTester->getDisplay();
56
        $exitCode = $this->commandTester->getStatusCode();
57
58
        $this->assertStringContainsString('An error occurred while updating GeoLite2 database', $output);
59
        $this->assertEquals(ExitCodes::EXIT_FAILURE, $exitCode);
60
        $download->shouldHaveBeenCalledOnce();
61
    }
62
63
    /** @test */
64
    public function warningMessageIsPrintedIfAnExceptionIsThrownAndErrorsAreIgnored(): void
65
    {
66
        $download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->willThrow(RuntimeException::class);
67
68
        $this->commandTester->execute(['--ignoreErrors' => true]);
69
        $output = $this->commandTester->getDisplay();
70
        $exitCode = $this->commandTester->getStatusCode();
71
72
        $this->assertStringContainsString('ignored', $output);
73
        $this->assertEquals(ExitCodes::EXIT_SUCCESS, $exitCode);
74
        $download->shouldHaveBeenCalledOnce();
75
    }
76
}
77