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\Core\Entity\ShortUrl; |
13
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException; |
14
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener; |
15
|
|
|
use Symfony\Component\Console\Application; |
16
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
17
|
|
|
use Zend\I18n\Translator\Translator; |
18
|
|
|
|
19
|
|
|
class GenerateShortUrlCommandTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var CommandTester |
23
|
|
|
*/ |
24
|
|
|
protected $commandTester; |
25
|
|
|
/** |
26
|
|
|
* @var ObjectProphecy |
27
|
|
|
*/ |
28
|
|
|
protected $urlShortener; |
29
|
|
|
|
30
|
|
|
public function setUp() |
31
|
|
|
{ |
32
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class); |
33
|
|
|
$command = new GenerateShortUrlCommand($this->urlShortener->reveal(), Translator::factory([]), [ |
34
|
|
|
'schema' => 'http', |
35
|
|
|
'hostname' => 'foo.com', |
36
|
|
|
]); |
37
|
|
|
$app = new Application(); |
38
|
|
|
$app->add($command); |
39
|
|
|
$this->commandTester = new CommandTester($command); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
*/ |
45
|
|
|
public function properShortCodeIsCreatedIfLongUrlIsCorrect() |
46
|
|
|
{ |
47
|
|
|
$urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willReturn( |
48
|
|
|
(new ShortUrl(''))->setShortCode('abc123') |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$this->commandTester->execute([ |
52
|
|
|
'command' => 'shortcode:generate', |
53
|
|
|
'longUrl' => 'http://domain.com/foo/bar', |
54
|
|
|
'--maxVisits' => '3', |
55
|
|
|
]); |
56
|
|
|
$output = $this->commandTester->getDisplay(); |
57
|
|
|
|
58
|
|
|
$this->assertContains('http://foo.com/abc123', $output); |
59
|
|
|
$urlToShortCode->shouldHaveBeenCalledOnce(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @test |
64
|
|
|
*/ |
65
|
|
|
public function exceptionWhileParsingLongUrlOutputsError() |
66
|
|
|
{ |
67
|
|
|
$this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(new InvalidUrlException()) |
68
|
|
|
->shouldBeCalledOnce(); |
69
|
|
|
|
70
|
|
|
$this->commandTester->execute([ |
71
|
|
|
'command' => 'shortcode:generate', |
72
|
|
|
'longUrl' => 'http://domain.com/invalid', |
73
|
|
|
]); |
74
|
|
|
$output = $this->commandTester->getDisplay(); |
75
|
|
|
$this->assertContains( |
76
|
|
|
'Provided URL "http://domain.com/invalid" is invalid.', |
77
|
|
|
$output |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @test |
83
|
|
|
*/ |
84
|
|
|
public function properlyProcessesProvidedTags() |
85
|
|
|
{ |
86
|
|
|
$urlToShortCode = $this->urlShortener->urlToShortCode( |
87
|
|
|
Argument::type(UriInterface::class), |
88
|
|
|
Argument::that(function (array $tags) { |
89
|
|
|
Assert::assertEquals(['foo', 'bar', 'baz', 'boo', 'zar'], $tags); |
90
|
|
|
return $tags; |
91
|
|
|
}), |
92
|
|
|
Argument::cetera() |
93
|
|
|
)->willReturn((new ShortUrl(''))->setShortCode('abc123')); |
94
|
|
|
|
95
|
|
|
$this->commandTester->execute([ |
96
|
|
|
'command' => 'shortcode:generate', |
97
|
|
|
'longUrl' => 'http://domain.com/foo/bar', |
98
|
|
|
'--tags' => ['foo,bar', 'baz', 'boo,zar'], |
99
|
|
|
]); |
100
|
|
|
$output = $this->commandTester->getDisplay(); |
101
|
|
|
|
102
|
|
|
$this->assertContains('http://foo.com/abc123', $output); |
103
|
|
|
$urlToShortCode->shouldHaveBeenCalledOnce(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|