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

49 lines
1.9 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\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
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
}
2019-02-17 13:28:34 -06:00
/** @test */
public function imageIsReturned(): void
2018-03-26 13:17:33 -05:00
{
$shortCode = 'abc123';
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''),
)->willReturn(ShortUrl::withLongUrl('http://domain.com/foo/bar'));
$this->requestTracker->expects($this->once())->method('trackIfApplicable')->withAnyParameters();
2018-03-26 13:17:33 -05:00
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$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
}
}