Skip to content

Commit 7871e11

Browse files
NattfarinnSteveb-p
andauthored
IBX-431: Added event dispatching after building the View (#226)
* IBX-431: Added event dispatching after building the View * fix: Rebrand PostBuildViewEvent * fix: Add missing test coverage * fix: Add missing test coverage * Update eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ViewControllerListenerTest.php Co-authored-by: Paweł Niedzielski <[email protected]> * Update eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ViewControllerListenerTest.php Co-authored-by: Paweł Niedzielski <[email protected]> * Update eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ViewControllerListenerTest.php Co-authored-by: Paweł Niedzielski <[email protected]> * Fixed test * Update tests/lib/Event/View/PostBuildViewEventTest.php Co-authored-by: Paweł Niedzielski <[email protected]> * Fixed test * Fixed test * Re-used test properties Co-authored-by: Paweł Niedzielski <[email protected]>
1 parent 56b73a0 commit 7871e11

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

eZ/Bundle/EzPublishCoreBundle/EventListener/ViewControllerListener.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use eZ\Publish\Core\MVC\Symfony\View\Builder\ViewBuilderRegistry;
1010
use eZ\Publish\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent;
1111
use eZ\Publish\Core\MVC\Symfony\View\ViewEvents;
12+
use Ibexa\Contracts\Core\Event\View\PostBuildViewEvent;
1213
use Psr\Log\LoggerInterface;
1314
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1415
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -66,6 +67,7 @@ public function getController(ControllerEvent $event)
6667
$parameterEvent = new FilterViewBuilderParametersEvent(clone $request);
6768
$this->eventDispatcher->dispatch($parameterEvent, ViewEvents::FILTER_BUILDER_PARAMETERS);
6869
$view = $viewBuilder->buildView($parameterEvent->getParameters()->all());
70+
$this->eventDispatcher->dispatch(new PostBuildViewEvent($view));
6971
$request->attributes->set('view', $view);
7072

7173
// View parameters are added as request attributes so that they are available as controller action parameters

eZ/Bundle/EzPublishCoreBundle/Tests/EventListener/ViewControllerListenerTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
namespace eZ\Bundle\EzPublishCoreBundle\Tests\EventListener;
88

99
use eZ\Bundle\EzPublishCoreBundle\EventListener\ViewControllerListener;
10+
use eZ\Publish\Core\MVC\Symfony\View\BaseView;
1011
use eZ\Publish\Core\MVC\Symfony\View\Builder\ViewBuilder;
1112
use eZ\Publish\Core\MVC\Symfony\View\Builder\ViewBuilderRegistry;
1213
use eZ\Publish\Core\MVC\Symfony\View\ContentView;
14+
use eZ\Publish\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent;
15+
use eZ\Publish\Core\MVC\Symfony\View\ViewEvents;
16+
use Ibexa\Contracts\Core\Event\View\PostBuildViewEvent;
17+
use PHPUnit\Framework\TestCase;
1318
use Psr\Log\LoggerInterface;
1419
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1520
use Symfony\Component\HttpFoundation\Request;
1621
use Symfony\Component\HttpKernel\Controller\ControllerReference;
17-
use PHPUnit\Framework\TestCase;
1822
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
1923
use Symfony\Component\HttpKernel\Event\ControllerEvent;
2024
use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -151,6 +155,37 @@ public function testGetControllerMatchedView()
151155
$this->assertEquals($expectedView, $this->request->attributes->get('view'));
152156
}
153157

158+
public function testGetControllerEmitsProperEvents(): void
159+
{
160+
$viewObject = new class() extends BaseView {
161+
};
162+
163+
$this->viewBuilderRegistry
164+
->expects($this->once())
165+
->method('getFromRegistry')
166+
->willReturn($this->viewBuilderMock);
167+
168+
$this->viewBuilderMock
169+
->expects($this->once())
170+
->method('buildView')
171+
->willReturn($viewObject);
172+
173+
$this->eventDispatcher
174+
->expects($this->exactly(2))
175+
->method('dispatch')
176+
->withConsecutive(
177+
[
178+
$this->isInstanceOf(FilterViewBuilderParametersEvent::class),
179+
$this->identicalTo(ViewEvents::FILTER_BUILDER_PARAMETERS),
180+
], [
181+
$this->isInstanceOf(PostBuildViewEvent::class),
182+
$this->isNull(),
183+
])
184+
->willReturnArgument(0);
185+
186+
$this->controllerListener->getController($this->event);
187+
}
188+
154189
/**
155190
* @return \PHPUnit\Framework\MockObject\MockObject|ControllerEvent
156191
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Contracts\Core\Event\View;
10+
11+
use eZ\Publish\Core\MVC\Symfony\View\View;
12+
use Symfony\Contracts\EventDispatcher\Event;
13+
14+
final class PostBuildViewEvent extends Event
15+
{
16+
/** @var \eZ\Publish\Core\MVC\Symfony\View\View */
17+
private $view;
18+
19+
public function __construct(View $view)
20+
{
21+
$this->view = $view;
22+
}
23+
24+
public function getView(): View
25+
{
26+
return $this->view;
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Core\Event\View;
10+
11+
use eZ\Publish\Core\MVC\Symfony\View\BaseView;
12+
use Ibexa\Contracts\Core\Event\View\PostBuildViewEvent;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class PostBuildViewEventTest extends TestCase
16+
{
17+
public function testEventConstruction(): void
18+
{
19+
$view = new class() extends BaseView {
20+
};
21+
22+
$event = new PostBuildViewEvent($view);
23+
24+
self::assertSame($view, $event->getView());
25+
}
26+
}

0 commit comments

Comments
 (0)