Skip to content

Commit

Permalink
Decouple bundles from render-event
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Jul 19, 2021
1 parent 04e8432 commit 80935f7
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 108 deletions.
1 change: 0 additions & 1 deletion packages/basic-page-bundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"friendsofsymfony/ckeditor-bundle": "^2.2",
"gedmo/doctrine-extensions": "^3.0",
"knplabs/doctrine-behaviors": "^2.0.6",
"runroom-packages/render-event-bundle": "^0.11@dev",
"runroom-packages/seo-bundle": "^0.11@dev",
"sonata-project/admin-bundle": "^3.103 || ^4.0@dev",
"sonata-project/doctrine-orm-admin-bundle": "^3.34 || ^4.0@dev",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,24 @@
namespace Runroom\BasicPageBundle\Controller;

use Runroom\BasicPageBundle\Service\BasicPageService;
use Runroom\RenderEventBundle\Renderer\PageRenderer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

final class BasicPageController
final class BasicPageController extends AbstractController
{
private PageRenderer $renderer;
private BasicPageService $service;

public function __construct(
PageRenderer $renderer,
BasicPageService $service
) {
$this->renderer = $renderer;
public function __construct(BasicPageService $service)
{
$this->service = $service;
}

public function show(string $slug): Response
{
$model = $this->service->getBasicPageViewModel($slug);

return $this->renderer->renderResponse('@RunroomBasicPage/show.html.twig', $model);
return $this->render('@RunroomBasicPage/show.html.twig', [
'model' => $model,
]);
}
}
8 changes: 3 additions & 5 deletions packages/basic-page-bundle/src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Runroom\BasicPageBundle\Service\BasicPageAlternateLinksProvider;
use Runroom\BasicPageBundle\Service\BasicPageMetaInformationProvider;
use Runroom\BasicPageBundle\Service\BasicPageService;
use Runroom\RenderEventBundle\Renderer\PageRenderer;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator;

Expand All @@ -33,12 +32,11 @@

$services->set(BasicPageController::class)
->arg('$service', new ReferenceConfigurator(BasicPageService::class))
->arg('$renderer', new ReferenceConfigurator(PageRenderer::class))
->tag('controller.service_arguments');
->call('setContainer', [new ReferenceConfigurator('service_container')])
->tag('controller.service_subscriber');

$services->set(BasicPageService::class)
->arg('$repository', new ReferenceConfigurator(BasicPageRepository::class))
->tag('kernel.event_subscriber');
->arg('$repository', new ReferenceConfigurator(BasicPageRepository::class));

$services->set(BasicPageAlternateLinksProvider::class)
->tag('runroom.seo.alternate_links');
Expand Down
20 changes: 1 addition & 19 deletions packages/basic-page-bundle/src/Service/BasicPageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@

use Runroom\BasicPageBundle\Repository\BasicPageRepository;
use Runroom\BasicPageBundle\ViewModel\BasicPageViewModel;
use Runroom\RenderEventBundle\Event\PageRenderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/** @final */
class BasicPageService implements EventSubscriberInterface
class BasicPageService
{
private BasicPageRepository $repository;

Expand All @@ -37,20 +35,4 @@ public function getBasicPageViewModel(string $slug): BasicPageViewModel

return $model;
}

public function onPageRender(PageRenderEvent $event): void
{
$page = $event->getPageViewModel();

$page->addContext('basic_pages', $this->repository->findBy(['publish' => true]));

$event->setPageViewModel($page);
}

public static function getSubscribedEvents()
{
return [
PageRenderEvent::EVENT_NAME => 'onPageRender',
];
}
}
2 changes: 0 additions & 2 deletions packages/basic-page-bundle/tests/App/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Knp\DoctrineBehaviors\DoctrineBehaviorsBundle;
use Runroom\BasicPageBundle\RunroomBasicPageBundle;
use Runroom\BasicPageBundle\Tests\App\Entity\Media;
use Runroom\RenderEventBundle\RunroomRenderEventBundle;
use Runroom\SeoBundle\RunroomSeoBundle;
use Sonata\AdminBundle\SonataAdminBundle;
use Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle;
Expand Down Expand Up @@ -58,7 +57,6 @@ public function registerBundles(): iterable
new ZenstruckFoundryBundle(),

new RunroomBasicPageBundle(),
new RunroomRenderEventBundle(),
new RunroomSeoBundle(),
];
}
Expand Down
27 changes: 13 additions & 14 deletions packages/basic-page-bundle/tests/Unit/BasicPageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,41 @@
use Runroom\BasicPageBundle\Controller\BasicPageController;
use Runroom\BasicPageBundle\Service\BasicPageService;
use Runroom\BasicPageBundle\ViewModel\BasicPageViewModel;
use Runroom\RenderEventBundle\Renderer\PageRenderer;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\Container;
use Twig\Environment;

class BasicPageControllerTest extends TestCase
{
/** @var MockObject&PageRenderer */
private $renderer;

/** @var MockObject&BasicPageService */
private $service;

/** @var MockObject&Environment */
private $twig;

private BasicPageController $controller;

/** @psalm-suppress InternalMethod setContainer is internal on Symfony 5.x */
protected function setUp(): void
{
$this->renderer = $this->createMock(PageRenderer::class);
$this->service = $this->createMock(BasicPageService::class);
$this->twig = $this->createMock(Environment::class);

$container = new Container();
$container->set('twig', $this->twig);

$this->controller = new BasicPageController(
$this->renderer,
$this->service
);
$this->controller = new BasicPageController($this->service);
$this->controller->setContainer($container);
}

/** @test */
public function itRendersStatic(): void
{
$model = new BasicPageViewModel();
$expectedResponse = new Response();

$this->service->method('getBasicPageViewModel')->with('slug')->willReturn($model);
$this->renderer->method('renderResponse')->with('@RunroomBasicPage/show.html.twig', $model, null)
->willReturn($expectedResponse);

$response = $this->controller->show('slug');

self::assertSame($expectedResponse, $response);
self::assertSame(200, $response->getStatusCode());
}
}
24 changes: 1 addition & 23 deletions packages/basic-page-bundle/tests/Unit/BasicPageServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
use Runroom\BasicPageBundle\Entity\BasicPage;
use Runroom\BasicPageBundle\Repository\BasicPageRepository;
use Runroom\BasicPageBundle\Service\BasicPageService;
use Runroom\RenderEventBundle\Event\PageRenderEvent;
use Runroom\RenderEventBundle\ViewModel\PageViewModel;

class BasicPageServiceTest extends TestCase
{
Expand All @@ -36,7 +34,7 @@ protected function setUp(): void
}

/** @test */
public function itGetsBasicViewModel(): void
public function itGetsBasicPage(): void
{
$basicPage = new BasicPage();

Expand All @@ -46,24 +44,4 @@ public function itGetsBasicViewModel(): void

self::assertSame($basicPage, $model->getBasicPage());
}

/** @test */
public function itAddsBasicPagesToPageViewModel(): void
{
$event = new PageRenderEvent('view', new PageViewModel());

$this->repository->method('findBy')->with(['publish' => true])->willReturn([]);

$this->service->onPageRender($event);

self::assertSame([], $event->getPageViewModel()->getContext('basic_pages'));
}

/** @test */
public function itHasSubscribedEvents(): void
{
$events = BasicPageService::getSubscribedEvents();

self::assertCount(1, $events);
}
}
1 change: 0 additions & 1 deletion packages/cookies-bundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"friendsofsymfony/ckeditor-bundle": "^2.2",
"knplabs/doctrine-behaviors": "^2.0.6",
"runroom-packages/form-handler-bundle": "^0.11@dev",
"runroom-packages/render-event-bundle": "^0.11@dev",
"sonata-project/admin-bundle": "^3.103 || ^4.0@dev",
"sonata-project/doctrine-orm-admin-bundle": "^3.34 || ^4.0@dev",
"symfony/config": "^4.4 || ^5.3",
Expand Down
18 changes: 8 additions & 10 deletions packages/cookies-bundle/src/Controller/CookiesPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,24 @@
namespace Runroom\CookiesBundle\Controller;

use Runroom\CookiesBundle\Service\CookiesPageService;
use Runroom\RenderEventBundle\Renderer\PageRenderer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

final class CookiesPageController
final class CookiesPageController extends AbstractController
{
private PageRenderer $renderer;
private CookiesPageService $service;

public function __construct(
PageRenderer $renderer,
CookiesPageService $service
) {
$this->renderer = $renderer;
public function __construct(CookiesPageService $service)
{
$this->service = $service;
}

public function index(): Response
{
$viewModel = $this->service->getViewModel();
$model = $this->service->getCookiesPageViewModel();

return $this->renderer->renderResponse('@RunroomCookies/show.html.twig', $viewModel);
return $this->render('@RunroomCookies/show.html.twig', [
'model' => $model,
]);
}
}
2 changes: 0 additions & 2 deletions packages/cookies-bundle/src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Runroom\CookiesBundle\Service\CookiesPageService;
use Runroom\CookiesBundle\Service\CookiesService;
use Runroom\FormHandlerBundle\FormHandler;
use Runroom\RenderEventBundle\Renderer\PageRenderer;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator;

Expand All @@ -33,7 +32,6 @@

$services->set(CookiesPageController::class)
->arg('$service', new ReferenceConfigurator(CookiesPageService::class))
->arg('$renderer', new ReferenceConfigurator(PageRenderer::class))
->tag('controller.service_arguments');

$services->set(CookiesPageService::class)
Expand Down
2 changes: 1 addition & 1 deletion packages/cookies-bundle/src/Service/CookiesPageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(
$this->cookies = $cookies;
}

public function getViewModel(): FormAwareInterface
public function getCookiesPageViewModel(): FormAwareInterface
{
$cookiesPage = $this->repository->find(self::COOKIES_PAGE_ID);

Expand Down
2 changes: 0 additions & 2 deletions packages/cookies-bundle/tests/App/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Knp\DoctrineBehaviors\DoctrineBehaviorsBundle;
use Runroom\CookiesBundle\RunroomCookiesBundle;
use Runroom\FormHandlerBundle\RunroomFormHandlerBundle;
use Runroom\RenderEventBundle\RunroomRenderEventBundle;
use Sonata\AdminBundle\SonataAdminBundle;
use Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
Expand Down Expand Up @@ -58,7 +57,6 @@ public function registerBundles(): iterable
new ZenstruckFoundryBundle(),

new RunroomFormHandlerBundle(),
new RunroomRenderEventBundle(),
new RunroomCookiesBundle(),
];
}
Expand Down
28 changes: 14 additions & 14 deletions packages/cookies-bundle/tests/Unit/CookiesPageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,38 @@
use Runroom\CookiesBundle\Controller\CookiesPageController;
use Runroom\CookiesBundle\Service\CookiesPageService;
use Runroom\CookiesBundle\ViewModel\CookiesPageViewModel;
use Runroom\RenderEventBundle\Renderer\PageRenderer;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\Container;
use Twig\Environment;

class CookiesPageControllerTest extends TestCase
{
/** @var MockObject&PageRenderer */
private $renderer;

/** @var MockObject&CookiesPageService */
private $service;

/** @var MockObject&Environment */
private $twig;

private CookiesPageController $controller;

/** @psalm-suppress InternalMethod setContainer is internal on Symfony 5.x */
protected function setUp(): void
{
$this->renderer = $this->createMock(PageRenderer::class);
$this->service = $this->createMock(CookiesPageService::class);
$this->twig = $this->createMock(Environment::class);

$container = new Container();
$container->set('twig', $this->twig);

$this->controller = new CookiesPageController(
$this->renderer,
$this->service
);
$this->controller = new CookiesPageController($this->service);
$this->controller->setContainer($container);
}

/** @test */
public function itRendersCookiesPage(): void
{
$viewModel = new CookiesPageViewModel();
$model = new CookiesPageViewModel();

$this->service->expects(self::once())->method('getViewModel')->willReturn($viewModel);
$this->renderer->method('renderResponse')->with('@RunroomCookies/show.html.twig', $viewModel)
->willReturn(new Response());
$this->service->expects(self::once())->method('getCookiesPageViewModel')->willReturn($model);

$response = $this->controller->index();

Expand Down
10 changes: 5 additions & 5 deletions packages/cookies-bundle/tests/Unit/CookiesPageServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function setUp(): void
}

/** @test */
public function itGetsViewModel(): void
public function itGetsCookiesPage(): void
{
$cookiesPage = CookiesPageFactory::createOne()->object();
$this->repository->expects(self::once())->method('find')->with(1)->willReturn($cookiesPage);
Expand All @@ -57,10 +57,10 @@ public function itGetsViewModel(): void
->with(CookiesFormType::class, [], self::isInstanceOf(CookiesPageViewModel::class))
->willReturnArgument(2);

$viewModel = $this->service->getViewModel();
$model = $this->service->getCookiesPageViewModel();

self::assertInstanceOf(CookiesPageViewModel::class, $viewModel);
self::assertSame($viewModel->getCookiesPage(), $cookiesPage);
self::assertSame($viewModel->getCookies(), []);
self::assertInstanceOf(CookiesPageViewModel::class, $model);
self::assertSame($model->getCookiesPage(), $cookiesPage);
self::assertSame($model->getCookies(), []);
}
}

0 comments on commit 80935f7

Please sign in to comment.