Completed
Pull Request — master (#209)
by Alejandro
04:24
created

GetVisitsCommandTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A noDateFlagsTriesToListWithoutDateRange() 0 11 1
A providingDateFlagsTheListGetsFiltered() 0 16 1
A outputIsProperlyGenerated() 0 18 1
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
5
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Argument;
8
use Prophecy\Prophecy\ObjectProphecy;
9
use Shlinkio\Shlink\CLI\Command\ShortUrl\GetVisitsCommand;
10
use Shlinkio\Shlink\Common\Util\DateRange;
11
use Shlinkio\Shlink\Core\Entity\Visit;
12
use Shlinkio\Shlink\Core\Entity\VisitLocation;
13
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
14
use Symfony\Component\Console\Application;
15
use Symfony\Component\Console\Tester\CommandTester;
16
use Zend\I18n\Translator\Translator;
17
18
class GetVisitsCommandTest extends TestCase
19
{
20
    /**
21
     * @var CommandTester
22
     */
23
    protected $commandTester;
24
    /**
25
     * @var ObjectProphecy
26
     */
27
    protected $visitsTracker;
28
29
    public function setUp()
30
    {
31
        $this->visitsTracker = $this->prophesize(VisitsTrackerInterface::class);
32
        $command = new GetVisitsCommand($this->visitsTracker->reveal(), Translator::factory([]));
33
        $app = new Application();
34
        $app->add($command);
35
        $this->commandTester = new CommandTester($command);
36
    }
37
38
    /**
39
     * @test
40
     */
41
    public function noDateFlagsTriesToListWithoutDateRange()
42
    {
43
        $shortCode = 'abc123';
44
        $this->visitsTracker->info($shortCode, new DateRange(null, null))->willReturn([])
45
                                                                         ->shouldBeCalledTimes(1);
46
47
        $this->commandTester->execute([
48
            'command' => 'shortcode:visits',
49
            'shortCode' => $shortCode,
50
        ]);
51
    }
52
53
    /**
54
     * @test
55
     */
56
    public function providingDateFlagsTheListGetsFiltered()
57
    {
58
        $shortCode = 'abc123';
59
        $startDate = '2016-01-01';
60
        $endDate = '2016-02-01';
61
        $this->visitsTracker->info($shortCode, new DateRange(new \DateTime($startDate), new \DateTime($endDate)))
62
            ->willReturn([])
63
            ->shouldBeCalledTimes(1);
64
65
        $this->commandTester->execute([
66
            'command' => 'shortcode:visits',
67
            'shortCode' => $shortCode,
68
            '--startDate' => $startDate,
69
            '--endDate' => $endDate,
70
        ]);
71
    }
72
73
    /**
74
     * @test
75
     */
76
    public function outputIsProperlyGenerated()
77
    {
78
        $shortCode = 'abc123';
79
        $this->visitsTracker->info($shortCode, Argument::any())->willReturn([
80
            (new Visit())->setReferer('foo')
81
                         ->setVisitLocation((new VisitLocation())->setCountryName('Spain'))
82
                         ->setUserAgent('bar'),
83
        ])->shouldBeCalledTimes(1);
84
85
        $this->commandTester->execute([
86
            'command' => 'shortcode:visits',
87
            'shortCode' => $shortCode,
88
        ]);
89
        $output = $this->commandTester->getDisplay();
90
        $this->assertGreaterThan(0, \strpos($output, 'foo'));
91
        $this->assertGreaterThan(0, \strpos($output, 'Spain'));
92
        $this->assertGreaterThan(0, \strpos($output, 'bar'));
93
    }
94
}
95