2016-10-22 19:52:40 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-10-22 19:52:40 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware;
|
|
|
|
|
2018-03-26 18:36:58 +03:00
|
|
|
use PHPUnit\Framework\Assert;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-03-26 18:36:58 +03:00
|
|
|
use Prophecy\Argument;
|
2016-10-22 19:52:40 +03:00
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
2018-03-26 20:02:41 +03:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2016-10-22 19:52:40 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware;
|
|
|
|
use Zend\Diactoros\Response;
|
2018-12-26 01:01:30 +03:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2016-10-22 19:52:40 +03:00
|
|
|
use Zend\Diactoros\Uri;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2018-10-28 10:24:06 +03:00
|
|
|
use function array_shift;
|
2016-10-22 19:52:40 +03:00
|
|
|
|
|
|
|
class PathVersionMiddlewareTest extends TestCase
|
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var PathVersionMiddleware */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $middleware;
|
2016-10-22 19:52:40 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2016-10-22 19:52:40 +03:00
|
|
|
{
|
|
|
|
$this->middleware = new PathVersionMiddleware();
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2016-10-22 19:52:40 +03:00
|
|
|
public function whenVersionIsProvidedRequestRemainsUnchanged()
|
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withUri(new Uri('/v2/foo'));
|
2018-03-26 18:36:58 +03:00
|
|
|
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$process = $delegate->handle($request)->willReturn(new Response());
|
2018-03-26 18:36:58 +03:00
|
|
|
|
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
|
|
|
|
|
|
|
$process->shouldHaveBeenCalled();
|
2016-10-22 19:52:40 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2016-10-22 19:52:40 +03:00
|
|
|
public function versionOneIsPrependedWhenNoVersionIsDefined()
|
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withUri(new Uri('/bar/baz'));
|
2018-03-26 18:36:58 +03:00
|
|
|
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$delegate->handle(Argument::type(Request::class))->will(function (array $args) use ($request) {
|
2018-10-28 10:24:06 +03:00
|
|
|
$req = array_shift($args);
|
2018-03-26 18:36:58 +03:00
|
|
|
|
|
|
|
Assert::assertNotSame($request, $req);
|
2018-04-07 09:31:03 +03:00
|
|
|
Assert::assertEquals('/v1/bar/baz', $req->getUri()->getPath());
|
2018-03-26 18:36:58 +03:00
|
|
|
return new Response();
|
2016-10-23 11:29:54 +03:00
|
|
|
});
|
2018-03-26 18:36:58 +03:00
|
|
|
|
|
|
|
|
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
2016-10-23 11:29:54 +03:00
|
|
|
}
|
2016-10-22 19:52:40 +03:00
|
|
|
}
|