shlink/module/Core/test/Action/PixelActionTest.php

55 lines
1.9 KiB
PHP
Raw Normal View History

2018-03-26 21:17:33 +03:00
<?php
2019-10-05 18:26:10 +03:00
2018-03-26 21:17:33 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Action;
2020-01-01 23:11:53 +03:00
use Laminas\Diactoros\ServerRequest;
2018-03-26 21:17:33 +03:00
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Server\RequestHandlerInterface;
2018-03-26 21:17:33 +03:00
use Shlinkio\Shlink\Common\Response\PixelResponse;
use Shlinkio\Shlink\Core\Action\PixelAction;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
2018-03-26 21:17:33 +03:00
use Shlinkio\Shlink\Core\Options\AppOptions;
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
2018-03-26 21:17:33 +03:00
use Shlinkio\Shlink\Core\Service\VisitsTracker;
class PixelActionTest extends TestCase
{
2019-12-30 00:27:00 +03:00
private PixelAction $action;
private ObjectProphecy $urlResolver;
2019-12-30 00:27:00 +03:00
private ObjectProphecy $visitTracker;
2018-03-26 21:17:33 +03:00
2019-02-16 12:53:45 +03:00
public function setUp(): void
2018-03-26 21:17:33 +03:00
{
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
2018-03-26 21:17:33 +03:00
$this->visitTracker = $this->prophesize(VisitsTracker::class);
$this->action = new PixelAction(
$this->urlResolver->reveal(),
2018-03-26 21:17:33 +03:00
$this->visitTracker->reveal(),
2020-01-01 22:48:31 +03:00
new AppOptions(),
2018-03-26 21:17:33 +03:00
);
}
2019-02-17 22:28:34 +03:00
/** @test */
public function imageIsReturned(): void
2018-03-26 21:17:33 +03:00
{
$shortCode = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))->willReturn(
2020-01-01 22:48:31 +03:00
new ShortUrl('http://domain.com/foo/bar'),
2018-11-11 15:18:21 +03:00
)->shouldBeCalledOnce();
$this->visitTracker->track(Argument::cetera())->shouldBeCalledOnce();
2018-03-26 21:17:33 +03:00
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$response = $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
2018-03-26 21:17:33 +03:00
2020-10-04 01:35:14 +03:00
self::assertInstanceOf(PixelResponse::class, $response);
self::assertEquals(200, $response->getStatusCode());
self::assertEquals('image/gif', $response->getHeaderLine('content-type'));
2018-03-26 21:17:33 +03:00
}
}