1
|
|
|
<?php |
2
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware; |
3
|
|
|
|
4
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
5
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
6
|
|
|
use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware; |
7
|
|
|
use Zend\Diactoros\Response; |
8
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
9
|
|
|
use Zend\Diactoros\Uri; |
10
|
|
|
|
11
|
|
|
class PathVersionMiddlewareTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var PathVersionMiddleware |
15
|
|
|
*/ |
16
|
|
|
protected $middleware; |
17
|
|
|
|
18
|
|
|
public function setUp() |
19
|
|
|
{ |
20
|
|
|
$this->middleware = new PathVersionMiddleware(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @test |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
public function whenVersionIsProvidedRequestRemainsUnchanged() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/v2/foo')); |
29
|
|
|
$test = $this; |
30
|
|
|
$this->middleware->__invoke($request, new Response(), function ($req) use ($request, $test) { |
31
|
|
|
$test->assertSame($request, $req); |
32
|
|
|
}); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @test |
37
|
|
|
*/ |
38
|
|
|
public function versionOneIsPrependedWhenNoVersionIsDefined() |
39
|
|
|
{ |
40
|
|
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/bar/baz')); |
41
|
|
|
$test = $this; |
42
|
|
|
$this->middleware->__invoke($request, new Response(), function (Request $req) use ($request, $test) { |
43
|
|
|
$test->assertNotSame($request, $req); |
44
|
|
|
$this->assertEquals('/rest/v1/bar/baz', $req->getUri()->getPath()); |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @test |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function nonRestPathsAreNotProcessed() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/non-rest')); |
54
|
|
|
$test = $this; |
55
|
|
|
$this->middleware->__invoke($request, new Response(), function ($req) use ($request, $test) { |
56
|
|
|
$test->assertSame($request, $req); |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.