Completed
Push — master ( 67e465...aa77c9 )
by Alejandro
13s queued 10s
created

ListShortUrlsCommandTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A loadingMorePagesCallsListMoreTimes() 0 19 2
A passingPageWillMakeListStartOnThatPage() 0 10 1
A havingMorePagesButAnsweringNoCallsListJustOnce() 0 22 2
A ifTagsFlagIsProvidedTagsColumnIsIncluded() 0 12 1
A setUp() 0 7 1
A noInputCallsListJustOnce() 0 7 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\ListShortUrlsCommand;
10
use Shlinkio\Shlink\Core\Entity\ShortUrl;
11
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
12
use Symfony\Component\Console\Application;
13
use Symfony\Component\Console\Tester\CommandTester;
14
use Zend\I18n\Translator\Translator;
15
use Zend\Paginator\Adapter\ArrayAdapter;
16
use Zend\Paginator\Paginator;
17
18
class ListShortUrlsCommandTest extends TestCase
19
{
20
    /**
21
     * @var CommandTester
22
     */
23
    protected $commandTester;
24
    /**
25
     * @var ObjectProphecy
26
     */
27
    protected $shortUrlService;
28
29
    public function setUp()
30
    {
31
        $this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class);
32
        $app = new Application();
33
        $command = new ListShortUrlsCommand($this->shortUrlService->reveal(), Translator::factory([]), []);
34
        $app->add($command);
35
        $this->commandTester = new CommandTester($command);
36
    }
37
38
    /**
39
     * @test
40
     */
41
    public function noInputCallsListJustOnce()
42
    {
43
        $this->shortUrlService->listShortUrls(1, null, [], null)->willReturn(new Paginator(new ArrayAdapter()))
44
                                                                ->shouldBeCalledOnce();
45
46
        $this->commandTester->setInputs(['n']);
47
        $this->commandTester->execute(['command' => 'shortcode:list']);
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function loadingMorePagesCallsListMoreTimes()
54
    {
55
        // The paginator will return more than one page
56
        $data = [];
57
        for ($i = 0; $i < 50; $i++) {
58
            $data[] = new ShortUrl('url_' . $i);
59
        }
60
61
        $this->shortUrlService->listShortUrls(Argument::cetera())->will(function () use (&$data) {
62
            return new Paginator(new ArrayAdapter($data));
63
        })->shouldBeCalledTimes(3);
64
65
        $this->commandTester->setInputs(['y', 'y', 'n']);
66
        $this->commandTester->execute(['command' => 'shortcode:list']);
67
        $output = $this->commandTester->getDisplay();
68
69
        $this->assertContains('Continue with page 2?', $output);
70
        $this->assertContains('Continue with page 3?', $output);
71
        $this->assertContains('Continue with page 4?', $output);
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function havingMorePagesButAnsweringNoCallsListJustOnce()
78
    {
79
        // The paginator will return more than one page
80
        $data = [];
81
        for ($i = 0; $i < 30; $i++) {
82
            $data[] = new ShortUrl('url_' . $i);
83
        }
84
85
        $this->shortUrlService->listShortUrls(1, null, [], null)->willReturn(new Paginator(new ArrayAdapter($data)))
86
                                                                ->shouldBeCalledOnce();
87
88
        $this->commandTester->setInputs(['n']);
89
        $this->commandTester->execute(['command' => 'shortcode:list']);
90
        $output = $this->commandTester->getDisplay();
91
92
        $this->assertContains('url_1', $output);
93
        $this->assertContains('url_9', $output);
94
        $this->assertNotContains('url_10', $output);
95
        $this->assertNotContains('url_20', $output);
96
        $this->assertNotContains('url_30', $output);
97
        $this->assertContains('Continue with page 2?', $output);
98
        $this->assertNotContains('Continue with page 3?', $output);
99
    }
100
101
    /**
102
     * @test
103
     */
104
    public function passingPageWillMakeListStartOnThatPage()
105
    {
106
        $page = 5;
107
        $this->shortUrlService->listShortUrls($page, null, [], null)->willReturn(new Paginator(new ArrayAdapter()))
108
                                                                    ->shouldBeCalledOnce();
109
110
        $this->commandTester->setInputs(['y']);
111
        $this->commandTester->execute([
112
            'command' => 'shortcode:list',
113
            '--page' => $page,
114
        ]);
115
    }
116
117
    /**
118
     * @test
119
     */
120
    public function ifTagsFlagIsProvidedTagsColumnIsIncluded()
121
    {
122
        $this->shortUrlService->listShortUrls(1, null, [], null)->willReturn(new Paginator(new ArrayAdapter()))
123
                                                                ->shouldBeCalledOnce();
124
125
        $this->commandTester->setInputs(['y']);
126
        $this->commandTester->execute([
127
            'command' => 'shortcode:list',
128
            '--showTags' => true,
129
        ]);
130
        $output = $this->commandTester->getDisplay();
131
        $this->assertContains('Tags', $output);
132
    }
133
}
134