cleanup global context usage in widgets

This commit is contained in:
Kevin Papst
2020-08-18 11:39:41 +02:00
parent 08696651fd
commit 9597015413
29 changed files with 458 additions and 202 deletions

View File

@@ -0,0 +1,39 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Twig;
use App\Twig\EventExtensions;
use PHPUnit\Framework\TestCase;
use Twig\TwigFunction;
/**
* @covers \App\Twig\EventExtensions
*/
class EventExtensionsTest extends TestCase
{
protected function getSut(): EventExtensions
{
return new EventExtensions();
}
public function testGetFunctions()
{
$functions = ['trigger'];
$sut = $this->getSut();
$twigFunctions = $sut->getFunctions();
self::assertCount(\count($functions), $twigFunctions);
$i = 0;
/** @var TwigFunction $filter */
foreach ($twigFunctions as $filter) {
self::assertInstanceOf(TwigFunction::class, $filter);
self::assertEquals($functions[$i++], $filter->getName());
}
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Twig\Runtime;
use App\Entity\User;
use App\Event\ThemeEvent;
use App\Tests\Mocks\Security\CurrentUserFactory;
use App\Twig\Runtime\ThemeEventExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* @covers \App\Twig\Runtime\ThemeEventExtension
*/
class ThemeEventExtensionTest extends TestCase
{
protected function getSut(bool $hasListener = true): ThemeEventExtension
{
$dispatcher = $this->createMock(EventDispatcherInterface::class);
$dispatcher->expects($this->once())->method('hasListeners')->willReturn($hasListener);
$dispatcher->expects($hasListener ? $this->once() : $this->never())->method('dispatch');
$user = (new CurrentUserFactory($this))->create(new User());
return new ThemeEventExtension($dispatcher, $user);
}
public function testTrigger()
{
$sut = $this->getSut();
$event = $sut->trigger('foo', []);
self::assertInstanceOf(ThemeEvent::class, $event);
}
public function testTriggerWithoutListener()
{
$sut = $this->getSut(false);
$event = $sut->trigger('foo', []);
self::assertInstanceOf(ThemeEvent::class, $event);
}
}

View File

@@ -10,7 +10,7 @@
namespace App\Tests\Twig;
use App\Twig\WidgetExtension;
use App\Widget\Type\Counter;
use App\Widget\Type\More;
use App\Widget\WidgetInterface;
use App\Widget\WidgetRendererInterface;
use App\Widget\WidgetService;
@@ -72,7 +72,7 @@ class WidgetExtensionTest extends TestCase
public function testRenderWidgetByString()
{
$widget = new Counter();
$widget = new More();
$sut = $this->getSut(true, $widget, new TestRenderer());
$options = ['foo' => 'bar', 'dataType' => 'blub'];
$result = $sut->renderWidget('test', $options);
@@ -82,7 +82,7 @@ class WidgetExtensionTest extends TestCase
public function testRenderWidgetObject()
{
$widget = new Counter();
$widget = new More();
$sut = $this->getSut(null, null, new TestRenderer());
$options = ['foo' => 'bar', 'dataType' => 'blub'];
$result = $sut->renderWidget($widget, $options);