PixelActionTest.php 1.8 KB

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