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\EventListener; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\EventListener\Request\RequestMeta; |
12
|
|
|
use KleijnWeb\SwaggerBundle\EventListener\Request\RequestProcessor; |
13
|
|
|
use KleijnWeb\SwaggerBundle\EventListener\RequestListener; |
14
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author John Kleijn <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class RequestListenerTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
const DOCUMENT_PATH = '/hi.yaml'; |
24
|
|
|
const SWAGGER_PATH = '/a/b/{hello}'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
28
|
|
|
*/ |
29
|
|
|
private $processorMock; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var RequestListener |
33
|
|
|
*/ |
34
|
|
|
private $listener; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
38
|
|
|
*/ |
39
|
|
|
private $eventMock; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var GetResponseForExceptionEvent |
43
|
|
|
*/ |
44
|
|
|
private $event; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create mocks |
48
|
|
|
*/ |
49
|
|
|
protected function setUp() |
50
|
|
|
{ |
51
|
|
|
$this->eventMock = $this->event = $this |
52
|
|
|
->getMockBuilder(GetResponseForExceptionEvent::class) |
53
|
|
|
->disableOriginalConstructor() |
54
|
|
|
->getMock(); |
55
|
|
|
|
56
|
|
|
/** @var RequestProcessor $processor */ |
57
|
|
|
$this->processorMock = $processor = $this |
58
|
|
|
->getMockBuilder(RequestProcessor::class) |
59
|
|
|
->disableOriginalConstructor() |
60
|
|
|
->getMock(); |
61
|
|
|
|
62
|
|
|
$this->listener = new RequestListener($processor); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
*/ |
68
|
|
|
public function willNotHandleIfNotMasterRequest() |
69
|
|
|
{ |
70
|
|
|
$this->eventMock |
71
|
|
|
->expects($this->once()) |
72
|
|
|
->method('isMasterRequest') |
73
|
|
|
->willReturn(false); |
74
|
|
|
|
75
|
|
|
$this->processorMock |
76
|
|
|
->expects($this->never()) |
77
|
|
|
->method('process'); |
78
|
|
|
|
79
|
|
|
$this->listener->onKernelRequest($this->event); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @test |
84
|
|
|
*/ |
85
|
|
|
public function willNotHandleIfNoDocumentUriInAttributes() |
86
|
|
|
{ |
87
|
|
|
$this->eventMock |
88
|
|
|
->expects($this->once()) |
89
|
|
|
->method('isMasterRequest') |
90
|
|
|
->willReturn(true); |
91
|
|
|
|
92
|
|
|
$this->eventMock |
93
|
|
|
->expects($this->once()) |
94
|
|
|
->method('getRequest') |
95
|
|
|
->willReturn( |
96
|
|
|
new Request() |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$this->processorMock |
100
|
|
|
->expects($this->never()) |
101
|
|
|
->method('process'); |
102
|
|
|
|
103
|
|
|
$this->listener->onKernelRequest($this->event); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @test |
108
|
|
|
*/ |
109
|
|
|
public function willInvokeProcessor() |
110
|
|
|
{ |
111
|
|
|
$this->eventMock |
112
|
|
|
->expects($this->once()) |
113
|
|
|
->method('isMasterRequest') |
114
|
|
|
->willReturn(true); |
115
|
|
|
|
116
|
|
|
$request = new class extends Request |
117
|
|
|
{ |
118
|
|
|
/** |
119
|
|
|
*/ |
120
|
|
|
function __construct() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
parent::__construct(); |
123
|
|
|
$this->attributes = new ParameterBag([RequestMeta::ATTRIBUTE_URI => '/uri']); |
124
|
|
|
} |
125
|
|
|
}; |
126
|
|
|
|
127
|
|
|
$this->eventMock |
128
|
|
|
->expects($this->once()) |
129
|
|
|
->method('getRequest') |
130
|
|
|
->willReturn($request); |
131
|
|
|
|
132
|
|
|
$this->processorMock |
133
|
|
|
->expects($this->once()) |
134
|
|
|
->method('process'); |
135
|
|
|
|
136
|
|
|
$this->listener->onKernelRequest($this->event); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.