1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Tests\Routing; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Description; |
12
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Operation; |
13
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Path; |
14
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Repository; |
15
|
|
|
use KleijnWeb\SwaggerBundle\EventListener\Request\RequestMeta; |
16
|
|
|
use KleijnWeb\SwaggerBundle\Security\RequestMatcher; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author John Kleijn <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class RequestMatcherTest extends \PHPUnit_Framework_TestCase |
23
|
|
|
{ |
24
|
|
|
const DOCUMENT_PATH = '/totally/non-existent/path'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
28
|
|
|
*/ |
29
|
|
|
private $repositoryMock; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
33
|
|
|
*/ |
34
|
|
|
private $decriptionMock; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var RequestMatcher |
38
|
|
|
*/ |
39
|
|
|
private $matcher; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create mocks |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$this->decriptionMock = $this |
47
|
|
|
->getMockBuilder(Description::class) |
48
|
|
|
->disableOriginalConstructor() |
49
|
|
|
->getMock(); |
50
|
|
|
|
51
|
|
|
/** @var Repository $repository */ |
52
|
|
|
$this->repositoryMock = $repository = $this |
53
|
|
|
->getMockBuilder(Repository::class) |
54
|
|
|
->disableOriginalConstructor() |
55
|
|
|
->getMock(); |
56
|
|
|
|
57
|
|
|
$this->repositoryMock |
58
|
|
|
->expects($this->any()) |
59
|
|
|
->method('get') |
60
|
|
|
->willReturn($this->decriptionMock); |
61
|
|
|
|
62
|
|
|
$this->matcher = new RequestMatcher($repository); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
*/ |
68
|
|
|
public function willReturnFalseIfNoDocumentUriInAttributes() |
69
|
|
|
{ |
70
|
|
|
$this->assertFalse($this->matcher->matches(new Request())); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @test |
75
|
|
|
*/ |
76
|
|
|
public function willReturnFalseWhenOperationNotSecured() |
77
|
|
|
{ |
78
|
|
|
$this->assertFalse($this->matcher->matches($this->createRequest(false))); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @test |
83
|
|
|
*/ |
84
|
|
|
public function willReturnTrueWhenOperationNotSecured() |
85
|
|
|
{ |
86
|
|
|
$this->assertTrue($this->matcher->matches($this->createRequest(true))); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param bool $securedOperation |
91
|
|
|
* @return Request |
92
|
|
|
*/ |
93
|
|
|
private function createRequest(bool $securedOperation): Request |
94
|
|
|
{ |
95
|
|
|
$this->repositoryMock |
96
|
|
|
->expects($this->once()) |
97
|
|
|
->method('get') |
98
|
|
|
->willReturn($this->decriptionMock); |
99
|
|
|
|
100
|
|
|
$pathMock = $this |
101
|
|
|
->getMockBuilder(Path::class) |
102
|
|
|
->disableOriginalConstructor() |
103
|
|
|
->getMock(); |
104
|
|
|
|
105
|
|
|
$this->decriptionMock |
106
|
|
|
->expects($this->once()) |
107
|
|
|
->method('getPath') |
108
|
|
|
->willReturn($pathMock); |
109
|
|
|
|
110
|
|
|
$pathMock |
111
|
|
|
->expects($this->once()) |
112
|
|
|
->method('getOperation') |
113
|
|
|
->willReturn( |
114
|
|
|
new Operation('', '', '', [], null, [], [], $securedOperation) |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$attributes = [RequestMeta::ATTRIBUTE_URI => 'http://acme.com', RequestMeta::ATTRIBUTE_PATH => '/foo']; |
118
|
|
|
|
119
|
|
|
return new Request([], [], $attributes); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.