|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Tests\AdminBundle\Action\Responder; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Action\Configuration\ActionConfiguration; |
|
6
|
|
|
use LAG\AdminBundle\Action\Responder\CreateResponder; |
|
7
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
8
|
|
|
use LAG\AdminBundle\Admin\Configuration\AdminConfiguration; |
|
9
|
|
|
use LAG\AdminBundle\Tests\AdminTestBase; |
|
10
|
|
|
use Symfony\Component\Form\FormInterface; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
14
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
15
|
|
|
use Test\TestBundle\Entity\TestEntity; |
|
16
|
|
|
use Twig_Environment; |
|
17
|
|
|
|
|
18
|
|
|
class CreateResponderTest extends AdminTestBase |
|
19
|
|
|
{ |
|
20
|
|
View Code Duplication |
public function testRespondWithSave() |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
$routing = $this->getMockWithoutConstructor(RouterInterface::class); |
|
23
|
|
|
$routing |
|
24
|
|
|
->expects($this->atLeastOnce()) |
|
25
|
|
|
->method('generate') |
|
26
|
|
|
->with('my_little_admin.edit') |
|
27
|
|
|
->willReturn('http://test.fr') |
|
28
|
|
|
; |
|
29
|
|
|
|
|
30
|
|
|
$configuration = $this->getMockWithoutConstructor(ActionConfiguration::class); |
|
31
|
|
|
$configuration |
|
32
|
|
|
->expects($this->atLeastOnce()) |
|
33
|
|
|
->method('getParameter') |
|
34
|
|
|
->willReturnMap([ |
|
35
|
|
|
['template', 'my_template.twig'], |
|
36
|
|
|
]) |
|
37
|
|
|
; |
|
38
|
|
|
|
|
39
|
|
|
$adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
|
40
|
|
|
$adminConfiguration |
|
41
|
|
|
->expects($this->once()) |
|
42
|
|
|
->method('isResolved') |
|
43
|
|
|
->willReturn(true) |
|
44
|
|
|
; |
|
45
|
|
|
$adminConfiguration |
|
46
|
|
|
->expects($this->atLeastOnce()) |
|
47
|
|
|
->method('getParameter') |
|
48
|
|
|
->willReturnMap([ |
|
49
|
|
|
['actions', ['edit' => [], 'delete' => []]], |
|
50
|
|
|
['routing_name_pattern', '{admin}.{action}'], |
|
51
|
|
|
]) |
|
52
|
|
|
; |
|
53
|
|
|
|
|
54
|
|
|
$admin = $this->getMockWithoutConstructor(AdminInterface::class); |
|
55
|
|
|
$admin |
|
56
|
|
|
->expects($this->atLeastOnce()) |
|
57
|
|
|
->method('getUniqueEntity') |
|
58
|
|
|
->willReturn(new TestEntity()) |
|
59
|
|
|
; |
|
60
|
|
|
$admin |
|
61
|
|
|
->expects($this->once()) |
|
62
|
|
|
->method('getConfiguration') |
|
63
|
|
|
->willReturn($adminConfiguration) |
|
64
|
|
|
; |
|
65
|
|
|
$admin |
|
66
|
|
|
->expects($this->atLeastOnce()) |
|
67
|
|
|
->method('getName') |
|
68
|
|
|
->willReturn('my_little_admin') |
|
69
|
|
|
; |
|
70
|
|
|
|
|
71
|
|
|
$form = $this->getMockWithoutConstructor(FormInterface::class); |
|
72
|
|
|
$form |
|
73
|
|
|
->expects($this->atLeastOnce()) |
|
74
|
|
|
->method('isValid') |
|
75
|
|
|
->willReturn(true) |
|
76
|
|
|
; |
|
77
|
|
|
$form |
|
78
|
|
|
->expects($this->atLeastOnce()) |
|
79
|
|
|
->method('isSubmitted') |
|
80
|
|
|
->willReturn(true) |
|
81
|
|
|
; |
|
82
|
|
|
|
|
83
|
|
|
$twig = $this->getMockWithoutConstructor(Twig_Environment::class); |
|
84
|
|
|
|
|
85
|
|
|
$responder = new CreateResponder($routing, $twig); |
|
86
|
|
|
|
|
87
|
|
|
$response = $responder->respond( |
|
88
|
|
|
$configuration, |
|
89
|
|
|
$admin, |
|
90
|
|
|
$form, |
|
91
|
|
|
'save' |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testRespondWithSubmit() |
|
98
|
|
|
{ |
|
99
|
|
|
$routing = $this->getMockWithoutConstructor(RouterInterface::class); |
|
100
|
|
|
$routing |
|
101
|
|
|
->expects($this->atLeastOnce()) |
|
102
|
|
|
->method('generate') |
|
103
|
|
|
->willReturn('http://test.fr') |
|
104
|
|
|
; |
|
105
|
|
|
|
|
106
|
|
|
$configuration = $this->getMockWithoutConstructor(ActionConfiguration::class); |
|
107
|
|
|
$configuration |
|
108
|
|
|
->expects($this->atLeastOnce()) |
|
109
|
|
|
->method('getParameter') |
|
110
|
|
|
->willReturnMap([ |
|
111
|
|
|
['template', 'my_template.twig'], |
|
112
|
|
|
]) |
|
113
|
|
|
; |
|
114
|
|
|
|
|
115
|
|
|
$adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
|
116
|
|
|
$adminConfiguration |
|
117
|
|
|
->expects($this->once()) |
|
118
|
|
|
->method('isResolved') |
|
119
|
|
|
->willReturn(true) |
|
120
|
|
|
; |
|
121
|
|
|
$adminConfiguration |
|
122
|
|
|
->expects($this->atLeastOnce()) |
|
123
|
|
|
->method('getParameter') |
|
124
|
|
|
->willReturnMap([ |
|
125
|
|
|
['actions', ['edit' => [], 'delete' => [], 'list' => []]], |
|
126
|
|
|
['routing_name_pattern', '{admin}.{action}'], |
|
127
|
|
|
]) |
|
128
|
|
|
; |
|
129
|
|
|
|
|
130
|
|
|
$admin = $this->getMockWithoutConstructor(AdminInterface::class); |
|
131
|
|
|
$admin |
|
132
|
|
|
->expects($this->once()) |
|
133
|
|
|
->method('getConfiguration') |
|
134
|
|
|
->willReturn($adminConfiguration) |
|
135
|
|
|
; |
|
136
|
|
|
$admin |
|
137
|
|
|
->expects($this->atLeastOnce()) |
|
138
|
|
|
->method('getName') |
|
139
|
|
|
->willReturn('my_little_admin') |
|
140
|
|
|
; |
|
141
|
|
|
|
|
142
|
|
|
$form = $this->getMockWithoutConstructor(FormInterface::class); |
|
143
|
|
|
$form |
|
144
|
|
|
->expects($this->atLeastOnce()) |
|
145
|
|
|
->method('isValid') |
|
146
|
|
|
->willReturn(true) |
|
147
|
|
|
; |
|
148
|
|
|
$form |
|
149
|
|
|
->expects($this->atLeastOnce()) |
|
150
|
|
|
->method('isSubmitted') |
|
151
|
|
|
->willReturn(true) |
|
152
|
|
|
; |
|
153
|
|
|
|
|
154
|
|
|
$twig = $this->getMockWithoutConstructor(Twig_Environment::class); |
|
155
|
|
|
|
|
156
|
|
|
$request = new Request([], [ |
|
157
|
|
|
'submit' => 'submit_and_save', |
|
158
|
|
|
]); |
|
159
|
|
|
|
|
160
|
|
|
$responder = new CreateResponder($routing, $twig); |
|
161
|
|
|
|
|
162
|
|
|
$response = $responder->respond( |
|
163
|
|
|
$configuration, |
|
164
|
|
|
$admin, |
|
165
|
|
|
$form, |
|
166
|
|
|
$request |
|
167
|
|
|
); |
|
168
|
|
|
|
|
169
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
View Code Duplication |
public function testRespondWithNotSubmittedForm() |
|
|
|
|
|
|
173
|
|
|
{ |
|
174
|
|
|
$routing = $this->getMockWithoutConstructor(RouterInterface::class); |
|
175
|
|
|
|
|
176
|
|
|
$configuration = $this->getMockWithoutConstructor(ActionConfiguration::class); |
|
177
|
|
|
$configuration |
|
178
|
|
|
->expects($this->atLeastOnce()) |
|
179
|
|
|
->method('getParameter') |
|
180
|
|
|
->willReturnMap([ |
|
181
|
|
|
['template', 'my_template.twig'], |
|
182
|
|
|
]) |
|
183
|
|
|
; |
|
184
|
|
|
|
|
185
|
|
|
$admin = $this->getMockWithoutConstructor(AdminInterface::class); |
|
186
|
|
|
|
|
187
|
|
|
$form = $this->getMockWithoutConstructor(FormInterface::class); |
|
188
|
|
|
$form |
|
189
|
|
|
->expects($this->atLeastOnce()) |
|
190
|
|
|
->method('isSubmitted') |
|
191
|
|
|
->willReturn(false) |
|
192
|
|
|
; |
|
193
|
|
|
|
|
194
|
|
|
$twig = $this->getMockWithoutConstructor(Twig_Environment::class); |
|
195
|
|
|
$twig |
|
196
|
|
|
->expects($this->atLeastOnce()) |
|
197
|
|
|
->method('render') |
|
198
|
|
|
->with('my_template.twig') |
|
199
|
|
|
->willReturn(new Response('content')) |
|
200
|
|
|
; |
|
201
|
|
|
|
|
202
|
|
|
$request = new Request([], [ |
|
203
|
|
|
'submit' => 'save', |
|
204
|
|
|
]); |
|
205
|
|
|
|
|
206
|
|
|
$responder = new CreateResponder($routing, $twig); |
|
207
|
|
|
|
|
208
|
|
|
$response = $responder->respond( |
|
209
|
|
|
$configuration, |
|
210
|
|
|
$admin, |
|
211
|
|
|
$form, |
|
212
|
|
|
$request |
|
213
|
|
|
); |
|
214
|
|
|
|
|
215
|
|
|
$this->assertInstanceOf(Response::class, $response); |
|
216
|
|
|
$this->assertStringEndsWith('content', $response->getContent()); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
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.