Migrated CreateShortUrlActionTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 22:09:37 +02:00
parent 74176c298f
commit a84b642ba5

View file

@ -8,10 +8,8 @@ use Cake\Chronos\Chronos;
use Laminas\Diactoros\Response\JsonResponse; use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\ServerRequest; use Laminas\Diactoros\ServerRequest;
use Laminas\Diactoros\ServerRequestFactory; use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Exception\ValidationException;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
@ -23,23 +21,16 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
class CreateShortUrlActionTest extends TestCase class CreateShortUrlActionTest extends TestCase
{ {
use ProphecyTrait;
private CreateShortUrlAction $action; private CreateShortUrlAction $action;
private ObjectProphecy $urlShortener; private MockObject $urlShortener;
private ObjectProphecy $transformer; private MockObject $transformer;
protected function setUp(): void protected function setUp(): void
{ {
$this->urlShortener = $this->prophesize(UrlShortener::class); $this->urlShortener = $this->createMock(UrlShortener::class);
$this->transformer = $this->prophesize(DataTransformerInterface::class); $this->transformer = $this->createMock(DataTransformerInterface::class);
$this->transformer->transform(Argument::type(ShortUrl::class))->willReturn([]);
$this->action = new CreateShortUrlAction( $this->action = new CreateShortUrlAction($this->urlShortener, $this->transformer, new UrlShortenerOptions());
$this->urlShortener->reveal(),
$this->transformer->reveal(),
new UrlShortenerOptions(),
);
} }
/** @test */ /** @test */
@ -58,8 +49,12 @@ class CreateShortUrlActionTest extends TestCase
]; ];
$expectedMeta['apiKey'] = $apiKey; $expectedMeta['apiKey'] = $apiKey;
$shorten = $this->urlShortener->shorten(ShortUrlCreation::fromRawData($expectedMeta))->willReturn($shortUrl); $this->urlShortener->expects($this->once())->method('shorten')->with(
$transform = $this->transformer->transform($shortUrl)->willReturn(['shortUrl' => 'stringified_short_url']); ShortUrlCreation::fromRawData($expectedMeta),
)->willReturn($shortUrl);
$this->transformer->expects($this->once())->method('transform')->with($shortUrl)->willReturn(
['shortUrl' => 'stringified_short_url'],
);
$request = ServerRequestFactory::fromGlobals()->withParsedBody($body)->withAttribute(ApiKey::class, $apiKey); $request = ServerRequestFactory::fromGlobals()->withParsedBody($body)->withAttribute(ApiKey::class, $apiKey);
@ -69,8 +64,6 @@ class CreateShortUrlActionTest extends TestCase
self::assertEquals(200, $response->getStatusCode()); self::assertEquals(200, $response->getStatusCode());
self::assertEquals('stringified_short_url', $payload['shortUrl']); self::assertEquals('stringified_short_url', $payload['shortUrl']);
$shorten->shouldHaveBeenCalledOnce();
$transform->shouldHaveBeenCalledOnce();
} }
/** /**
@ -79,8 +72,8 @@ class CreateShortUrlActionTest extends TestCase
*/ */
public function anInvalidDomainReturnsError(string $domain): void public function anInvalidDomainReturnsError(string $domain): void
{ {
$shortUrl = ShortUrl::createEmpty(); $this->urlShortener->expects($this->never())->method('shorten');
$urlToShortCode = $this->urlShortener->shorten(Argument::cetera())->willReturn($shortUrl); $this->transformer->expects($this->never())->method('transform');
$request = (new ServerRequest())->withParsedBody([ $request = (new ServerRequest())->withParsedBody([
'longUrl' => 'http://www.domain.com/foo/bar', 'longUrl' => 'http://www.domain.com/foo/bar',
@ -88,7 +81,6 @@ class CreateShortUrlActionTest extends TestCase
])->withAttribute(ApiKey::class, ApiKey::create()); ])->withAttribute(ApiKey::class, ApiKey::create());
$this->expectException(ValidationException::class); $this->expectException(ValidationException::class);
$urlToShortCode->shouldNotBeCalled();
$this->action->handle($request); $this->action->handle($request);
} }