2018-09-20 21:27:34 +03:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware\ShortUrl;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\Assert;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Argument;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Message\UriInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\ShortCodePathMiddleware;
|
|
|
|
use Zend\Diactoros\Response;
|
|
|
|
use Zend\Diactoros\Uri;
|
|
|
|
|
|
|
|
class ShortCodePathMiddlewareTest extends TestCase
|
|
|
|
{
|
|
|
|
private $middleware;
|
|
|
|
private $requestHandler;
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2018-09-20 21:27:34 +03:00
|
|
|
{
|
|
|
|
$this->middleware = new ShortCodePathMiddleware();
|
|
|
|
$this->requestHandler = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn(new Response());
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2018-09-20 21:27:34 +03:00
|
|
|
public function properlyReplacesTheOldPathByTheNewOne()
|
|
|
|
{
|
|
|
|
$uri = new Uri('/short-codes/foo');
|
|
|
|
|
|
|
|
$request = $this->prophesize(ServerRequestInterface::class);
|
|
|
|
$request->getUri()->willReturn($uri);
|
|
|
|
$withUri = $request->withUri(Argument::that(function (UriInterface $uri) {
|
|
|
|
$path = $uri->getPath();
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
Assert::assertStringContainsString('/short-urls', $path);
|
|
|
|
Assert::assertStringNotContainsString('/short-codes', $path);
|
2018-09-20 21:27:34 +03:00
|
|
|
|
|
|
|
return $uri;
|
|
|
|
}))->willReturn($request->reveal());
|
|
|
|
|
|
|
|
$this->middleware->process($request->reveal(), $this->requestHandler->reveal());
|
|
|
|
|
2018-11-11 15:18:21 +03:00
|
|
|
$withUri->shouldHaveBeenCalledOnce();
|
2018-09-20 21:27:34 +03:00
|
|
|
}
|
|
|
|
}
|