Migrated SingleStepCreateShortUrlActionTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 22:16:19 +02:00
parent acc9cb94b5
commit 9ac6a50e66

View File

@ -5,10 +5,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl; namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
use Laminas\Diactoros\ServerRequest; use Laminas\Diactoros\ServerRequest;
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\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
@ -19,21 +17,18 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
class SingleStepCreateShortUrlActionTest extends TestCase class SingleStepCreateShortUrlActionTest extends TestCase
{ {
use ProphecyTrait;
private SingleStepCreateShortUrlAction $action; private SingleStepCreateShortUrlAction $action;
private ObjectProphecy $urlShortener; private MockObject $urlShortener;
private ObjectProphecy $transformer;
protected function setUp(): void protected function setUp(): void
{ {
$this->urlShortener = $this->prophesize(UrlShortenerInterface::class); $this->urlShortener = $this->createMock(UrlShortenerInterface::class);
$this->transformer = $this->prophesize(DataTransformerInterface::class); $transformer = $this->createMock(DataTransformerInterface::class);
$this->transformer->transform(Argument::type(ShortUrl::class))->willReturn([]); $transformer->method('transform')->willReturn([]);
$this->action = new SingleStepCreateShortUrlAction( $this->action = new SingleStepCreateShortUrlAction(
$this->urlShortener->reveal(), $this->urlShortener,
$this->transformer->reveal(), $transformer,
new UrlShortenerOptions(), new UrlShortenerOptions(),
); );
} }
@ -46,13 +41,12 @@ class SingleStepCreateShortUrlActionTest extends TestCase
$request = (new ServerRequest())->withQueryParams([ $request = (new ServerRequest())->withQueryParams([
'longUrl' => 'http://foobar.com', 'longUrl' => 'http://foobar.com',
])->withAttribute(ApiKey::class, $apiKey); ])->withAttribute(ApiKey::class, $apiKey);
$generateShortCode = $this->urlShortener->shorten( $this->urlShortener->expects($this->once())->method('shorten')->with(
ShortUrlCreation::fromRawData(['apiKey' => $apiKey, 'longUrl' => 'http://foobar.com']), ShortUrlCreation::fromRawData(['apiKey' => $apiKey, 'longUrl' => 'http://foobar.com']),
)->willReturn(ShortUrl::createEmpty()); )->willReturn(ShortUrl::createEmpty());
$resp = $this->action->handle($request); $resp = $this->action->handle($request);
self::assertEquals(200, $resp->getStatusCode()); self::assertEquals(200, $resp->getStatusCode());
$generateShortCode->shouldHaveBeenCalled();
} }
} }