Completed
Pull Request — master (#500)
by Alejandro
06:22
created

providingNonUniqueSlugOutputsError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
5
6
use PHPUnit\Framework\Assert;
7
use PHPUnit\Framework\TestCase;
8
use Prophecy\Argument;
9
use Prophecy\Prophecy\ObjectProphecy;
10
use Psr\Http\Message\UriInterface;
11
use Shlinkio\Shlink\CLI\Command\ShortUrl\GenerateShortUrlCommand;
12
use Shlinkio\Shlink\CLI\Util\ExitCodes;
13
use Shlinkio\Shlink\Core\Entity\ShortUrl;
14
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
15
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
16
use Shlinkio\Shlink\Core\Service\UrlShortener;
17
use Symfony\Component\Console\Application;
18
use Symfony\Component\Console\Tester\CommandTester;
19
20
class GenerateShortUrlCommandTest extends TestCase
21
{
22
    /** @var CommandTester */
23
    private $commandTester;
24
    /** @var ObjectProphecy */
25
    private $urlShortener;
26
27
    public function setUp(): void
28
    {
29
        $this->urlShortener = $this->prophesize(UrlShortener::class);
30
        $command = new GenerateShortUrlCommand($this->urlShortener->reveal(), [
31
            'schema' => 'http',
32
            'hostname' => 'foo.com',
33
        ]);
34
        $app = new Application();
35
        $app->add($command);
36
        $this->commandTester = new CommandTester($command);
37
    }
38
39
    /** @test */
40
    public function properShortCodeIsCreatedIfLongUrlIsCorrect(): void
41
    {
42
        $urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willReturn(
43
            (new ShortUrl(''))->setShortCode('abc123')
44
        );
45
46
        $this->commandTester->execute([
47
            'longUrl' => 'http://domain.com/foo/bar',
48
            '--maxVisits' => '3',
49
        ]);
50
        $output = $this->commandTester->getDisplay();
51
52
        $this->assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
53
        $this->assertStringContainsString('http://foo.com/abc123', $output);
54
        $urlToShortCode->shouldHaveBeenCalledOnce();
55
    }
56
57
    /** @test */
58
    public function exceptionWhileParsingLongUrlOutputsError(): void
59
    {
60
        $this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(new InvalidUrlException())
61
                                                               ->shouldBeCalledOnce();
62
63
        $this->commandTester->execute(['longUrl' => 'http://domain.com/invalid']);
64
        $output = $this->commandTester->getDisplay();
65
66
        $this->assertEquals(ExitCodes::EXIT_FAILURE, $this->commandTester->getStatusCode());
67
        $this->assertStringContainsString('Provided URL "http://domain.com/invalid" is invalid.', $output);
68
    }
69
70
    /** @test */
71
    public function providingNonUniqueSlugOutputsError(): void
72
    {
73
        $urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(
74
            NonUniqueSlugException::class
75
        );
76
77
        $this->commandTester->execute(['longUrl' => 'http://domain.com/invalid', '--customSlug' => 'my-slug']);
78
        $output = $this->commandTester->getDisplay();
79
80
        $this->assertEquals(ExitCodes::EXIT_FAILURE, $this->commandTester->getStatusCode());
81
        $this->assertStringContainsString('Provided slug "my-slug" is already in use', $output);
82
        $urlToShortCode->shouldHaveBeenCalledOnce();
83
    }
84
85
    /** @test */
86
    public function properlyProcessesProvidedTags(): void
87
    {
88
        $urlToShortCode = $this->urlShortener->urlToShortCode(
89
            Argument::type(UriInterface::class),
90
            Argument::that(function (array $tags) {
91
                Assert::assertEquals(['foo', 'bar', 'baz', 'boo', 'zar'], $tags);
92
                return $tags;
93
            }),
94
            Argument::cetera()
95
        )->willReturn((new ShortUrl(''))->setShortCode('abc123'));
96
97
        $this->commandTester->execute([
98
            'longUrl' => 'http://domain.com/foo/bar',
99
            '--tags' => ['foo,bar', 'baz', 'boo,zar,baz'],
100
        ]);
101
        $output = $this->commandTester->getDisplay();
102
103
        $this->assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
104
        $this->assertStringContainsString('http://foo.com/abc123', $output);
105
        $urlToShortCode->shouldHaveBeenCalledOnce();
106
    }
107
}
108