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\Security; |
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 $repositoryStub; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
33
|
|
|
*/ |
34
|
|
|
private $decriptionStub; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var RequestMatcher |
38
|
|
|
*/ |
39
|
|
|
private $matcher; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create mocks |
43
|
|
|
*/ |
44
|
|
|
protected function setUp() |
45
|
|
|
{ |
46
|
|
|
$this->decriptionStub = $this |
47
|
|
|
->getMockBuilder(Description::class) |
48
|
|
|
->disableOriginalConstructor() |
49
|
|
|
->getMock(); |
50
|
|
|
|
51
|
|
|
/** @var Repository $repository */ |
52
|
|
|
$this->repositoryStub = $repository = $this |
53
|
|
|
->getMockBuilder(Repository::class) |
54
|
|
|
->disableOriginalConstructor() |
55
|
|
|
->getMock(); |
56
|
|
|
|
57
|
|
|
$this->repositoryStub |
58
|
|
|
->expects($this->any()) |
59
|
|
|
->method('get') |
60
|
|
|
->willReturn($this->decriptionStub); |
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
|
|
|
* @test |
91
|
|
|
*/ |
92
|
|
|
public function settingMatchUnsecuredToTrueWillReturnTrueEvenIfMatchHasNoSecurityInfo() |
93
|
|
|
{ |
94
|
|
|
$this->matcher->setMatchUnsecured(); |
95
|
|
|
$this->assertTrue($this->matcher->matches($this->createRequest(false))); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param bool $securedOperation |
100
|
|
|
* @return Request |
101
|
|
|
*/ |
102
|
|
|
private function createRequest(bool $securedOperation): Request |
103
|
|
|
{ |
104
|
|
|
$this->repositoryStub |
105
|
|
|
->expects($this->any()) |
106
|
|
|
->method('get') |
107
|
|
|
->willReturn($this->decriptionStub); |
108
|
|
|
|
109
|
|
|
$pathStub = $this |
110
|
|
|
->getMockBuilder(Path::class) |
111
|
|
|
->disableOriginalConstructor() |
112
|
|
|
->getMock(); |
113
|
|
|
|
114
|
|
|
$this->decriptionStub |
115
|
|
|
->expects($this->any()) |
116
|
|
|
->method('getPath') |
117
|
|
|
->willReturn($pathStub); |
118
|
|
|
|
119
|
|
|
$pathStub |
120
|
|
|
->expects($this->any()) |
121
|
|
|
->method('getOperation') |
122
|
|
|
->willReturn( |
123
|
|
|
new Operation('', '', '', [], null, [], [], $securedOperation) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$attributes = [RequestMeta::ATTRIBUTE_URI => 'http://acme.com', RequestMeta::ATTRIBUTE_PATH => '/foo']; |
127
|
|
|
|
128
|
|
|
return new Request([], [], $attributes); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|