Migrated PixelActionTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-21 18:32:34 +02:00
parent 847cc2bc50
commit 843754b7e7

View file

@ -5,10 +5,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Action;
use Laminas\Diactoros\ServerRequest;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Common\Response\PixelResponse;
use Shlinkio\Shlink\Core\Action\PixelAction;
@ -19,32 +17,29 @@ use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
class PixelActionTest extends TestCase
{
use ProphecyTrait;
private PixelAction $action;
private ObjectProphecy $urlResolver;
private ObjectProphecy $requestTracker;
private MockObject $urlResolver;
private MockObject $requestTracker;
protected function setUp(): void
{
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
$this->requestTracker = $this->prophesize(RequestTrackerInterface::class);
$this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
$this->requestTracker = $this->createMock(RequestTrackerInterface::class);
$this->action = new PixelAction($this->urlResolver->reveal(), $this->requestTracker->reveal());
$this->action = new PixelAction($this->urlResolver, $this->requestTracker);
}
/** @test */
public function imageIsReturned(): void
{
$shortCode = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''),
)->willReturn(ShortUrl::withLongUrl('http://domain.com/foo/bar'))
->shouldBeCalledOnce();
$this->requestTracker->trackIfApplicable(Argument::cetera())->shouldBeCalledOnce();
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
)->willReturn(ShortUrl::withLongUrl('http://domain.com/foo/bar'));
$this->requestTracker->expects($this->once())->method('trackIfApplicable')->withAnyParameters();
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$response = $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
$response = $this->action->process($request, $this->createMock(RequestHandlerInterface::class));
self::assertInstanceOf(PixelResponse::class, $response);
self::assertEquals(200, $response->getStatusCode());