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;
|
2023-02-09 22:42:18 +03:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-21 19:32:34 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-03-26 21:17:33 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-08-12 19:34:52 +03:00
|
|
|
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;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2022-09-23 19:05:17 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
2022-09-23 19:42:38 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
|
2021-07-15 18:23:09 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
2018-03-26 21:17:33 +03:00
|
|
|
|
|
|
|
class PixelActionTest extends TestCase
|
|
|
|
{
|
2019-12-30 00:27:00 +03:00
|
|
|
private PixelAction $action;
|
2022-10-24 20:53:13 +03:00
|
|
|
private MockObject & ShortUrlResolverInterface $urlResolver;
|
|
|
|
private MockObject & RequestTrackerInterface $requestTracker;
|
2018-03-26 21:17:33 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2018-03-26 21:17:33 +03:00
|
|
|
{
|
2022-10-21 19:32:34 +03:00
|
|
|
$this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
|
|
|
|
$this->requestTracker = $this->createMock(RequestTrackerInterface::class);
|
2018-03-26 21:17:33 +03:00
|
|
|
|
2022-10-21 19:32:34 +03:00
|
|
|
$this->action = new PixelAction($this->urlResolver, $this->requestTracker);
|
2018-03-26 21:17:33 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test]
|
2019-08-12 19:34:52 +03:00
|
|
|
public function imageIsReturned(): void
|
2018-03-26 21:17:33 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2022-10-21 19:32:34 +03:00
|
|
|
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''),
|
2022-10-21 19:32:34 +03:00
|
|
|
)->willReturn(ShortUrl::withLongUrl('http://domain.com/foo/bar'));
|
|
|
|
$this->requestTracker->expects($this->once())->method('trackIfApplicable')->withAnyParameters();
|
2018-03-26 21:17:33 +03:00
|
|
|
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
|
2022-10-21 19:32:34 +03:00
|
|
|
$response = $this->action->process($request, $this->createMock(RequestHandlerInterface::class));
|
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
|
|
|
}
|
|
|
|
}
|