diff --git a/Classes/Controller/Frontend/CalendarController.php b/Classes/Controller/Frontend/CalendarController.php index 67345db..9d3416b 100644 --- a/Classes/Controller/Frontend/CalendarController.php +++ b/Classes/Controller/Frontend/CalendarController.php @@ -21,6 +21,7 @@ namespace WerkraumMedia\Calendar\Controller\Frontend; * 02110-1301, USA. */ +use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Annotation as Extbase; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter; @@ -28,6 +29,7 @@ use WerkraumMedia\Calendar\Domain\Model\Day; use WerkraumMedia\Calendar\Domain\Model\Month; use WerkraumMedia\Calendar\Domain\Model\Week; use WerkraumMedia\Calendar\Domain\Model\Year; +use WerkraumMedia\Calendar\Events\AssignTemplateVariables; class CalendarController extends ActionController { @@ -51,7 +53,7 @@ class CalendarController extends ActionController */ public function yearAction(Year $year) { - $this->view->assignMultiple([ + $this->assignVariables([ 'year' => $year, ]); } @@ -77,7 +79,7 @@ class CalendarController extends ActionController */ public function monthAction(Month $month) { - $this->view->assignMultiple([ + $this->assignVariables([ 'month' => $month, ]); } @@ -103,7 +105,7 @@ class CalendarController extends ActionController */ public function weekAction(Week $week) { - $this->view->assignMultiple([ + $this->assignVariables([ 'week' => $week, ]); } @@ -134,8 +136,15 @@ class CalendarController extends ActionController */ public function dayAction(Day $day) { - $this->view->assignMultiple([ + $this->assignVariables([ 'day' => $day, ]); } + + private function assignVariables(array $variables): void + { + $event = GeneralUtility::makeInstance(AssignTemplateVariables::class, $variables); + $this->eventDispatcher->dispatch($event); + $this->view->assignMultiple($event->getVariables()); + } } diff --git a/Classes/Events/AssignTemplateVariables.php b/Classes/Events/AssignTemplateVariables.php new file mode 100644 index 0000000..35c4e0f --- /dev/null +++ b/Classes/Events/AssignTemplateVariables.php @@ -0,0 +1,46 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +final class AssignTemplateVariables +{ + /** + * @var array + */ + private $variables; + + public function __construct( + array $variables + ) { + $this->variables = $variables; + } + + public function getVariables(): array + { + return $this->variables; + } + + public function setVariables(array $variables): void + { + $this->variables = $variables; + } +} diff --git a/Tests/Unit/Controller/Frontend/CalendarControllerTest.php b/Tests/Unit/Controller/Frontend/CalendarControllerTest.php index 353d63e..004330f 100644 --- a/Tests/Unit/Controller/Frontend/CalendarControllerTest.php +++ b/Tests/Unit/Controller/Frontend/CalendarControllerTest.php @@ -25,6 +25,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\Argument as ProphetArgument; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; +use Psr\EventDispatcher\EventDispatcherInterface; use TYPO3\CMS\Extbase\Mvc\Controller\Argument; use TYPO3\CMS\Extbase\Mvc\Controller\Arguments; use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; @@ -35,6 +36,7 @@ use WerkraumMedia\Calendar\Domain\Model\Day; use WerkraumMedia\Calendar\Domain\Model\Month; use WerkraumMedia\Calendar\Domain\Model\Week; use WerkraumMedia\Calendar\Domain\Model\Year; +use WerkraumMedia\Calendar\Events\AssignTemplateVariables; use WerkraumMedia\Calendar\Tests\ForcePropertyTrait; /** @@ -96,6 +98,10 @@ class CalendarControllerTest extends TestCase { $subject = new CalendarController(); + $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + $eventDispatcher->dispatch(ProphetArgument::type(AssignTemplateVariables::class))->shouldBeCalled(); + $subject->injectEventDispatcher($eventDispatcher->reveal()); + $year = $this->prophesize(Year::class); $view = $this->prophesize(ViewInterface::class); $view->assignMultiple([ @@ -159,6 +165,10 @@ class CalendarControllerTest extends TestCase { $subject = new CalendarController(); + $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + $eventDispatcher->dispatch(ProphetArgument::type(AssignTemplateVariables::class))->shouldBeCalled(); + $subject->injectEventDispatcher($eventDispatcher->reveal()); + $month = $this->prophesize(Month::class); $view = $this->prophesize(ViewInterface::class); $view->assignMultiple([ @@ -222,6 +232,10 @@ class CalendarControllerTest extends TestCase { $subject = new CalendarController(); + $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + $eventDispatcher->dispatch(ProphetArgument::type(AssignTemplateVariables::class))->shouldBeCalled(); + $subject->injectEventDispatcher($eventDispatcher->reveal()); + $week = $this->prophesize(Week::class); $view = $this->prophesize(ViewInterface::class); $view->assignMultiple([ @@ -306,6 +320,10 @@ class CalendarControllerTest extends TestCase { $subject = new CalendarController(); + $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + $eventDispatcher->dispatch(ProphetArgument::type(AssignTemplateVariables::class))->shouldBeCalled(); + $subject->injectEventDispatcher($eventDispatcher->reveal()); + $day = $this->prophesize(Day::class); $view = $this->prophesize(ViewInterface::class); $view->assignMultiple([ diff --git a/Tests/Unit/Events/AssignTemplateVariablesTest.php b/Tests/Unit/Events/AssignTemplateVariablesTest.php new file mode 100644 index 0000000..b4c6dee --- /dev/null +++ b/Tests/Unit/Events/AssignTemplateVariablesTest.php @@ -0,0 +1,83 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use PHPUnit\Framework\TestCase; +use WerkraumMedia\Calendar\Events\AssignTemplateVariables; + +/** + * @covers WerkraumMedia\Calendar\Events\AssignTemplateVariables + */ +class AssignTemplateVariablesTest extends TestCase +{ + /** + * @test + */ + public function canBeCreated(): void + { + $subject = new AssignTemplateVariables( + [] + ); + + self::assertInstanceOf(AssignTemplateVariables::class, $subject); + } + + /** + * @test + */ + public function returnsVariables(): void + { + $subject = new AssignTemplateVariables( + [ + 'variable1' => 'value1', + ] + ); + + $result = $subject->getVariables(); + + self::assertSame([ + 'variable1' => 'value1', + ], $result); + } + + /** + * @test + */ + public function newVariablesOverwriteExistingVariables(): void + { + $subject = new AssignTemplateVariables( + [ + 'variable1' => 'value1', + ] + ); + + $subject->setVariables([ + 'variable2' => 'value2', + ]); + + $result = $subject->getVariables(); + + self::assertSame([ + 'variable2' => 'value2', + ], $result); + } +}