Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
29 | class SecurityControllerTest extends TestCase |
||
30 | { |
||
31 | public static $functions; |
||
32 | |||
33 | public function setUp() |
||
34 | { |
||
35 | parent::setUp(); |
||
36 | self::$functions = Mockery::mock(); |
||
37 | } |
||
38 | |||
39 | public function testShowLoginWithValidServiceUrl() |
||
40 | { |
||
41 | $serviceRepository = Mockery::mock(ServiceRepository::class) |
||
42 | ->shouldReceive('isUrlValid') |
||
43 | ->andReturn(true) |
||
44 | ->once() |
||
45 | ->getMock(); |
||
46 | app()->instance(ServiceRepository::class, $serviceRepository); |
||
47 | $loginInteraction = Mockery::mock(UserLogin::class) |
||
48 | ->shouldReceive('showLoginPage') |
||
49 | ->andReturnUsing( |
||
50 | function ($request, $errors) { |
||
51 | $this->assertEmpty($errors); |
||
52 | |||
53 | return 'show login called'; |
||
54 | } |
||
55 | ) |
||
56 | ->once() |
||
57 | ->shouldReceive('getCurrentUser') |
||
58 | ->andReturn(false) |
||
59 | ->once() |
||
60 | ->getMock(); |
||
61 | app()->instance(UserLogin::class, $loginInteraction); |
||
62 | $request = Mockery::mock(Request::class) |
||
63 | ->shouldReceive('get') |
||
64 | ->withArgs(['service', '']) |
||
65 | ->andReturn('what ever') |
||
66 | ->once() |
||
67 | ->getMock(); |
||
68 | $this->assertEquals('show login called', app()->make(SecurityController::class)->showLogin($request)); |
||
69 | |||
70 | } |
||
71 | |||
72 | public function testShowLoginWithInvalidServiceUrl() |
||
73 | { |
||
74 | $serviceRepository = Mockery::mock(ServiceRepository::class) |
||
75 | ->shouldReceive('isUrlValid') |
||
76 | ->andReturn(false) |
||
77 | ->once() |
||
78 | ->getMock(); |
||
79 | app()->instance(ServiceRepository::class, $serviceRepository); |
||
80 | $loginInteraction = Mockery::mock(UserLogin::class) |
||
81 | ->shouldReceive('showLoginPage') |
||
82 | ->andReturnUsing( |
||
83 | function ($request, $errors) { |
||
84 | $this->assertNotEmpty($errors); |
||
85 | $this->assertEquals(CasException::INVALID_SERVICE, $errors[0]); |
||
86 | |||
87 | return 'show login called'; |
||
88 | } |
||
89 | ) |
||
90 | ->once() |
||
91 | ->shouldReceive('getCurrentUser') |
||
92 | ->andReturn(false) |
||
93 | ->once() |
||
94 | ->getMock(); |
||
95 | app()->instance(UserLogin::class, $loginInteraction); |
||
96 | $request = Mockery::mock(Request::class) |
||
97 | ->shouldReceive('get') |
||
98 | ->withArgs(['service', '']) |
||
99 | ->andReturn('what ever') |
||
100 | ->once() |
||
101 | ->getMock(); |
||
102 | $this->assertEquals('show login called', app()->make(SecurityController::class)->showLogin($request)); |
||
103 | } |
||
104 | |||
105 | public function testShowLoginWhenLoggedInWithValidServiceUrlWithoutWarn() |
||
106 | { |
||
107 | //logged in with valid service url without warn parameter |
||
108 | $serviceRepository = Mockery::mock(ServiceRepository::class) |
||
109 | ->shouldReceive('isUrlValid') |
||
110 | ->andReturn(true) |
||
111 | ->once() |
||
112 | ->getMock(); |
||
113 | $ticketRepository = Mockery::mock(TicketRepository::class); |
||
114 | $user = new User(); |
||
115 | $loginInteraction = Mockery::mock(UserLogin::class) |
||
116 | ->shouldReceive('getCurrentUser') |
||
117 | ->andReturn($user) |
||
118 | ->once() |
||
119 | ->getMock(); |
||
120 | $request = Mockery::mock(Request::class) |
||
121 | ->shouldReceive('get') |
||
122 | ->withArgs(['service', '']) |
||
123 | ->andReturn('what ever') |
||
124 | ->once() |
||
125 | ->shouldReceive('get') |
||
126 | ->withArgs(['warn']) |
||
127 | ->andReturn(false) |
||
128 | ->once() |
||
129 | ->getMock(); |
||
130 | $controller = Mockery::mock( |
||
131 | SecurityController::class, |
||
132 | [$serviceRepository, $ticketRepository, $loginInteraction] |
||
133 | ) |
||
134 | ->makePartial() |
||
135 | ->shouldReceive('authenticated') |
||
136 | ->withArgs([$request, $user]) |
||
137 | ->andReturn('authenticated called') |
||
138 | ->once() |
||
139 | ->getMock(); |
||
140 | $this->assertEquals('authenticated called', $controller->showLogin($request)); |
||
141 | } |
||
142 | |||
143 | public function testShowLoginWhenLoggedInWithValidServiceUrlWithWarn() |
||
144 | { |
||
145 | //logged in with valid service url with warn parameter |
||
146 | $serviceRepository = Mockery::mock(ServiceRepository::class) |
||
147 | ->shouldReceive('isUrlValid') |
||
148 | ->andReturn(true) |
||
149 | ->once() |
||
150 | ->getMock(); |
||
151 | app()->instance(ServiceRepository::class, $serviceRepository); |
||
152 | $ticketRepository = Mockery::mock(TicketRepository::class); |
||
153 | app()->instance(TicketRepository::class, $ticketRepository); |
||
154 | $loginInteraction = Mockery::mock(UserLogin::class) |
||
155 | ->shouldReceive('getCurrentUser') |
||
156 | ->andReturn(true)//just not false is OK |
||
157 | ->once() |
||
158 | ->shouldReceive('showLoginWarnPage') |
||
159 | ->andReturn('showLoginWarnPage called') |
||
160 | ->once() |
||
161 | ->getMock(); |
||
162 | app()->instance(UserLogin::class, $loginInteraction); |
||
163 | $request = Mockery::mock(Request::class) |
||
164 | ->shouldReceive('get') |
||
165 | ->withArgs(['service', '']) |
||
166 | ->andReturn('what ever') |
||
167 | ->once() |
||
168 | ->shouldReceive('get') |
||
169 | ->withArgs(['warn']) |
||
170 | ->andReturn('true') |
||
171 | ->once() |
||
172 | ->getMock(); |
||
173 | $request->query = Mockery::mock() |
||
174 | ->shouldReceive('all') |
||
175 | ->andReturn([]) |
||
176 | ->once() |
||
177 | ->getMock(); |
||
178 | self::$functions->shouldReceive('cas_route')->andReturn('some string')->once(); |
||
179 | $controller = app()->make(SecurityController::class); |
||
180 | $this->assertEquals('showLoginWarnPage called', $controller->showLogin($request)); |
||
181 | } |
||
182 | |||
183 | public function testShowLoginWhenLoggedInWithInvalidServiceUrl() |
||
184 | { |
||
185 | //logged in with invalid service url |
||
186 | $serviceRepository = Mockery::mock(ServiceRepository::class) |
||
187 | ->shouldReceive('isUrlValid') |
||
188 | ->andReturn(false) |
||
189 | ->once() |
||
190 | ->getMock(); |
||
191 | app()->instance(ServiceRepository::class, $serviceRepository); |
||
192 | $ticketRepository = Mockery::mock(TicketRepository::class); |
||
193 | app()->instance(TicketRepository::class, $ticketRepository); |
||
194 | $loginInteraction = Mockery::mock(UserLogin::class) |
||
195 | ->shouldReceive('getCurrentUser') |
||
196 | ->andReturn(true)//just not false is OK |
||
197 | ->once() |
||
198 | ->shouldReceive('redirectToHome') |
||
199 | ->andReturnUsing( |
||
200 | function ($errors) { |
||
201 | $this->assertNotEmpty($errors); |
||
202 | $this->assertEquals(CasException::INVALID_SERVICE, $errors[0]); |
||
203 | |||
204 | return 'redirectToHome called'; |
||
205 | } |
||
206 | ) |
||
207 | ->once() |
||
208 | ->getMock(); |
||
209 | app()->instance(UserLogin::class, $loginInteraction); |
||
210 | $request = Mockery::mock(Request::class) |
||
211 | ->shouldReceive('get') |
||
212 | ->withArgs(['service', '']) |
||
213 | ->andReturn('what ever') |
||
214 | ->once() |
||
215 | ->getMock(); |
||
216 | $controller = app()->make(SecurityController::class); |
||
217 | $this->assertEquals('redirectToHome called', $controller->showLogin($request)); |
||
218 | } |
||
219 | |||
220 | public function testAuthenticatedWithoutService() |
||
221 | { |
||
222 | //without service url |
||
223 | $user = new User(); |
||
224 | $loginInteraction = Mockery::mock(UserLogin::class) |
||
225 | ->shouldReceive('redirectToHome') |
||
226 | ->andReturnUsing( |
||
227 | function () { |
||
228 | return 'redirectToHome called'; |
||
229 | } |
||
230 | ) |
||
231 | ->once() |
||
232 | ->getMock(); |
||
233 | app()->instance(UserLogin::class, $loginInteraction); |
||
234 | $request = Mockery::mock(Request::class) |
||
235 | ->shouldReceive('get') |
||
236 | ->withArgs(['service', '']) |
||
237 | ->andReturn('') |
||
238 | ->once() |
||
239 | ->getMock(); |
||
240 | $this->expectsEvents(CasUserLoginEvent::class); |
||
241 | $this->assertEquals( |
||
242 | 'redirectToHome called', |
||
243 | app()->make(SecurityController::class)->authenticated($request, $user) |
||
244 | ); |
||
245 | } |
||
246 | |||
247 | public function testAuthenticatedWithService() |
||
248 | { |
||
249 | //with service url but apply ticket failed |
||
250 | $user = new User(); |
||
251 | $loginInteraction = Mockery::mock(UserLogin::class) |
||
252 | ->shouldReceive('redirectToHome') |
||
253 | ->andReturnUsing( |
||
254 | function ($errors) { |
||
255 | $this->assertNotEmpty($errors); |
||
256 | $this->assertEquals(CasException::INTERNAL_ERROR, $errors[0]); |
||
257 | |||
258 | return 'redirectToHome called'; |
||
259 | } |
||
260 | ) |
||
261 | ->once() |
||
262 | ->getMock(); |
||
263 | app()->instance(UserLogin::class, $loginInteraction); |
||
264 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
265 | ->shouldReceive('applyTicket') |
||
266 | ->andThrow(new CasException(CasException::INTERNAL_ERROR)) |
||
267 | ->once() |
||
268 | ->getMock(); |
||
269 | app()->instance(TicketRepository::class, $ticketRepository); |
||
270 | $request = Mockery::mock(Request::class) |
||
271 | ->shouldReceive('get') |
||
272 | ->withArgs(['service', '']) |
||
273 | ->andReturn('http://leo108.com') |
||
274 | ->once() |
||
275 | ->getMock(); |
||
276 | $this->expectsEvents(CasUserLoginEvent::class); |
||
277 | $this->assertEquals( |
||
278 | 'redirectToHome called', |
||
279 | app()->make(SecurityController::class)->authenticated($request, $user) |
||
280 | ); |
||
281 | |||
282 | //with service url |
||
283 | $ticket = Mockery::mock(); |
||
284 | $ticket->ticket = 'ST-abc'; |
||
285 | $ticketRepository = Mockery::mock(TicketRepository::class) |
||
286 | ->shouldReceive('applyTicket') |
||
287 | ->andReturn($ticket) |
||
288 | ->once() |
||
289 | ->getMock(); |
||
290 | app()->instance(TicketRepository::class, $ticketRepository); |
||
291 | $request = Mockery::mock(Request::class) |
||
292 | ->shouldReceive('get') |
||
293 | ->withArgs(['service', '']) |
||
294 | ->andReturn('http://leo108.com') |
||
295 | ->once() |
||
296 | ->getMock(); |
||
297 | $this->expectsEvents(CasUserLoginEvent::class); |
||
298 | $resp = app()->make(SecurityController::class)->authenticated($request, $user); |
||
299 | $this->assertInstanceOf(RedirectResponse::class, $resp); |
||
300 | $this->assertEquals($resp->getTargetUrl(), 'http://leo108.com?ticket=ST-abc'); |
||
301 | } |
||
302 | |||
303 | public function testLogoutWhenNotLoggedInWithoutService() |
||
304 | { |
||
305 | $loginInteraction = Mockery::mock(UserLogin::class) |
||
306 | ->shouldReceive('getCurrentUser') |
||
307 | ->andReturn(false) |
||
308 | ->once() |
||
309 | ->shouldReceive('showLoggedOut') |
||
310 | ->andReturnUsing( |
||
311 | function ($request) { |
||
312 | return 'showLoggedOut called'; |
||
313 | } |
||
314 | ) |
||
315 | ->once() |
||
316 | ->getMock(); |
||
317 | app()->instance(UserLogin::class, $loginInteraction); |
||
318 | $request = Mockery::mock(Request::class) |
||
319 | ->shouldReceive('get') |
||
320 | ->withArgs(['service']) |
||
321 | ->andReturn(null) |
||
322 | ->once() |
||
323 | ->getMock(); |
||
324 | $this->doesntExpectEvents(CasUserLogoutEvent::class); |
||
325 | $this->assertEquals('showLoggedOut called', app()->make(SecurityController::class)->logout($request)); |
||
326 | } |
||
327 | |||
328 | public function testLogoutWithoutService() |
||
329 | { |
||
330 | $loginInteraction = Mockery::mock(UserLogin::class) |
||
331 | ->shouldReceive('logout') |
||
332 | ->once() |
||
333 | ->shouldReceive('getCurrentUser') |
||
334 | ->andReturn(new User()) |
||
335 | ->once() |
||
336 | ->shouldReceive('showLoggedOut') |
||
337 | ->andReturnUsing( |
||
338 | function ($request) { |
||
339 | return 'showLoggedOut called'; |
||
340 | } |
||
341 | ) |
||
342 | ->once() |
||
343 | ->getMock(); |
||
344 | app()->instance(UserLogin::class, $loginInteraction); |
||
345 | $request = Mockery::mock(Request::class) |
||
346 | ->shouldReceive('get') |
||
347 | ->withArgs(['service']) |
||
348 | ->andReturn(null) |
||
349 | ->once() |
||
350 | ->getMock(); |
||
351 | $this->expectsEvents(CasUserLogoutEvent::class); |
||
352 | $this->assertEquals('showLoggedOut called', app()->make(SecurityController::class)->logout($request)); |
||
353 | } |
||
354 | |||
355 | public function testLogoutWithValidService() |
||
356 | { |
||
357 | $serviceRepository = Mockery::mock(ServiceRepository::class) |
||
358 | ->shouldReceive('isUrlValid') |
||
359 | ->andReturn(true) |
||
360 | ->once() |
||
361 | ->getMock(); |
||
362 | app()->instance(ServiceRepository::class, $serviceRepository); |
||
363 | $loginInteraction = Mockery::mock(UserLogin::class) |
||
364 | ->shouldReceive('logout') |
||
365 | ->once() |
||
366 | ->shouldReceive('getCurrentUser') |
||
367 | ->andReturn(new User()) |
||
368 | ->once() |
||
369 | ->getMock(); |
||
370 | app()->instance(UserLogin::class, $loginInteraction); |
||
371 | $request = Mockery::mock(Request::class) |
||
372 | ->shouldReceive('get') |
||
373 | ->withArgs(['service']) |
||
374 | ->andReturn('http://leo108.com') |
||
375 | ->once() |
||
376 | ->getMock(); |
||
377 | $this->expectsEvents(CasUserLogoutEvent::class); |
||
378 | $resp = app()->make(SecurityController::class)->logout($request); |
||
379 | $this->assertInstanceOf(RedirectResponse::class, $resp); |
||
380 | } |
||
381 | |||
382 | public function testLogin() |
||
383 | { |
||
384 | $user = new User(); |
||
385 | $loginInteraction = Mockery::mock(UserLogin::class) |
||
386 | ->shouldReceive('login') |
||
387 | ->andReturn($user) |
||
388 | ->once() |
||
389 | ->getMock(); |
||
390 | app()->instance(UserLogin::class, $loginInteraction); |
||
391 | $request = Mockery::mock(Request::class); |
||
392 | $serviceRepository = Mockery::mock(ServiceRepository::class); |
||
393 | $ticketRepository = Mockery::mock(TicketRepository::class); |
||
394 | $controller = Mockery::mock( |
||
395 | SecurityController::class, |
||
396 | [$serviceRepository, $ticketRepository, $loginInteraction] |
||
397 | ) |
||
398 | ->makePartial() |
||
399 | ->shouldReceive('authenticated') |
||
400 | ->withArgs([$request, $user]) |
||
401 | ->andReturn('authenticated called') |
||
402 | ->once() |
||
403 | ->getMock(); |
||
404 | $this->assertEquals('authenticated called', $controller->login($request)); |
||
405 | } |
||
406 | } |
||
407 |