|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\Assert; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Prophecy\Argument; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
11
|
|
|
use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware; |
|
12
|
|
|
use Zend\Diactoros\Response; |
|
13
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
|
14
|
|
|
use Zend\Diactoros\Uri; |
|
15
|
|
|
|
|
16
|
|
|
class PathVersionMiddlewareTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var PathVersionMiddleware |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $middleware; |
|
22
|
|
|
|
|
23
|
|
|
public function setUp() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->middleware = new PathVersionMiddleware(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @test |
|
30
|
|
|
*/ |
|
31
|
|
View Code Duplication |
public function whenVersionIsProvidedRequestRemainsUnchanged() |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/v2/foo')); |
|
34
|
|
|
|
|
35
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class); |
|
36
|
|
|
$process = $delegate->handle($request)->willReturn(new Response()); |
|
37
|
|
|
|
|
38
|
|
|
$this->middleware->process($request, $delegate->reveal()); |
|
39
|
|
|
|
|
40
|
|
|
$process->shouldHaveBeenCalled(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @test |
|
45
|
|
|
*/ |
|
46
|
|
|
public function versionOneIsPrependedWhenNoVersionIsDefined() |
|
47
|
|
|
{ |
|
48
|
|
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/bar/baz')); |
|
49
|
|
|
|
|
50
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class); |
|
51
|
|
|
$delegate->handle(Argument::type(Request::class))->will(function (array $args) use ($request) { |
|
52
|
|
|
$req = \array_shift($args); |
|
53
|
|
|
|
|
54
|
|
|
Assert::assertNotSame($request, $req); |
|
55
|
|
|
Assert::assertEquals('/v1/bar/baz', $req->getUri()->getPath()); |
|
56
|
|
|
return new Response(); |
|
57
|
|
|
}); |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$this->middleware->process($request, $delegate->reveal()); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
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.