Completed
Push — master ( 8cc4d3...038254 )
by Alejandro
16s queued 11s
created

CreateShortUrlActionTest::provideRequestBodies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
6
7
use Cake\Chronos\Chronos;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\Argument;
10
use Prophecy\Prophecy\ObjectProphecy;
11
use Shlinkio\Shlink\Core\Entity\ShortUrl;
12
use Shlinkio\Shlink\Core\Exception\ValidationException;
13
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
14
use Shlinkio\Shlink\Core\Service\UrlShortener;
15
use Shlinkio\Shlink\Rest\Action\ShortUrl\CreateShortUrlAction;
16
use Zend\Diactoros\ServerRequest;
17
use Zend\Diactoros\ServerRequestFactory;
18
use Zend\Diactoros\Uri;
19
20
use function strpos;
21
22
class CreateShortUrlActionTest extends TestCase
23
{
24
    private const DOMAIN_CONFIG = [
25
        'schema' => 'http',
26
        'hostname' => 'foo.com',
27
    ];
28
29
    /** @var CreateShortUrlAction */
30
    private $action;
31
    /** @var ObjectProphecy */
32
    private $urlShortener;
33
34
    public function setUp(): void
35
    {
36
        $this->urlShortener = $this->prophesize(UrlShortener::class);
37
        $this->action = new CreateShortUrlAction($this->urlShortener->reveal(), self::DOMAIN_CONFIG);
38
    }
39
40
    /** @test */
41
    public function missingLongUrlParamReturnsError(): void
42
    {
43
        $this->expectException(ValidationException::class);
44
        $this->action->handle(new ServerRequest());
45
    }
46
47
    /**
48
     * @test
49
     * @dataProvider provideRequestBodies
50
     */
51
    public function properShortcodeConversionReturnsData(array $body, ShortUrlMeta $expectedMeta): void
52
    {
53
        $shortUrl = new ShortUrl('');
54
        $shorten = $this->urlShortener->urlToShortCode(
55
            Argument::type(Uri::class),
56
            Argument::type('array'),
57
            $expectedMeta
58
        )->willReturn($shortUrl);
59
60
        $request = ServerRequestFactory::fromGlobals()->withParsedBody($body);
61
        $response = $this->action->handle($request);
62
63
        $this->assertEquals(200, $response->getStatusCode());
64
        $this->assertTrue(strpos($response->getBody()->getContents(), $shortUrl->toString(self::DOMAIN_CONFIG)) > 0);
65
        $shorten->shouldHaveBeenCalledOnce();
66
    }
67
68
    public function provideRequestBodies(): iterable
69
    {
70
        $fullMeta = [
71
            'longUrl' => 'http://www.domain.com/foo/bar',
72
            'validSince' => Chronos::now()->toAtomString(),
73
            'validUntil' => Chronos::now()->toAtomString(),
74
            'customSlug' => 'foo-bar-baz',
75
            'maxVisits' => 50,
76
            'findIfExists' => true,
77
            'domain' => 'my-domain.com',
78
        ];
79
80
        yield [['longUrl' => 'http://www.domain.com/foo/bar'], ShortUrlMeta::createEmpty()];
81
        yield [$fullMeta, ShortUrlMeta::createFromRawData($fullMeta)];
82
    }
83
84
    /**
85
     * @test
86
     * @dataProvider provideInvalidDomains
87
     */
88
    public function anInvalidDomainReturnsError(string $domain): void
89
    {
90
        $shortUrl = new ShortUrl('');
91
        $urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willReturn($shortUrl);
92
93
        $request = (new ServerRequest())->withParsedBody([
94
            'longUrl' => 'http://www.domain.com/foo/bar',
95
            'domain' => $domain,
96
        ]);
97
98
        $this->expectException(ValidationException::class);
99
        $urlToShortCode->shouldNotBeCalled();
100
101
        $this->action->handle($request);
102
    }
103
104
    public function provideInvalidDomains(): iterable
105
    {
106
        yield ['localhost:80000'];
107
        yield ['127.0.0.1'];
108
        yield ['???/&%$&'];
109
    }
110
}
111