shlink/module/Rest/test/Action/ShortUrl/SingleStepCreateShortUrlActionTest.php

59 lines
1.9 KiB
PHP
Raw Normal View History

<?php
2019-10-05 18:26:10 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
2020-01-01 23:11:53 +03:00
use Laminas\Diactoros\ServerRequest;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
2020-11-02 13:50:19 +03:00
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Rest\Action\ShortUrl\SingleStepCreateShortUrlAction;
2020-11-08 13:28:27 +03:00
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class SingleStepCreateShortUrlActionTest extends TestCase
{
2020-11-02 13:50:19 +03:00
use ProphecyTrait;
private SingleStepCreateShortUrlAction $action;
private ObjectProphecy $urlShortener;
private ObjectProphecy $transformer;
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->action = new SingleStepCreateShortUrlAction(
$this->urlShortener->reveal(),
$this->transformer->reveal(),
new UrlShortenerOptions(),
);
}
2019-02-17 22:28:34 +03:00
/** @test */
public function properDataIsPassedWhenGeneratingShortCode(): void
{
$apiKey = ApiKey::create();
2020-11-08 13:28:27 +03:00
$request = (new ServerRequest())->withQueryParams([
'longUrl' => 'http://foobar.com',
2021-01-21 21:26:19 +03:00
])->withAttribute(ApiKey::class, $apiKey);
$generateShortCode = $this->urlShortener->shorten(
ShortUrlMeta::fromRawData(['apiKey' => $apiKey, 'longUrl' => 'http://foobar.com']),
)->willReturn(ShortUrl::createEmpty());
$resp = $this->action->handle($request);
2020-10-04 01:35:14 +03:00
self::assertEquals(200, $resp->getStatusCode());
$generateShortCode->shouldHaveBeenCalled();
}
}