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