|
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\GenerateShortUrlCommand; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException; |
|
12
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener; |
|
13
|
|
|
use Symfony\Component\Console\Application; |
|
14
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
15
|
|
|
use Zend\I18n\Translator\Translator; |
|
16
|
|
|
|
|
17
|
|
|
class GenerateShortcodeCommandTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var CommandTester |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $commandTester; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ObjectProphecy |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $urlShortener; |
|
27
|
|
|
|
|
28
|
|
|
public function setUp() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class); |
|
31
|
|
|
$command = new GenerateShortUrlCommand($this->urlShortener->reveal(), Translator::factory([]), [ |
|
32
|
|
|
'schema' => 'http', |
|
33
|
|
|
'hostname' => 'foo.com', |
|
34
|
|
|
]); |
|
35
|
|
|
$app = new Application(); |
|
36
|
|
|
$app->add($command); |
|
37
|
|
|
$this->commandTester = new CommandTester($command); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @test |
|
42
|
|
|
*/ |
|
43
|
|
|
public function properShortCodeIsCreatedIfLongUrlIsCorrect() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->urlShortener->urlToShortCode(Argument::cetera()) |
|
46
|
|
|
->willReturn( |
|
47
|
|
|
(new ShortUrl())->setShortCode('abc123') |
|
48
|
|
|
->setLongUrl('') |
|
49
|
|
|
) |
|
50
|
|
|
->shouldBeCalledTimes(1); |
|
51
|
|
|
|
|
52
|
|
|
$this->commandTester->execute([ |
|
53
|
|
|
'command' => 'shortcode:generate', |
|
54
|
|
|
'longUrl' => 'http://domain.com/foo/bar', |
|
55
|
|
|
]); |
|
56
|
|
|
$output = $this->commandTester->getDisplay(); |
|
57
|
|
|
$this->assertTrue(strpos($output, 'http://foo.com/abc123') > 0); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @test |
|
62
|
|
|
*/ |
|
63
|
|
|
public function exceptionWhileParsingLongUrlOutputsError() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(new InvalidUrlException()) |
|
66
|
|
|
->shouldBeCalledTimes(1); |
|
67
|
|
|
|
|
68
|
|
|
$this->commandTester->execute([ |
|
69
|
|
|
'command' => 'shortcode:generate', |
|
70
|
|
|
'longUrl' => 'http://domain.com/invalid', |
|
71
|
|
|
]); |
|
72
|
|
|
$output = $this->commandTester->getDisplay(); |
|
73
|
|
|
$this->assertContains( |
|
74
|
|
|
'Provided URL "http://domain.com/invalid" is invalid.', |
|
75
|
|
|
$output |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|