Code Duplication    Length = 30-31 lines in 3 locations

Tests/EventListener/GeoBlockingKernelRequestListenerTest.php 3 locations

@@ 294-324 (lines=31) @@
291
292
    }
293
294
    public function testOnKernelRequestGeoBlocking_RouteBlocking_AllowWithBlackList()
295
    {
296
        $parameters = $this->getDefaultParams();
297
        $parameters["routeWhitelist"] = array();
298
        $parameters["routeBlacklist"] = array("notAllowedRoute");
299
300
        $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock();
301
        $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock();
302
303
        $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock();
304
        $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock();
305
306
        $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock();
307
        $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock();
308
        $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock();
309
310
        $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null));
311
        $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock));
312
        $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock));
313
314
        $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST));
315
        $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock));
316
        $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->chIP));
317
        $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("someOtherRoute"));
318
        $lookUpMock->expects($this->once())->method("getCountry")->with($this->chIP)->will($this->returnValue("CH"));
319
        $eventBlockMock->expects($this->never())->method("setResponse");
320
        $eventBlockMock->expects($this->never())->method("stopPropagation");
321
322
        $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters);
323
        $geoBlockingListener->onKernelRequest($eventBlockMock);
324
   }
325
326
    public function testOnKernelRequestGeoBlocking_CountryBlocking_AllowWithWhiteList()
327
    {
@@ 326-355 (lines=30) @@
323
        $geoBlockingListener->onKernelRequest($eventBlockMock);
324
   }
325
326
    public function testOnKernelRequestGeoBlocking_CountryBlocking_AllowWithWhiteList()
327
    {
328
        $parameters = $this->getDefaultParams();
329
        $parameters["routeWhitelist"] = array();
330
331
        $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock();
332
        $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock();
333
334
        $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock();
335
        $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock();
336
337
        $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock();
338
        $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock();
339
        $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock();
340
341
        $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null));
342
        $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock));
343
        $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock));
344
345
        $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST));
346
        $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock));
347
        $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->chIP));
348
        $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("noWhiteListRoute"));
349
        $lookUpMock->expects($this->once())->method("getCountry")->with($this->chIP)->will($this->returnValue("CH"));
350
        $eventBlockMock->expects($this->never())->method("setResponse");
351
        $eventBlockMock->expects($this->never())->method("stopPropagation");
352
353
        $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters);
354
        $geoBlockingListener->onKernelRequest($eventBlockMock);
355
    }
356
357
    public function testOnKernelRequestGeoBlocking_CountryBlocking_DenyWithWhiteList()
358
    {
@@ 455-484 (lines=30) @@
452
        $geoBlockingListener->onKernelRequest($eventBlockMock);
453
    }
454
455
    public function testOnKernelRequestGeoBlocking_IP_WhiteList_allow_regexp()
456
    {
457
        $parameters = $this->getDefaultParams();
458
        $parameters['ip_whitelist'] = array("/66\.249\.78\.\d{1,3}/");
459
        $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock();
460
        $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock();
461
462
           $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock();
463
        $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock();
464
465
        $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock();
466
        $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock();
467
        $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock();
468
469
        $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null));
470
        $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock));
471
        $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock));
472
473
        $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST));
474
        $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock));
475
        $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->googleBotIP));
476
        $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("random_route"));
477
        $lookUpMock->expects($this->once())->method("getCountry")->with($this->googleBotIP)->will($this->returnValue("US"));
478
        $eventBlockMock->expects($this->never())->method("setResponse");
479
        $eventBlockMock->expects($this->never())->method("stopPropagation");
480
481
        $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters);
482
        $geoBlockingListener->onKernelRequest($eventBlockMock);
483
484
    }
485
486
    public function testOnKernelRequestGeoBlocking_IP_WhiteList_allow_ip()
487
    {