Code Duplication    Length = 31-32 lines in 4 locations

Tests/EventListener/GeoBlockingKernelRequestListenerTest.php 4 locations

@@ 486-517 (lines=32) @@
483
484
    }
485
486
    public function testOnKernelRequestGeoBlocking_IP_WhiteList_allow_ip()
487
    {
488
        $parameters = $this->getDefaultParams();
489
        $parameters['ip_whitelist'] = array($this->googleBotIP);
490
        $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock();
491
        $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock();
492
493
        $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock();
494
        $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock();
495
496
        $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock();
497
        $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\SecurityContext")->disableOriginalConstructor()->getMock();
498
        $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock();
499
500
        $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null));
501
        $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock));
502
        $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock));
503
504
        $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST));
505
        $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock));
506
        $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->googleBotIP));
507
        $requestMock->expects($this->once())->method("get")->with("_route", null, false)->will($this->returnValue("random_route"));
508
        $lookUpMock->expects($this->once())->method("getCountry")->with($this->googleBotIP)->will($this->returnValue("US"));
509
        $eventBlockMock->expects($this->never())->method("setResponse");
510
        $eventBlockMock->expects($this->never())->method("stopPropagation");
511
512
        $loggerMock->expects($this->never())->method("warning");
513
514
        $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters);
515
        $geoBlockingListener->onKernelRequest($eventBlockMock);
516
517
    }
518
519
    public function testOnKernelRequestGeoBlocking_IP_WhiteList_deny_ip()
520
    {
@@ 591-621 (lines=31) @@
588
589
    }
590
591
    public function testOnKernelRequestGeoBlocking_Search_Engine_Bot_Allow_Google()
592
    {
593
        $parameters = $this->getDefaultParams();
594
        $parameters['allow_search_bots'] = true;
595
        $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock();
596
        $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock();
597
598
        $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock();
599
        $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock();
600
601
        $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock();
602
        $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\SecurityContext")->disableOriginalConstructor()->getMock();
603
        $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock();
604
605
        $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null));
606
        $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock));
607
        $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock));
608
609
        $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST));
610
        $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock));
611
        $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->googleBotIP));
612
        $requestMock->expects($this->once())->method("get")->with("_route", null, false)->will($this->returnValue("random_route"));
613
        $lookUpMock->expects($this->once())->method("getCountry")->with($this->googleBotIP)->will($this->returnValue("US"));
614
        $eventBlockMock->expects($this->never())->method("setResponse");
615
        $eventBlockMock->expects($this->never())->method("stopPropagation");
616
617
        $loggerMock->expects($this->never())->method("warning");
618
619
        $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters);
620
        $geoBlockingListener->onKernelRequest($eventBlockMock);
621
    }
622
623
    public function testOnKernelRequestGeoBlocking_Search_Engine_Bot_Allow_MSN()
624
    {
@@ 623-653 (lines=31) @@
620
        $geoBlockingListener->onKernelRequest($eventBlockMock);
621
    }
622
623
    public function testOnKernelRequestGeoBlocking_Search_Engine_Bot_Allow_MSN()
624
    {
625
        $parameters = $this->getDefaultParams();
626
        $parameters['allow_search_bots'] = true;
627
        $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock();
628
        $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock();
629
630
        $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock();
631
        $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock();
632
633
        $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock();
634
        $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\SecurityContext")->disableOriginalConstructor()->getMock();
635
        $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock();
636
637
        $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null));
638
        $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock));
639
        $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock));
640
641
        $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST));
642
        $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock));
643
        $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->msnBotIP));
644
        $requestMock->expects($this->once())->method("get")->with("_route", null, false)->will($this->returnValue("random_route"));
645
        $lookUpMock->expects($this->once())->method("getCountry")->with($this->msnBotIP)->will($this->returnValue("US"));
646
        $eventBlockMock->expects($this->never())->method("setResponse");
647
        $eventBlockMock->expects($this->never())->method("stopPropagation");
648
649
        $loggerMock->expects($this->never())->method("warning");
650
651
        $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters);
652
        $geoBlockingListener->onKernelRequest($eventBlockMock);
653
    }
654
655
    public function testOnKernelRequestGeoBlocking_Search_Engine_Bot_Deny_usIP()
656
    {
@@ 655-685 (lines=31) @@
652
        $geoBlockingListener->onKernelRequest($eventBlockMock);
653
    }
654
655
    public function testOnKernelRequestGeoBlocking_Search_Engine_Bot_Deny_usIP()
656
    {
657
        $parameters = $this->getDefaultParams();
658
        $parameters['allow_search_bots'] = true;
659
660
        $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock();
661
        $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock();
662
663
        $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock();
664
        $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock();
665
666
        $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock();
667
        $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\SecurityContext")->disableOriginalConstructor()->getMock();
668
        $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock();
669
670
        $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null));
671
        $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock));
672
        $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock));
673
674
        $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST));
675
        $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock));
676
        $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->usIP));
677
        $requestMock->expects($this->once())->method("get")->with("_route", null, false)->will($this->returnValue("random_route"));
678
        $lookUpMock->expects($this->once())->method("getCountry")->with($this->usIP)->will($this->returnValue("US"));
679
680
        $eventBlockMock->expects($this->once())->method("setResponse")->will($this->returnCallback(array($this, 'checkResponseCode')));
681
        $eventBlockMock->expects($this->once())->method("stopPropagation");
682
683
        $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(true), $lookUpMock, $loggerMock, $containerMock, $parameters);
684
        $geoBlockingListener->onKernelRequest($eventBlockMock);
685
    }
686
687
    public function testOnKernelRequestGeoBlocking_allow_by_cookie_yes(){
688
     	$parameters = $this->getDefaultParams();