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;
|
|
|
|
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-10-22 19:52:40 +03:00
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
|
|
use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware;
|
|
|
|
use Zend\Diactoros\Response;
|
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
|
|
|
use Zend\Diactoros\Uri;
|
|
|
|
|
|
|
|
class PathVersionMiddlewareTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var PathVersionMiddleware
|
|
|
|
*/
|
|
|
|
protected $middleware;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->middleware = new PathVersionMiddleware();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function whenVersionIsProvidedRequestRemainsUnchanged()
|
|
|
|
{
|
2017-01-21 22:12:12 +03:00
|
|
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/v2/foo'));
|
2016-10-22 19:52:40 +03:00
|
|
|
$test = $this;
|
|
|
|
$this->middleware->__invoke($request, new Response(), function ($req) use ($request, $test) {
|
|
|
|
$test->assertSame($request, $req);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function versionOneIsPrependedWhenNoVersionIsDefined()
|
|
|
|
{
|
2017-01-21 22:12:12 +03:00
|
|
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/bar/baz'));
|
2016-10-22 19:52:40 +03:00
|
|
|
$test = $this;
|
|
|
|
$this->middleware->__invoke($request, new Response(), function (Request $req) use ($request, $test) {
|
|
|
|
$test->assertNotSame($request, $req);
|
2017-01-21 22:12:12 +03:00
|
|
|
$this->assertEquals('/v1/bar/baz', $req->getUri()->getPath());
|
2016-10-23 11:29:54 +03:00
|
|
|
});
|
|
|
|
}
|
2016-10-22 19:52:40 +03:00
|
|
|
}
|