PixelActionTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 21
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A imageIsReturned() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Core\Action;
6
7
use Laminas\Diactoros\ServerRequest;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\Argument;
10
use Prophecy\PhpUnit\ProphecyTrait;
11
use Prophecy\Prophecy\ObjectProphecy;
12
use Psr\Http\Server\RequestHandlerInterface;
13
use Shlinkio\Shlink\Common\Response\PixelResponse;
14
use Shlinkio\Shlink\Core\Action\PixelAction;
15
use Shlinkio\Shlink\Core\Entity\ShortUrl;
16
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
17
use Shlinkio\Shlink\Core\Options\AppOptions;
18
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
19
use Shlinkio\Shlink\Core\Service\VisitsTracker;
20
21
class PixelActionTest extends TestCase
22
{
23
    use ProphecyTrait;
24
25
    private PixelAction $action;
26
    private ObjectProphecy $urlResolver;
27
    private ObjectProphecy $visitTracker;
28
29
    public function setUp(): void
30
    {
31
        $this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
32
        $this->visitTracker = $this->prophesize(VisitsTracker::class);
33
34
        $this->action = new PixelAction(
35
            $this->urlResolver->reveal(),
36
            $this->visitTracker->reveal(),
37
            new AppOptions(),
38
        );
39
    }
40
41
    /** @test */
42
    public function imageIsReturned(): void
43
    {
44
        $shortCode = 'abc123';
45
        $this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))->willReturn(
46
            new ShortUrl('http://domain.com/foo/bar'),
47
        )->shouldBeCalledOnce();
48
        $this->visitTracker->track(Argument::cetera())->shouldBeCalledOnce();
49
50
        $request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
51
        $response = $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
52
53
        self::assertInstanceOf(PixelResponse::class, $response);
54
        self::assertEquals(200, $response->getStatusCode());
55
        self::assertEquals('image/gif', $response->getHeaderLine('content-type'));
56
    }
57
}
58