"Fossies" - the Fresh Open Source Software Archive

Member "contao-4.4.48/core-bundle/tests/EventListener/AddToSearchIndexListenerTest.php" (2 Apr 2020, 5339 Bytes) of package /linux/www/contao-4.4.48.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "AddToSearchIndexListenerTest.php": 4.4.47_vs_4.4.48.

    1 <?php
    2 
    3 /*
    4  * This file is part of Contao.
    5  *
    6  * (c) Leo Feyer
    7  *
    8  * @license LGPL-3.0-or-later
    9  */
   10 
   11 namespace Contao\CoreBundle\Tests\EventListener;
   12 
   13 use Contao\CoreBundle\EventListener\AddToSearchIndexListener;
   14 use Contao\CoreBundle\Framework\Adapter;
   15 use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
   16 use PHPUnit\Framework\TestCase;
   17 use Symfony\Component\HttpFoundation\Request;
   18 use Symfony\Component\HttpFoundation\Response;
   19 use Symfony\Component\HttpKernel\Event\PostResponseEvent;
   20 use Symfony\Component\HttpKernel\KernelInterface;
   21 
   22 /**
   23  * Tests the AddToSearchIndexListener class.
   24  *
   25  * @author Leo Feyer <https://github.com/leofeyer>
   26  */
   27 class AddToSearchIndexListenerTest extends TestCase
   28 {
   29     /**
   30      * @var ContaoFrameworkInterface|\PHPUnit_Framework_MockObject_MockObject
   31      */
   32     private $framework;
   33 
   34     /**
   35      * {@inheritdoc}
   36      */
   37     protected function setUp()
   38     {
   39         parent::setUp();
   40 
   41         $this->framework = $this->createMock(ContaoFrameworkInterface::class);
   42 
   43         $frontendAdapter = $this
   44             ->getMockBuilder(Adapter::class)
   45             ->disableOriginalConstructor()
   46             ->setMethods(['indexPageIfApplicable'])
   47             ->getMock()
   48         ;
   49 
   50         $frontendAdapter
   51             ->method('indexPageIfApplicable')
   52             ->willReturn(null)
   53         ;
   54 
   55         $this->framework
   56             ->method('getAdapter')
   57             ->willReturn($frontendAdapter)
   58         ;
   59     }
   60 
   61     /**
   62      * Tests that the listener does use the response if the Contao framework is booted.
   63      */
   64     public function testIndexesTheResponse()
   65     {
   66         $this->framework
   67             ->method('isInitialized')
   68             ->willReturn(true)
   69         ;
   70 
   71         $event = $this->mockPostResponseEvent();
   72 
   73         $event
   74             ->expects($this->once())
   75             ->method('getResponse')
   76             ->willReturn(new Response())
   77         ;
   78 
   79         $listener = new AddToSearchIndexListener($this->framework);
   80         $listener->onKernelTerminate($event);
   81     }
   82 
   83     /**
   84      * Tests that the listener does nothing if the Contao framework is not booted.
   85      */
   86     public function testDoesNotIndexTheResponseIfTheContaoFrameworkIsNotInitialized()
   87     {
   88         $this->framework
   89             ->method('isInitialized')
   90             ->willReturn(false)
   91         ;
   92 
   93         $event = $this->mockPostResponseEvent();
   94 
   95         $event
   96             ->expects($this->never())
   97             ->method('getResponse')
   98         ;
   99 
  100         $listener = new AddToSearchIndexListener($this->framework);
  101         $listener->onKernelTerminate($event);
  102     }
  103 
  104     /**
  105      * Tests that the listener does nothing if the request method is not GET.
  106      */
  107     public function testDoesNotIndexTheResponseIfTheRequestMethodIsNotGet()
  108     {
  109         $this->framework
  110             ->method('isInitialized')
  111             ->willReturn(true)
  112         ;
  113 
  114         $event = $this->mockPostResponseEvent(null, Request::METHOD_POST);
  115 
  116         $event
  117             ->expects($this->never())
  118             ->method('getResponse')
  119         ;
  120 
  121         $listener = new AddToSearchIndexListener($this->framework);
  122         $listener->onKernelTerminate($event);
  123     }
  124 
  125     /**
  126      * Tests that the listener does nothing if the request is a fragment.
  127      */
  128     public function testDoesNotIndexTheResponseUponFragmentRequests()
  129     {
  130         $this->framework
  131             ->method('isInitialized')
  132             ->willReturn(true)
  133         ;
  134 
  135         $event = $this->mockPostResponseEvent('_fragment/foo/bar');
  136 
  137         $event
  138             ->expects($this->never())
  139             ->method('getResponse')
  140         ;
  141 
  142         $listener = new AddToSearchIndexListener($this->framework);
  143         $listener->onKernelTerminate($event);
  144     }
  145 
  146     /**
  147      * Tests that the listener does nothing if the response was not successful.
  148      */
  149     public function testDoesNotIndexTheResponseIfItWasNotSuccessful()
  150     {
  151         $framework = $this->createMock(ContaoFrameworkInterface::class);
  152 
  153         $framework
  154             ->method('isInitialized')
  155             ->willReturn(true)
  156         ;
  157 
  158         $framework
  159             ->expects($this->never())
  160             ->method('getAdapter')
  161         ;
  162 
  163         $event = $this->mockPostResponseEvent();
  164 
  165         $event
  166             ->expects($this->once())
  167             ->method('getResponse')
  168             ->willReturn(new Response('', 500))
  169         ;
  170 
  171         $listener = new AddToSearchIndexListener($framework);
  172         $listener->onKernelTerminate($event);
  173     }
  174 
  175     /**
  176      * Returns a PostResponseEvent mock object.
  177      *
  178      * @param string|null $requestUri
  179      * @param string      $requestMethod
  180      *
  181      * @return \PHPUnit_Framework_MockObject_MockObject|PostResponseEvent
  182      */
  183     private function mockPostResponseEvent($requestUri = null, $requestMethod = Request::METHOD_GET)
  184     {
  185         $request = new Request();
  186         $request->setMethod($requestMethod);
  187 
  188         if (null !== $requestUri) {
  189             $request->server->set('REQUEST_URI', $requestUri);
  190         }
  191 
  192         return $this
  193             ->getMockBuilder(PostResponseEvent::class)
  194             ->setConstructorArgs([
  195                 $this->createMock(KernelInterface::class),
  196                 $request,
  197                 new Response(),
  198             ])
  199             ->setMethods(['getResponse'])
  200             ->getMock()
  201         ;
  202     }
  203 }