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

57 lines
2.1 KiB
PHP
Raw Normal View History

2018-03-26 13:17:33 -05:00
<?php
2019-10-05 10:26:10 -05:00
2018-03-26 13:17:33 -05:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Action;
2020-01-01 14:11:53 -06:00
use Laminas\Diactoros\ServerRequest;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
2018-03-26 13:17:33 -05:00
use PHPUnit\Framework\TestCase;
use Psr\Http\Server\RequestHandlerInterface;
2018-03-26 13:17:33 -05:00
use Shlinkio\Shlink\Common\Response\PixelResponse;
use Shlinkio\Shlink\Core\Action\PixelAction;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
2018-03-26 13:17:33 -05:00
2024-11-24 07:05:33 -06:00
use const Shlinkio\Shlink\REDIRECT_URL_REQUEST_ATTRIBUTE;
2018-03-26 13:17:33 -05:00
class PixelActionTest extends TestCase
{
2019-12-29 15:27:00 -06:00
private PixelAction $action;
2022-10-24 12:53:13 -05:00
private MockObject & ShortUrlResolverInterface $urlResolver;
private MockObject & RequestTrackerInterface $requestTracker;
2018-03-26 13:17:33 -05:00
protected function setUp(): void
2018-03-26 13:17:33 -05:00
{
$this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
$this->requestTracker = $this->createMock(RequestTrackerInterface::class);
2018-03-26 13:17:33 -05:00
$this->action = new PixelAction($this->urlResolver, $this->requestTracker);
2018-03-26 13:17:33 -05:00
}
#[Test]
public function imageIsReturned(): void
2018-03-26 13:17:33 -05:00
{
$shortCode = 'abc123';
2024-11-24 07:05:33 -06:00
$shortUrl = ShortUrl::withLongUrl('http://domain.com/foo/bar');
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''),
2024-11-24 07:05:33 -06:00
)->willReturn($shortUrl);
$this->requestTracker->expects($this->once())->method('trackIfApplicable')->with(
$shortUrl,
$request->withAttribute(REDIRECT_URL_REQUEST_ATTRIBUTE, null),
);
2018-03-26 13:17:33 -05:00
$response = $this->action->process($request, $this->createMock(RequestHandlerInterface::class));
2018-03-26 13:17:33 -05:00
2020-10-03 17:35:14 -05:00
self::assertInstanceOf(PixelResponse::class, $response);
self::assertEquals(200, $response->getStatusCode());
self::assertEquals('image/gif', $response->getHeaderLine('content-type'));
2018-03-26 13:17:33 -05:00
}
}