shlink/module/Rest/test/Middleware/PathVersionMiddlewareTest.php

59 lines
1.7 KiB
PHP
Raw Normal View History

2016-10-22 19:52:40 +03:00
<?php
2019-10-05 18:26:10 +03:00
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;
use PHPUnit\Framework\Assert;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
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;
use Zend\Diactoros\ServerRequest;
2016-10-22 19:52:40 +03:00
use Zend\Diactoros\Uri;
use function array_shift;
2016-10-22 19:52:40 +03:00
class PathVersionMiddlewareTest extends TestCase
{
private PathVersionMiddleware $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 */
public function whenVersionIsProvidedRequestRemainsUnchanged(): void
2016-10-22 19:52:40 +03:00
{
$request = (new ServerRequest())->withUri(new Uri('/v2/foo'));
2018-03-26 20:02:41 +03:00
$delegate = $this->prophesize(RequestHandlerInterface::class);
$process = $delegate->handle($request)->willReturn(new Response());
$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 */
public function versionOneIsPrependedWhenNoVersionIsDefined(): void
2016-10-22 19:52:40 +03:00
{
$request = (new ServerRequest())->withUri(new Uri('/bar/baz'));
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) {
$req = array_shift($args);
Assert::assertNotSame($request, $req);
Assert::assertEquals('/v1/bar/baz', $req->getUri()->getPath());
return new Response();
});
$this->middleware->process($request, $delegate->reveal());
}
2016-10-22 19:52:40 +03:00
}