Completed
Push — master ( f7424d...b53e51 )
by Alejandro
07:43
created

PreviewActionTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A invalidShortCodeFallsBackToNextMiddleware() 0 12 1
A correctShortCodeReturnsImageResponse() 0 16 1
A invalidShortCodeExceptionFallsBackToNextMiddleware() 0 14 1
1
<?php
2
namespace ShlinkioTest\Shlink\Core\Action;
3
4
use Interop\Http\ServerMiddleware\DelegateInterface;
5
use PHPUnit\Framework\TestCase;
6
use Prophecy\Argument;
7
use Prophecy\Prophecy\ObjectProphecy;
8
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
9
use Shlinkio\Shlink\Core\Action\PreviewAction;
10
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
11
use Shlinkio\Shlink\Core\Service\UrlShortener;
12
use ShlinkioTest\Shlink\Common\Util\TestUtils;
13
use Zend\Diactoros\ServerRequestFactory;
14
15
class PreviewActionTest extends TestCase
16
{
17
    /**
18
     * @var PreviewAction
19
     */
20
    protected $action;
21
    /**
22
     * @var ObjectProphecy
23
     */
24
    private $previewGenerator;
25
    /**
26
     * @var ObjectProphecy
27
     */
28
    private $urlShortener;
29
30
    public function setUp()
31
    {
32
        $this->previewGenerator = $this->prophesize(PreviewGenerator::class);
33
        $this->urlShortener = $this->prophesize(UrlShortener::class);
34
        $this->action = new PreviewAction($this->previewGenerator->reveal(), $this->urlShortener->reveal());
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function invalidShortCodeFallsBackToNextMiddleware()
41
    {
42
        $shortCode = 'abc123';
43
        $this->urlShortener->shortCodeToUrl($shortCode)->willReturn(null)->shouldBeCalledTimes(1);
44
        $delegate = $this->prophesize(DelegateInterface::class);
45
        $delegate->process(Argument::cetera())->shouldBeCalledTimes(1);
46
47
        $this->action->process(
48
            ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
49
            $delegate->reveal()
50
        );
51
    }
52
53
    /**
54
     * @test
55
     */
56
    public function correctShortCodeReturnsImageResponse()
57
    {
58
        $shortCode = 'abc123';
59
        $url = 'foobar.com';
60
        $path = __FILE__;
61
        $this->urlShortener->shortCodeToUrl($shortCode)->willReturn($url)->shouldBeCalledTimes(1);
62
        $this->previewGenerator->generatePreview($url)->willReturn($path)->shouldBeCalledTimes(1);
63
64
        $resp = $this->action->process(
65
            ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
66
            TestUtils::createDelegateMock()->reveal()
67
        );
68
69
        $this->assertEquals(filesize($path), $resp->getHeaderLine('Content-length'));
70
        $this->assertEquals((new \finfo(FILEINFO_MIME))->file($path), $resp->getHeaderLine('Content-type'));
71
    }
72
73
    /**
74
     * @test
75
     */
76
    public function invalidShortCodeExceptionFallsBackToNextMiddleware()
77
    {
78
        $shortCode = 'abc123';
79
        $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class)
80
                                                       ->shouldBeCalledTimes(1);
81
        $delegate = $this->prophesize(DelegateInterface::class);
82
83
        $this->action->process(
84
            ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
85
            $delegate->reveal()
86
        );
87
88
        $delegate->process(Argument::any())->shouldHaveBeenCalledTimes(1);
89
    }
90
}
91