Skip to content

Commit

Permalink
Remove reference to LiipThemeBundle and dispatch event instead (#7004)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogamoore committed Feb 15, 2023
1 parent 675ea77 commit faee567
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 11 deletions.
6 changes: 6 additions & 0 deletions UPGRADE.md
@@ -1,5 +1,11 @@
# Upgrade

## 2.5.7

### Constructor of ValidateWebspacesCommand changed

The constructor of `ValidateWebspacesCommand` requires now EventDispatcherInterface instead of activeTheme.

## 2.5.2

### Add indexes to route table
Expand Down
22 changes: 13 additions & 9 deletions src/Sulu/Bundle/PageBundle/Command/ValidateWebspacesCommand.php
Expand Up @@ -11,15 +11,19 @@

namespace Sulu\Bundle\PageBundle\Command;

use Sulu\Bundle\PreviewBundle\Preview\Events;
use Sulu\Bundle\PreviewBundle\Preview\Events\PreRenderEvent;
use Sulu\Component\Content\Compat\StructureManagerInterface;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\Webspace\Analyzer\Attributes\RequestAttributes;
use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
use Sulu\Component\Webspace\StructureProvider\WebspaceStructureProvider;
use Sulu\Component\Webspace\Webspace;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;

class ValidateWebspacesCommand extends Command
Expand Down Expand Up @@ -62,14 +66,14 @@ class ValidateWebspacesCommand extends Command
private $errors = [];

/**
* @var string
* @var WebspaceManagerInterface
*/
private $activeTheme;
private $webspaceManager;

/**
* @var WebspaceManagerInterface
* @var EventDispatcherInterface
*/
private $webspaceManager;
private $eventDispatcher;

public function __construct(
Environment $twig,
Expand All @@ -78,7 +82,7 @@ public function __construct(
StructureManagerInterface $structureManager,
WebspaceStructureProvider $structureProvider,
WebspaceManagerInterface $webspaceManager,
$activeTheme = null
EventDispatcherInterface $eventDispatcher
) {
parent::__construct();

Expand All @@ -87,8 +91,8 @@ public function __construct(
$this->controllerNameConverter = $controllerNameConverter;
$this->structureManager = $structureManager;
$this->structureProvider = $structureProvider;
$this->activeTheme = $activeTheme;
$this->webspaceManager = $webspaceManager;
$this->eventDispatcher = $eventDispatcher;
}

protected function configure()
Expand All @@ -106,9 +110,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$messages = '';

foreach ($webspaces as $webspace) {
if (null !== $this->activeTheme) {
$this->activeTheme->setName($webspace->getTheme());
}
$this->eventDispatcher->dispatch(new PreRenderEvent(new RequestAttributes([
'webspace' => $webspace,
])), Events::PRE_RENDER);

$this->outputWebspace($webspace);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sulu/Bundle/PageBundle/Resources/config/command.xml
Expand Up @@ -54,7 +54,7 @@
<argument type="service" id="sulu.content.structure_manager"/>
<argument type="service" id="sulu.content.webspace_structure_provider"/>
<argument type="service" id="sulu_core.webspace.webspace_manager"/>
<argument type="service" id="liip_theme.active_theme" on-invalid="null"/>
<argument type="service" id="event_dispatcher"/>

<tag name="console.command"/>
</service>
Expand Down
Expand Up @@ -45,7 +45,8 @@ public function setUp(): void
$controllerNameParser,
$this->getContainer()->get('sulu.content.structure_manager'),
$this->getContainer()->get('sulu.content.webspace_structure_provider'),
$this->getContainer()->get('sulu_core.webspace.webspace_manager')
$this->getContainer()->get('sulu_core.webspace.webspace_manager'),
$this->getContainer()->get('event_dispatcher')
);
$command->setApplication($application);
$this->tester = new CommandTester($command);
Expand Down
@@ -0,0 +1,125 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\PageBundle\Tests\Unit\Command;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Sulu\Bundle\PageBundle\Command\ValidateWebspacesCommand;
use Sulu\Bundle\PreviewBundle\Preview\Events;
use Sulu\Bundle\PreviewBundle\Preview\Events\PreRenderEvent;
use Sulu\Component\Content\Compat\StructureManagerInterface;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactory;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\Localization\Localization;
use Sulu\Component\Webspace\Analyzer\Attributes\RequestAttributes;
use Sulu\Component\Webspace\Manager\WebspaceCollection;
use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
use Sulu\Component\Webspace\StructureProvider\WebspaceStructureProvider;
use Sulu\Component\Webspace\Webspace;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;

class ValidateWebspacesCommandTest extends TestCase
{
use ProphecyTrait;

/**
* @var ObjectProphecy<Environment>
*/
private $twig;

/**
* @var ObjectProphecy<StructureMetadataFactory>
*/
private $structureMetadataFactory;

/**
* @var ObjectProphecy<StructureManagerInterface>
*/
private $structureManager;

/**
* @var ObjectProphecy<WebspaceStructureProvider>
*/
private $structureProvider;

/**
* @var ObjectProphecy<WebspaceManagerInterface>
*/
private $webspaceManager;

/**
* @var ObjectProphecy<EventDispatcherInterface>
*/
private $eventDispatcher;

/**
* @var ValidateWebspacesCommand
*/
private $validateWebspacesCommand;

/**
* @var ObjectProphecy<InputInterface>
*/
private $input;

/**
* @var ObjectProphecy<OutputInterface>
*/
private $output;

public function setUp(): void
{
$this->twig = $this->prophesize(Environment::class);
$this->structureMetadataFactory = $this->prophesize(StructureMetadataFactoryInterface::class);
$this->structureManager = $this->prophesize(StructureManagerInterface::class);
$this->structureProvider = $this->prophesize(WebspaceStructureProvider::class);
$this->webspaceManager = $this->prophesize(WebspaceManagerInterface::class);
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);

$this->validateWebspacesCommand = new ValidateWebspacesCommand(
$this->twig->reveal(),
$this->structureMetadataFactory->reveal(),
null,
$this->structureManager->reveal(),
$this->structureProvider->reveal(),
$this->webspaceManager->reveal(),
$this->eventDispatcher->reveal()
);

$this->input = $this->prophesize(InputInterface::class);
$this->output = $this->prophesize(OutputInterface::class);
}

public function testExecute(): void
{
$webspace = new Webspace();
$webspace->setKey('sulu_io');
$webspace->addLocalization(new Localization('de'));

$this->structureManager->getStructures()->willReturn([]);
$this->webspaceManager->getWebspaceCollection()->willReturn(new WebspaceCollection([$webspace]));

$executeMethod = new \ReflectionMethod(ValidateWebspacesCommand::class, 'execute');
$executeMethod->setAccessible(true);

$this->eventDispatcher->dispatch(new PreRenderEvent(new RequestAttributes([
'webspace' => $webspace,
])), Events::PRE_RENDER)
->shouldBeCalled();

$executeMethod->invoke($this->validateWebspacesCommand, $this->input->reveal(), $this->output->reveal());
}
}

0 comments on commit faee567

Please sign in to comment.