cleanup global context usage in widgets
This commit is contained in:
@@ -13,6 +13,7 @@ use App\Event\DashboardEvent;
|
|||||||
use App\Widget\Type\AbstractContainer;
|
use App\Widget\Type\AbstractContainer;
|
||||||
use App\Widget\Type\AuthorizedWidget;
|
use App\Widget\Type\AuthorizedWidget;
|
||||||
use App\Widget\Type\CompoundRow;
|
use App\Widget\Type\CompoundRow;
|
||||||
|
use App\Widget\Type\UserWidget;
|
||||||
use App\Widget\WidgetContainerInterface;
|
use App\Widget\WidgetContainerInterface;
|
||||||
use App\Widget\WidgetException;
|
use App\Widget\WidgetException;
|
||||||
use App\Widget\WidgetService;
|
use App\Widget\WidgetService;
|
||||||
@@ -31,15 +32,15 @@ class DashboardController extends AbstractController
|
|||||||
/**
|
/**
|
||||||
* @var EventDispatcherInterface
|
* @var EventDispatcherInterface
|
||||||
*/
|
*/
|
||||||
protected $eventDispatcher;
|
private $eventDispatcher;
|
||||||
/**
|
/**
|
||||||
* @var WidgetService
|
* @var WidgetService
|
||||||
*/
|
*/
|
||||||
protected $widgets;
|
private $widgets;
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $dashboard;
|
private $dashboard;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param EventDispatcherInterface $dispatcher
|
* @param EventDispatcherInterface $dispatcher
|
||||||
@@ -58,8 +59,9 @@ class DashboardController extends AbstractController
|
|||||||
*/
|
*/
|
||||||
public function indexAction()
|
public function indexAction()
|
||||||
{
|
{
|
||||||
$event = new DashboardEvent($this->getUser());
|
$user = $this->getUser();
|
||||||
|
|
||||||
|
$event = new DashboardEvent($user);
|
||||||
foreach ($this->dashboard as $widgetRow) {
|
foreach ($this->dashboard as $widgetRow) {
|
||||||
if (empty($widgetRow['widgets'])) {
|
if (empty($widgetRow['widgets'])) {
|
||||||
continue;
|
continue;
|
||||||
@@ -110,6 +112,10 @@ class DashboardController extends AbstractController
|
|||||||
$add = $tmp;
|
$add = $tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($widget instanceof UserWidget) {
|
||||||
|
$widget->setUser($user);
|
||||||
|
}
|
||||||
|
|
||||||
if ($add) {
|
if ($add) {
|
||||||
$row->addWidget($widget);
|
$row->addWidget($widget);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,8 @@
|
|||||||
|
|
||||||
namespace App\Repository;
|
namespace App\Repository;
|
||||||
|
|
||||||
use App\Entity\User;
|
|
||||||
use App\Security\CurrentUser;
|
|
||||||
use App\Widget\Type\AbstractWidgetType;
|
|
||||||
use App\Widget\Type\Counter;
|
use App\Widget\Type\Counter;
|
||||||
|
use App\Widget\Type\SimpleStatisticChart;
|
||||||
use App\Widget\Type\YearChart;
|
use App\Widget\Type\YearChart;
|
||||||
use App\Widget\WidgetException;
|
use App\Widget\WidgetException;
|
||||||
use App\Widget\WidgetInterface;
|
use App\Widget\WidgetInterface;
|
||||||
@@ -25,29 +23,19 @@ class WidgetRepository
|
|||||||
/**
|
/**
|
||||||
* @var TimesheetRepository
|
* @var TimesheetRepository
|
||||||
*/
|
*/
|
||||||
protected $repository;
|
private $repository;
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $widgets = [];
|
private $widgets = [];
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $definitions = [];
|
private $definitions = [];
|
||||||
/**
|
|
||||||
* @var User|null
|
|
||||||
*/
|
|
||||||
protected $user;
|
|
||||||
|
|
||||||
/**
|
public function __construct(TimesheetRepository $repository, array $widgets)
|
||||||
* @param TimesheetRepository $repository
|
|
||||||
* @param CurrentUser $user
|
|
||||||
* @param array $widgets
|
|
||||||
*/
|
|
||||||
public function __construct(TimesheetRepository $repository, CurrentUser $user, array $widgets)
|
|
||||||
{
|
{
|
||||||
$this->repository = $repository;
|
$this->repository = $repository;
|
||||||
$this->user = $user->getUser();
|
|
||||||
$this->definitions = array_merge($this->getDefaultWidgets(), $widgets);
|
$this->definitions = array_merge($this->getDefaultWidgets(), $widgets);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,12 +77,6 @@ class WidgetRepository
|
|||||||
*/
|
*/
|
||||||
protected function create(string $name, array $widget): WidgetInterface
|
protected function create(string $name, array $widget): WidgetInterface
|
||||||
{
|
{
|
||||||
$user = $this->user;
|
|
||||||
$timezone = new \DateTimeZone($user->getTimezone());
|
|
||||||
$begin = !empty($widget['begin']) ? new \DateTime($widget['begin'], $timezone) : null;
|
|
||||||
$end = !empty($widget['end']) ? new \DateTime($widget['end'], $timezone) : null;
|
|
||||||
$theUser = $widget['user'] ? $user : null;
|
|
||||||
|
|
||||||
if (!isset($widget['type'])) {
|
if (!isset($widget['type'])) {
|
||||||
@trigger_error('Using a widget definition without a "type" is deprecated', E_USER_DEPRECATED);
|
@trigger_error('Using a widget definition without a "type" is deprecated', E_USER_DEPRECATED);
|
||||||
$widget['type'] = Counter::class;
|
$widget['type'] = Counter::class;
|
||||||
@@ -104,30 +86,25 @@ class WidgetRepository
|
|||||||
throw new WidgetException(sprintf('Unknown widget type "%s"', $widgetClassName));
|
throw new WidgetException(sprintf('Unknown widget type "%s"', $widgetClassName));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var AbstractWidgetType $model */
|
/** @var SimpleStatisticChart $model */
|
||||||
$model = new $widgetClassName();
|
$model = new $widgetClassName($this->repository);
|
||||||
if (!($model instanceof AbstractWidgetType)) {
|
if (!($model instanceof SimpleStatisticChart)) {
|
||||||
throw new WidgetException(
|
throw new WidgetException(
|
||||||
sprintf(
|
sprintf(
|
||||||
'Widget type "%s" is not an instance of "%s"',
|
'Widget type "%s" is not an instance of "%s"',
|
||||||
$widgetClassName,
|
$widgetClassName,
|
||||||
AbstractWidgetType::class
|
SimpleStatisticChart::class
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
$data = $this->repository->getStatistic($widget['query'], $begin, $end, $theUser);
|
|
||||||
} catch (\Exception $ex) {
|
|
||||||
throw new WidgetException(
|
|
||||||
'Failed loading widget data: ' . $ex->getMessage()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$model
|
$model
|
||||||
|
->setQuery($widget['query'])
|
||||||
|
->setBegin($widget['begin'])
|
||||||
|
->setEnd($widget['end'])
|
||||||
->setId($name)
|
->setId($name)
|
||||||
->setTitle($widget['title'])
|
->setTitle($widget['title'])
|
||||||
->setData($data);
|
;
|
||||||
|
|
||||||
if ($widget['query'] == TimesheetRepository::STATS_QUERY_DURATION) {
|
if ($widget['query'] == TimesheetRepository::STATS_QUERY_DURATION) {
|
||||||
$model->setOption('dataType', 'duration');
|
$model->setOption('dataType', 'duration');
|
||||||
|
|||||||
@@ -9,75 +9,19 @@
|
|||||||
|
|
||||||
namespace App\Twig;
|
namespace App\Twig;
|
||||||
|
|
||||||
use App\Entity\User;
|
use App\Twig\Runtime\ThemeEventExtension;
|
||||||
use App\Event\ThemeEvent;
|
|
||||||
use App\Security\CurrentUser;
|
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
||||||
use Twig\Extension\AbstractExtension;
|
use Twig\Extension\AbstractExtension;
|
||||||
use Twig\TwigFunction;
|
use Twig\TwigFunction;
|
||||||
|
|
||||||
class EventExtensions extends AbstractExtension
|
class EventExtensions extends AbstractExtension
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var EventDispatcherInterface
|
|
||||||
*/
|
|
||||||
protected $eventDispatcher;
|
|
||||||
/**
|
|
||||||
* @var User
|
|
||||||
*/
|
|
||||||
protected $user;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param EventDispatcherInterface $dispatcher
|
|
||||||
* @param CurrentUser $user
|
|
||||||
*/
|
|
||||||
public function __construct(EventDispatcherInterface $dispatcher, CurrentUser $user)
|
|
||||||
{
|
|
||||||
$this->eventDispatcher = $dispatcher;
|
|
||||||
$this->user = $user->getUser();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getFunctions()
|
public function getFunctions()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
new TwigFunction('trigger', [$this, 'triggerEvent']),
|
new TwigFunction('trigger', [ThemeEventExtension::class, 'trigger']),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return EventDispatcherInterface
|
|
||||||
*/
|
|
||||||
protected function getDispatcher()
|
|
||||||
{
|
|
||||||
return $this->eventDispatcher;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $eventName
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
protected function hasListener($eventName)
|
|
||||||
{
|
|
||||||
return $this->getDispatcher()->hasListeners($eventName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $eventName
|
|
||||||
* @param mixed $payload
|
|
||||||
* @return ThemeEvent
|
|
||||||
*/
|
|
||||||
public function triggerEvent(string $eventName, $payload = null)
|
|
||||||
{
|
|
||||||
$themeEvent = new ThemeEvent($this->user, $payload);
|
|
||||||
|
|
||||||
if ($this->hasListener($eventName)) {
|
|
||||||
$this->getDispatcher()->dispatch($themeEvent, $eventName);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $themeEvent;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ final class MarkdownExtension extends AbstractExtension
|
|||||||
/**
|
/**
|
||||||
* Transforms the entities comment (customer, project, activity ...) into HTML.
|
* Transforms the entities comment (customer, project, activity ...) into HTML.
|
||||||
*
|
*
|
||||||
* @param string $content
|
* @param string|null $content
|
||||||
* @param bool $fullLength
|
* @param bool $fullLength
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
|
|||||||
49
src/Twig/Runtime/ThemeEventExtension.php
Normal file
49
src/Twig/Runtime/ThemeEventExtension.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?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\Twig\Runtime;
|
||||||
|
|
||||||
|
use App\Event\ThemeEvent;
|
||||||
|
use App\Security\CurrentUser;
|
||||||
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
|
use Twig\Extension\RuntimeExtensionInterface;
|
||||||
|
|
||||||
|
final class ThemeEventExtension implements RuntimeExtensionInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var EventDispatcherInterface
|
||||||
|
*/
|
||||||
|
private $eventDispatcher;
|
||||||
|
/**
|
||||||
|
* @var CurrentUser
|
||||||
|
*/
|
||||||
|
private $user;
|
||||||
|
|
||||||
|
public function __construct(EventDispatcherInterface $dispatcher, CurrentUser $user)
|
||||||
|
{
|
||||||
|
$this->eventDispatcher = $dispatcher;
|
||||||
|
$this->user = $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $eventName
|
||||||
|
* @param mixed|null $payload
|
||||||
|
* @return ThemeEvent
|
||||||
|
*/
|
||||||
|
public function trigger(string $eventName, $payload = null): ThemeEvent
|
||||||
|
{
|
||||||
|
$themeEvent = new ThemeEvent($this->user->getUser(), $payload);
|
||||||
|
|
||||||
|
if ($this->eventDispatcher->hasListeners($eventName)) {
|
||||||
|
$this->eventDispatcher->dispatch($themeEvent, $eventName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $themeEvent;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,7 +30,7 @@ abstract class AbstractWidgetType implements WidgetInterface
|
|||||||
*/
|
*/
|
||||||
protected $data;
|
protected $data;
|
||||||
|
|
||||||
public function setId(string $id): AbstractWidgetType
|
public function setId(string $id): self
|
||||||
{
|
{
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ abstract class AbstractWidgetType implements WidgetInterface
|
|||||||
return $this->id;
|
return $this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setData($data): AbstractWidgetType
|
public function setData($data): self
|
||||||
{
|
{
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ abstract class AbstractWidgetType implements WidgetInterface
|
|||||||
return $this->data;
|
return $this->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTitle(string $title): AbstractWidgetType
|
public function setTitle(string $title): self
|
||||||
{
|
{
|
||||||
$this->title = $title;
|
$this->title = $title;
|
||||||
|
|
||||||
@@ -70,7 +70,7 @@ abstract class AbstractWidgetType implements WidgetInterface
|
|||||||
return $this->title;
|
return $this->title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setOptions(array $options): AbstractWidgetType
|
public function setOptions(array $options): self
|
||||||
{
|
{
|
||||||
foreach ($options as $key => $value) {
|
foreach ($options as $key => $value) {
|
||||||
$this->options[$key] = $value;
|
$this->options[$key] = $value;
|
||||||
|
|||||||
@@ -9,10 +9,13 @@
|
|||||||
|
|
||||||
namespace App\Widget\Type;
|
namespace App\Widget\Type;
|
||||||
|
|
||||||
class Counter extends SimpleWidget
|
use App\Repository\TimesheetRepository;
|
||||||
|
|
||||||
|
final class Counter extends SimpleStatisticChart
|
||||||
{
|
{
|
||||||
public function __construct()
|
public function __construct(TimesheetRepository $repository)
|
||||||
{
|
{
|
||||||
|
parent::__construct($repository);
|
||||||
$this->setOption('dataType', 'int');
|
$this->setOption('dataType', 'int');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,12 +11,11 @@ namespace App\Widget\Type;
|
|||||||
|
|
||||||
use App\Entity\Activity;
|
use App\Entity\Activity;
|
||||||
use App\Entity\Project;
|
use App\Entity\Project;
|
||||||
|
use App\Entity\User;
|
||||||
use App\Repository\TimesheetRepository;
|
use App\Repository\TimesheetRepository;
|
||||||
use App\Security\CurrentUser;
|
|
||||||
use App\Timesheet\UserDateTimeFactory;
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
|
||||||
class DailyWorkingTimeChart extends SimpleWidget
|
class DailyWorkingTimeChart extends SimpleWidget implements UserWidget
|
||||||
{
|
{
|
||||||
public const DEFAULT_CHART = 'bar';
|
public const DEFAULT_CHART = 'bar';
|
||||||
|
|
||||||
@@ -24,27 +23,26 @@ class DailyWorkingTimeChart extends SimpleWidget
|
|||||||
* @var TimesheetRepository
|
* @var TimesheetRepository
|
||||||
*/
|
*/
|
||||||
protected $repository;
|
protected $repository;
|
||||||
/**
|
|
||||||
* @var UserDateTimeFactory
|
|
||||||
*/
|
|
||||||
private $dateTimeFactory;
|
|
||||||
|
|
||||||
public function __construct(TimesheetRepository $repository, CurrentUser $user, UserDateTimeFactory $dateTime)
|
public function __construct(TimesheetRepository $repository)
|
||||||
{
|
{
|
||||||
$this->repository = $repository;
|
$this->repository = $repository;
|
||||||
$this->dateTimeFactory = $dateTime;
|
|
||||||
$this->setId('DailyWorkingTimeChart');
|
$this->setId('DailyWorkingTimeChart');
|
||||||
$this->setTitle('stats.yourWorkingHours');
|
$this->setTitle('stats.yourWorkingHours');
|
||||||
$this->setOptions([
|
$this->setOptions([
|
||||||
'begin' => 'monday this week 00:00:00',
|
'begin' => 'monday this week 00:00:00',
|
||||||
'end' => 'sunday this week 23:59:59',
|
'end' => 'sunday this week 23:59:59',
|
||||||
'color' => '',
|
'color' => '',
|
||||||
'user' => $user->getUser(),
|
|
||||||
'type' => self::DEFAULT_CHART,
|
'type' => self::DEFAULT_CHART,
|
||||||
'id' => '',
|
'id' => '',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setUser(User $user): void
|
||||||
|
{
|
||||||
|
$this->setOption('user', $user);
|
||||||
|
}
|
||||||
|
|
||||||
public function getOptions(array $options = []): array
|
public function getOptions(array $options = []): array
|
||||||
{
|
{
|
||||||
$options = parent::getOptions($options);
|
$options = parent::getOptions($options);
|
||||||
@@ -65,16 +63,20 @@ class DailyWorkingTimeChart extends SimpleWidget
|
|||||||
$options = $this->getOptions($options);
|
$options = $this->getOptions($options);
|
||||||
|
|
||||||
$user = $options['user'];
|
$user = $options['user'];
|
||||||
|
if (null === $user || !($user instanceof User)) {
|
||||||
|
throw new \InvalidArgumentException('Widget option "user" must be an instance of ' . User::class);
|
||||||
|
}
|
||||||
|
|
||||||
if ($options['begin'] instanceof DateTime) {
|
if ($options['begin'] instanceof DateTime) {
|
||||||
$begin = $options['begin'];
|
$begin = $options['begin'];
|
||||||
} else {
|
} else {
|
||||||
$begin = new DateTime($options['begin'], $this->dateTimeFactory->getTimezone());
|
$begin = new DateTime($options['begin'], new \DateTimeZone($user->getTimezone()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($options['end'] instanceof DateTime) {
|
if ($options['end'] instanceof DateTime) {
|
||||||
$end = $options['end'];
|
$end = $options['end'];
|
||||||
} else {
|
} else {
|
||||||
$end = new DateTime($options['end'], $this->dateTimeFactory->getTimezone());
|
$end = new DateTime($options['end'], new \DateTimeZone($user->getTimezone()));
|
||||||
}
|
}
|
||||||
|
|
||||||
$activities = [];
|
$activities = [];
|
||||||
|
|||||||
@@ -9,37 +9,40 @@
|
|||||||
|
|
||||||
namespace App\Widget\Type;
|
namespace App\Widget\Type;
|
||||||
|
|
||||||
|
use App\Entity\User;
|
||||||
use App\Repository\TimesheetRepository;
|
use App\Repository\TimesheetRepository;
|
||||||
use App\Security\CurrentUser;
|
|
||||||
use App\Timesheet\UserDateTimeFactory;
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
|
||||||
final class PaginatedWorkingTimeChart extends SimpleWidget
|
final class PaginatedWorkingTimeChart extends SimpleWidget implements UserWidget
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var TimesheetRepository
|
* @var TimesheetRepository
|
||||||
*/
|
*/
|
||||||
private $repository;
|
private $repository;
|
||||||
/**
|
|
||||||
* @var UserDateTimeFactory
|
|
||||||
*/
|
|
||||||
private $dateTimeFactory;
|
|
||||||
|
|
||||||
public function __construct(TimesheetRepository $repository, CurrentUser $user, UserDateTimeFactory $dateTime)
|
public function __construct(TimesheetRepository $repository)
|
||||||
{
|
{
|
||||||
$this->repository = $repository;
|
$this->repository = $repository;
|
||||||
$this->dateTimeFactory = $dateTime;
|
|
||||||
$this->setId('PaginatedWorkingTimeChart');
|
$this->setId('PaginatedWorkingTimeChart');
|
||||||
$this->setTitle('stats.yourWorkingHours');
|
$this->setTitle('stats.yourWorkingHours');
|
||||||
|
|
||||||
$this->setOptions([
|
$this->setOptions([
|
||||||
'year' => (new DateTime('now', $this->dateTimeFactory->getTimezone()))->format('Y'),
|
'year' => (new DateTime('now'))->format('Y'),
|
||||||
'week' => (new DateTime('now', $this->dateTimeFactory->getTimezone()))->format('W'),
|
'week' => (new DateTime('now'))->format('W'),
|
||||||
'user' => $user->getUser(),
|
|
||||||
'type' => 'bar',
|
'type' => 'bar',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setUser(User $user): void
|
||||||
|
{
|
||||||
|
$this->setOption('user', $user);
|
||||||
|
$now = new DateTime('now', new \DateTimeZone($user->getTimezone()));
|
||||||
|
$this->setOptions([
|
||||||
|
'year' => $now->format('Y'),
|
||||||
|
'week' => $now->format('W'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
public function getOptions(array $options = []): array
|
public function getOptions(array $options = []): array
|
||||||
{
|
{
|
||||||
$options = parent::getOptions($options);
|
$options = parent::getOptions($options);
|
||||||
@@ -51,9 +54,9 @@ final class PaginatedWorkingTimeChart extends SimpleWidget
|
|||||||
return $options;
|
return $options;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getDate($year, $week, $day, $hour, $minute, $second)
|
private function getDate(\DateTimeZone $timezone, $year, $week, $day, $hour, $minute, $second)
|
||||||
{
|
{
|
||||||
$now = new DateTime('now', $this->dateTimeFactory->getTimezone());
|
$now = new DateTime('now', $timezone);
|
||||||
$now->setISODate($year, $week, $day);
|
$now->setISODate($year, $week, $day);
|
||||||
$now->setTime($hour, $minute, $second);
|
$now->setTime($hour, $minute, $second);
|
||||||
|
|
||||||
@@ -63,10 +66,16 @@ final class PaginatedWorkingTimeChart extends SimpleWidget
|
|||||||
public function getData(array $options = [])
|
public function getData(array $options = [])
|
||||||
{
|
{
|
||||||
$options = $this->getOptions($options);
|
$options = $this->getOptions($options);
|
||||||
$user = $options['user'];
|
|
||||||
|
|
||||||
$weekBegin = $this->getDate($options['year'], $options['week'], 1, 0, 0, 0);
|
$user = $options['user'];
|
||||||
$weekEnd = $this->getDate($options['year'], $options['week'], 7, 23, 59, 59);
|
if (null === $user || !($user instanceof User)) {
|
||||||
|
throw new \InvalidArgumentException('Widget option "user" must be an instance of ' . User::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
$timezone = new \DateTimeZone($user->getTimezone());
|
||||||
|
|
||||||
|
$weekBegin = $this->getDate($timezone, $options['year'], $options['week'], 1, 0, 0, 0);
|
||||||
|
$weekEnd = $this->getDate($timezone, $options['year'], $options['week'], 7, 23, 59, 59);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'begin' => clone $weekBegin,
|
'begin' => clone $weekBegin,
|
||||||
@@ -74,8 +83,8 @@ final class PaginatedWorkingTimeChart extends SimpleWidget
|
|||||||
'stats' => $this->repository->getDailyStats($user, $weekBegin, $weekEnd),
|
'stats' => $this->repository->getDailyStats($user, $weekBegin, $weekEnd),
|
||||||
'day' => $this->repository->getStatistic(
|
'day' => $this->repository->getStatistic(
|
||||||
'duration',
|
'duration',
|
||||||
new DateTime('00:00:00', $this->dateTimeFactory->getTimezone()),
|
new DateTime('00:00:00', $timezone),
|
||||||
new DateTime('23:59:59', $this->dateTimeFactory->getTimezone()),
|
new DateTime('23:59:59', $timezone),
|
||||||
$user
|
$user
|
||||||
),
|
),
|
||||||
'week' => $this->repository->getStatistic(
|
'week' => $this->repository->getStatistic(
|
||||||
@@ -92,8 +101,8 @@ final class PaginatedWorkingTimeChart extends SimpleWidget
|
|||||||
),
|
),
|
||||||
'year' => $this->repository->getStatistic(
|
'year' => $this->repository->getStatistic(
|
||||||
'duration',
|
'duration',
|
||||||
new DateTime(sprintf('01 january %s 00:00:00', $options['year']), $this->dateTimeFactory->getTimezone()),
|
new DateTime(sprintf('01 january %s 00:00:00', $options['year']), $timezone),
|
||||||
new DateTime(sprintf('31 december %s 23:59:59', $options['year']), $this->dateTimeFactory->getTimezone()),
|
new DateTime(sprintf('31 december %s 23:59:59', $options['year']), $timezone),
|
||||||
$user
|
$user
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|||||||
101
src/Widget/Type/SimpleStatisticChart.php
Normal file
101
src/Widget/Type/SimpleStatisticChart.php
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
<?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\Widget\Type;
|
||||||
|
|
||||||
|
use App\Entity\User;
|
||||||
|
use App\Repository\TimesheetRepository;
|
||||||
|
use App\Widget\WidgetException;
|
||||||
|
|
||||||
|
class SimpleStatisticChart extends SimpleWidget
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var TimesheetRepository
|
||||||
|
*/
|
||||||
|
private $repository;
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $query;
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $begin;
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $end;
|
||||||
|
/**
|
||||||
|
* @var User
|
||||||
|
*/
|
||||||
|
private $user;
|
||||||
|
|
||||||
|
public function __construct(TimesheetRepository $repository)
|
||||||
|
{
|
||||||
|
$this->repository = $repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setQuery(string $query): SimpleStatisticChart
|
||||||
|
{
|
||||||
|
$this->query = $query;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setBegin(?string $begin): SimpleStatisticChart
|
||||||
|
{
|
||||||
|
$this->begin = $begin;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setEnd(?string $end): SimpleStatisticChart
|
||||||
|
{
|
||||||
|
$this->end = $end;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setUser(User $user): SimpleStatisticChart
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setData($data): AbstractWidgetType
|
||||||
|
{
|
||||||
|
throw new \InvalidArgumentException('Cannot set data on instances of SimpleStatisticChart');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $options
|
||||||
|
* @return mixed|null
|
||||||
|
* @throws WidgetException
|
||||||
|
*/
|
||||||
|
public function getData(array $options = [])
|
||||||
|
{
|
||||||
|
$timezone = date_default_timezone_get();
|
||||||
|
if (null !== $this->user) {
|
||||||
|
$timezone = $this->user->getTimezone();
|
||||||
|
}
|
||||||
|
$timezone = new \DateTimeZone($timezone);
|
||||||
|
|
||||||
|
$begin = !empty($this->begin) ? new \DateTime($this->begin, $timezone) : null;
|
||||||
|
$end = !empty($this->end) ? new \DateTime($this->end, $timezone) : null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
return $this->repository->getStatistic($this->query, $begin, $end, $this->user);
|
||||||
|
} catch (\Exception $ex) {
|
||||||
|
throw new WidgetException(
|
||||||
|
'Failed loading widget data: ' . $ex->getMessage()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,23 +13,19 @@ use App\Entity\Project;
|
|||||||
use App\Entity\Team;
|
use App\Entity\Team;
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use App\Repository\ProjectRepository;
|
use App\Repository\ProjectRepository;
|
||||||
use App\Security\CurrentUser;
|
|
||||||
|
|
||||||
class UserTeamProjects extends SimpleWidget implements AuthorizedWidget
|
class UserTeamProjects extends SimpleWidget implements AuthorizedWidget, UserWidget
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var ProjectRepository
|
* @var ProjectRepository
|
||||||
*/
|
*/
|
||||||
private $repository;
|
private $repository;
|
||||||
|
|
||||||
public function __construct(CurrentUser $user, ProjectRepository $repository)
|
public function __construct(ProjectRepository $repository)
|
||||||
{
|
{
|
||||||
$this->setId('UserTeamProjects');
|
$this->setId('UserTeamProjects');
|
||||||
$this->setTitle('label.my_team_projects');
|
$this->setTitle('label.my_team_projects');
|
||||||
$this->setOptions([
|
$this->setOption('id', '');
|
||||||
'user' => $user->getUser(),
|
|
||||||
'id' => '',
|
|
||||||
]);
|
|
||||||
$this->repository = $repository;
|
$this->repository = $repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,4 +76,9 @@ class UserTeamProjects extends SimpleWidget implements AuthorizedWidget
|
|||||||
{
|
{
|
||||||
return ['budget_team_project', 'budget_teamlead_project', 'budget_project'];
|
return ['budget_team_project', 'budget_teamlead_project', 'budget_project'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setUser(User $user): void
|
||||||
|
{
|
||||||
|
$this->setOption('user', $user);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,18 +10,14 @@
|
|||||||
namespace App\Widget\Type;
|
namespace App\Widget\Type;
|
||||||
|
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use App\Security\CurrentUser;
|
|
||||||
|
|
||||||
class UserTeams extends SimpleWidget implements AuthorizedWidget
|
class UserTeams extends SimpleWidget implements AuthorizedWidget, UserWidget
|
||||||
{
|
{
|
||||||
public function __construct(CurrentUser $user)
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->setId('UserTeams');
|
$this->setId('UserTeams');
|
||||||
$this->setTitle('label.my_teams');
|
$this->setTitle('label.my_teams');
|
||||||
$this->setOptions([
|
$this->setOption('id', '');
|
||||||
'user' => $user->getUser(),
|
|
||||||
'id' => '',
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getOptions(array $options = []): array
|
public function getOptions(array $options = []): array
|
||||||
@@ -51,4 +47,9 @@ class UserTeams extends SimpleWidget implements AuthorizedWidget
|
|||||||
{
|
{
|
||||||
return ['view_team_member', 'view_team'];
|
return ['view_team_member', 'view_team'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setUser(User $user): void
|
||||||
|
{
|
||||||
|
$this->setOption('user', $user);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
22
src/Widget/Type/UserWidget.php
Normal file
22
src/Widget/Type/UserWidget.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?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\Widget\Type;
|
||||||
|
|
||||||
|
use App\Entity\User;
|
||||||
|
|
||||||
|
interface UserWidget
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Sets the current user.
|
||||||
|
*
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function setUser(User $user): void;
|
||||||
|
}
|
||||||
@@ -9,6 +9,6 @@
|
|||||||
|
|
||||||
namespace App\Widget\Type;
|
namespace App\Widget\Type;
|
||||||
|
|
||||||
class YearChart extends SimpleWidget
|
final class YearChart extends SimpleStatisticChart
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,11 +16,11 @@ class WidgetService
|
|||||||
/**
|
/**
|
||||||
* @var WidgetRendererInterface[]
|
* @var WidgetRendererInterface[]
|
||||||
*/
|
*/
|
||||||
protected $renderer = [];
|
private $renderer = [];
|
||||||
/**
|
/**
|
||||||
* @var WidgetRepository
|
* @var WidgetRepository
|
||||||
*/
|
*/
|
||||||
protected $repository;
|
private $repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param WidgetRepository $repository
|
* @param WidgetRepository $repository
|
||||||
@@ -34,10 +34,6 @@ class WidgetService
|
|||||||
$this->repository = $repository;
|
$this->repository = $repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $widget
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function hasWidget(string $widget): bool
|
public function hasWidget(string $widget): bool
|
||||||
{
|
{
|
||||||
return $this->repository->has($widget);
|
return $this->repository->has($widget);
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
{{ render_widget('PaginatedWorkingTimeChart', {'year': year, 'week': week}) }}
|
{{ render_widget('PaginatedWorkingTimeChart', {'user': user, 'year': year, 'week': week}) }}
|
||||||
|
|||||||
34
tests/Controller/WidgetControllerTest.php
Normal file
34
tests/Controller/WidgetControllerTest.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?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\Controller;
|
||||||
|
|
||||||
|
use App\Entity\User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group integration
|
||||||
|
*/
|
||||||
|
class WidgetControllerTest extends ControllerBaseTest
|
||||||
|
{
|
||||||
|
public function testIsSecure()
|
||||||
|
{
|
||||||
|
$this->assertUrlIsSecured('/widgets/working-time/2020/1');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWorkingtimechartAction()
|
||||||
|
{
|
||||||
|
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
|
||||||
|
$this->assertAccessIsGranted($client, '/widgets/working-time/2020/1');
|
||||||
|
|
||||||
|
$content = $client->getResponse()->getContent();
|
||||||
|
self::assertStringContainsString('id="PaginatedWorkingTimeChart"', $content);
|
||||||
|
self::assertStringContainsString('myChart = new Chart', $content);
|
||||||
|
self::assertStringContainsString("KimaiPaginatedBoxWidget.create('#PaginatedWorkingTimeChart');", $content);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,10 +9,8 @@
|
|||||||
|
|
||||||
namespace App\Tests\Repository;
|
namespace App\Tests\Repository;
|
||||||
|
|
||||||
use App\Entity\User;
|
|
||||||
use App\Repository\TimesheetRepository;
|
use App\Repository\TimesheetRepository;
|
||||||
use App\Repository\WidgetRepository;
|
use App\Repository\WidgetRepository;
|
||||||
use App\Tests\Mocks\Security\CurrentUserFactory;
|
|
||||||
use App\Widget\Type\CompoundChart;
|
use App\Widget\Type\CompoundChart;
|
||||||
use App\Widget\Type\Counter;
|
use App\Widget\Type\Counter;
|
||||||
use App\Widget\WidgetException;
|
use App\Widget\WidgetException;
|
||||||
@@ -26,9 +24,8 @@ class WidgetRepositoryTest extends TestCase
|
|||||||
public function testHasWidget()
|
public function testHasWidget()
|
||||||
{
|
{
|
||||||
$repoMock = $this->createMock(TimesheetRepository::class);
|
$repoMock = $this->createMock(TimesheetRepository::class);
|
||||||
$userMock = (new CurrentUserFactory($this))->create(new User());
|
|
||||||
|
|
||||||
$sut = new WidgetRepository($repoMock, $userMock, ['test' => []]);
|
$sut = new WidgetRepository($repoMock, ['test' => []]);
|
||||||
|
|
||||||
$this->assertFalse($sut->has('foo'));
|
$this->assertFalse($sut->has('foo'));
|
||||||
$this->assertTrue($sut->has('test'));
|
$this->assertTrue($sut->has('test'));
|
||||||
@@ -40,9 +37,8 @@ class WidgetRepositoryTest extends TestCase
|
|||||||
$this->expectExceptionMessage('Cannot find widget "foo".');
|
$this->expectExceptionMessage('Cannot find widget "foo".');
|
||||||
|
|
||||||
$repoMock = $this->createMock(TimesheetRepository::class);
|
$repoMock = $this->createMock(TimesheetRepository::class);
|
||||||
$userMock = (new CurrentUserFactory($this))->create(new User());
|
|
||||||
|
|
||||||
$sut = new WidgetRepository($repoMock, $userMock, ['test' => []]);
|
$sut = new WidgetRepository($repoMock, ['test' => []]);
|
||||||
$sut->get('foo');
|
$sut->get('foo');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,21 +48,19 @@ class WidgetRepositoryTest extends TestCase
|
|||||||
$this->expectExceptionMessage('Unknown widget type "FooBar"');
|
$this->expectExceptionMessage('Unknown widget type "FooBar"');
|
||||||
|
|
||||||
$repoMock = $this->createMock(TimesheetRepository::class);
|
$repoMock = $this->createMock(TimesheetRepository::class);
|
||||||
$userMock = (new CurrentUserFactory($this))->create(new User());
|
|
||||||
|
|
||||||
$sut = new WidgetRepository($repoMock, $userMock, ['test' => ['type' => 'FooBar', 'user' => false]]);
|
$sut = new WidgetRepository($repoMock, ['test' => ['type' => 'FooBar', 'user' => false]]);
|
||||||
$sut->get('test');
|
$sut->get('test');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetWidgetTriggersExceptionOnWrongClass()
|
public function testGetWidgetTriggersExceptionOnWrongClass()
|
||||||
{
|
{
|
||||||
$this->expectException(WidgetException::class);
|
$this->expectException(WidgetException::class);
|
||||||
$this->expectExceptionMessage('Widget type "App\Widget\Type\CompoundChart" is not an instance of "App\Widget\Type\AbstractWidgetType"');
|
$this->expectExceptionMessage('Widget type "App\Widget\Type\CompoundChart" is not an instance of "App\Widget\Type\SimpleStatisticChart"');
|
||||||
|
|
||||||
$repoMock = $this->createMock(TimesheetRepository::class);
|
$repoMock = $this->createMock(TimesheetRepository::class);
|
||||||
$userMock = (new CurrentUserFactory($this))->create(new User());
|
|
||||||
|
|
||||||
$sut = new WidgetRepository($repoMock, $userMock, ['test' => ['type' => CompoundChart::class, 'user' => false]]);
|
$sut = new WidgetRepository($repoMock, ['test' => ['type' => CompoundChart::class, 'user' => false]]);
|
||||||
$sut->get('test');
|
$sut->get('test');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,8 +72,6 @@ class WidgetRepositoryTest extends TestCase
|
|||||||
$repoMock = $this->createMock(TimesheetRepository::class);
|
$repoMock = $this->createMock(TimesheetRepository::class);
|
||||||
$repoMock->method('getStatistic')->willReturn($data);
|
$repoMock->method('getStatistic')->willReturn($data);
|
||||||
|
|
||||||
$userMock = (new CurrentUserFactory($this))->create(new User());
|
|
||||||
|
|
||||||
$widget = [
|
$widget = [
|
||||||
'color' => 'sunny',
|
'color' => 'sunny',
|
||||||
'icon' => 'far fa-test',
|
'icon' => 'far fa-test',
|
||||||
@@ -91,7 +83,7 @@ class WidgetRepositoryTest extends TestCase
|
|||||||
'type' => Counter::class,
|
'type' => Counter::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
$sut = new WidgetRepository($repoMock, $userMock, ['test' => $widget]);
|
$sut = new WidgetRepository($repoMock, ['test' => $widget]);
|
||||||
$widget = $sut->get('test');
|
$widget = $sut->get('test');
|
||||||
|
|
||||||
$options = $widget->getOptions();
|
$options = $widget->getOptions();
|
||||||
|
|||||||
39
tests/Twig/EventExtensionsTest.php
Normal file
39
tests/Twig/EventExtensionsTest.php
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
48
tests/Twig/Runtime/ThemeEventExtensionTest.php
Normal file
48
tests/Twig/Runtime/ThemeEventExtensionTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
namespace App\Tests\Twig;
|
namespace App\Tests\Twig;
|
||||||
|
|
||||||
use App\Twig\WidgetExtension;
|
use App\Twig\WidgetExtension;
|
||||||
use App\Widget\Type\Counter;
|
use App\Widget\Type\More;
|
||||||
use App\Widget\WidgetInterface;
|
use App\Widget\WidgetInterface;
|
||||||
use App\Widget\WidgetRendererInterface;
|
use App\Widget\WidgetRendererInterface;
|
||||||
use App\Widget\WidgetService;
|
use App\Widget\WidgetService;
|
||||||
@@ -72,7 +72,7 @@ class WidgetExtensionTest extends TestCase
|
|||||||
|
|
||||||
public function testRenderWidgetByString()
|
public function testRenderWidgetByString()
|
||||||
{
|
{
|
||||||
$widget = new Counter();
|
$widget = new More();
|
||||||
$sut = $this->getSut(true, $widget, new TestRenderer());
|
$sut = $this->getSut(true, $widget, new TestRenderer());
|
||||||
$options = ['foo' => 'bar', 'dataType' => 'blub'];
|
$options = ['foo' => 'bar', 'dataType' => 'blub'];
|
||||||
$result = $sut->renderWidget('test', $options);
|
$result = $sut->renderWidget('test', $options);
|
||||||
@@ -82,7 +82,7 @@ class WidgetExtensionTest extends TestCase
|
|||||||
|
|
||||||
public function testRenderWidgetObject()
|
public function testRenderWidgetObject()
|
||||||
{
|
{
|
||||||
$widget = new Counter();
|
$widget = new More();
|
||||||
$sut = $this->getSut(null, null, new TestRenderer());
|
$sut = $this->getSut(null, null, new TestRenderer());
|
||||||
$options = ['foo' => 'bar', 'dataType' => 'blub'];
|
$options = ['foo' => 'bar', 'dataType' => 'blub'];
|
||||||
$result = $sut->renderWidget($widget, $options);
|
$result = $sut->renderWidget($widget, $options);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace App\Tests\Widget\Renderer;
|
|||||||
use App\Widget\Renderer\CompoundChartRenderer;
|
use App\Widget\Renderer\CompoundChartRenderer;
|
||||||
use App\Widget\Type\CompoundChart;
|
use App\Widget\Type\CompoundChart;
|
||||||
use App\Widget\Type\CompoundRow;
|
use App\Widget\Type\CompoundRow;
|
||||||
use App\Widget\Type\Counter;
|
use App\Widget\Type\More;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Twig\Environment;
|
use Twig\Environment;
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ class CompoundChartRendererTest extends TestCase
|
|||||||
$sut = new CompoundChartRenderer($twig);
|
$sut = new CompoundChartRenderer($twig);
|
||||||
$row = new CompoundChart();
|
$row = new CompoundChart();
|
||||||
$row->setTitle('foo-bar');
|
$row->setTitle('foo-bar');
|
||||||
$row->addWidget(new Counter());
|
$row->addWidget(new More());
|
||||||
|
|
||||||
$result = $sut->render($row);
|
$result = $sut->render($row);
|
||||||
$result = json_decode($result, true);
|
$result = json_decode($result, true);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace App\Tests\Widget\Renderer;
|
|||||||
use App\Widget\Renderer\CompoundRowRenderer;
|
use App\Widget\Renderer\CompoundRowRenderer;
|
||||||
use App\Widget\Type\CompoundChart;
|
use App\Widget\Type\CompoundChart;
|
||||||
use App\Widget\Type\CompoundRow;
|
use App\Widget\Type\CompoundRow;
|
||||||
use App\Widget\Type\Counter;
|
use App\Widget\Type\More;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Twig\Environment;
|
use Twig\Environment;
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ class CompoundRowRendererTest extends TestCase
|
|||||||
$sut = new CompoundRowRenderer($twig);
|
$sut = new CompoundRowRenderer($twig);
|
||||||
$row = new CompoundRow();
|
$row = new CompoundRow();
|
||||||
$row->setTitle('foo-bar');
|
$row->setTitle('foo-bar');
|
||||||
$row->addWidget(new Counter());
|
$row->addWidget(new More());
|
||||||
|
|
||||||
$result = $sut->render($row);
|
$result = $sut->render($row);
|
||||||
$result = json_decode($result, true);
|
$result = json_decode($result, true);
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
namespace App\Tests\Widget\Renderer;
|
namespace App\Tests\Widget\Renderer;
|
||||||
|
|
||||||
use App\Widget\Renderer\SimpleWidgetRenderer;
|
use App\Widget\Renderer\SimpleWidgetRenderer;
|
||||||
use App\Widget\Type\Counter;
|
|
||||||
use App\Widget\Type\More;
|
use App\Widget\Type\More;
|
||||||
use App\Widget\Type\SimpleWidget;
|
use App\Widget\Type\SimpleWidget;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
@@ -60,7 +59,6 @@ class SimpleWidgetRendererTest extends TestCase
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
[new SimpleWidget(), 'widget/widget-simplewidget.html.twig', 'yellow'],
|
[new SimpleWidget(), 'widget/widget-simplewidget.html.twig', 'yellow'],
|
||||||
[new Counter(), 'widget/widget-counter.html.twig', 'asdfgh'],
|
|
||||||
[new More(), 'widget/widget-more.html.twig', '#123456'],
|
[new More(), 'widget/widget-more.html.twig', '#123456'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
26
tests/Widget/Type/AbstractSimpleStatisticsWidgetTypeTest.php
Normal file
26
tests/Widget/Type/AbstractSimpleStatisticsWidgetTypeTest.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?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\Widget\Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \App\Widget\Type\SimpleStatisticChart
|
||||||
|
*/
|
||||||
|
abstract class AbstractSimpleStatisticsWidgetTypeTest extends AbstractWidgetTypeTest
|
||||||
|
{
|
||||||
|
public function testData()
|
||||||
|
{
|
||||||
|
$sut = $this->createSut();
|
||||||
|
|
||||||
|
$this->expectException(\InvalidArgumentException::class);
|
||||||
|
$this->expectExceptionMessage('Cannot set data on instances of SimpleStatisticChart');
|
||||||
|
|
||||||
|
$sut->setData(10);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,7 +38,6 @@ abstract class AbstractWidgetTypeTest extends TestCase
|
|||||||
self::assertInstanceOf(AbstractWidgetType::class, $sut->setOptions([]));
|
self::assertInstanceOf(AbstractWidgetType::class, $sut->setOptions([]));
|
||||||
self::assertInstanceOf(AbstractWidgetType::class, $sut->setId(''));
|
self::assertInstanceOf(AbstractWidgetType::class, $sut->setId(''));
|
||||||
self::assertInstanceOf(AbstractWidgetType::class, $sut->setTitle(''));
|
self::assertInstanceOf(AbstractWidgetType::class, $sut->setTitle(''));
|
||||||
self::assertInstanceOf(AbstractWidgetType::class, $sut->setData(''));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetter()
|
public function testSetter()
|
||||||
@@ -57,8 +56,14 @@ abstract class AbstractWidgetTypeTest extends TestCase
|
|||||||
// id
|
// id
|
||||||
$sut->setId('cvbnmyx');
|
$sut->setId('cvbnmyx');
|
||||||
self::assertEquals('cvbnmyx', $sut->getId());
|
self::assertEquals('cvbnmyx', $sut->getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testData()
|
||||||
|
{
|
||||||
|
$sut = $this->createSut();
|
||||||
|
|
||||||
|
self::assertInstanceOf(AbstractWidgetType::class, $sut->setData(''));
|
||||||
|
|
||||||
// data
|
|
||||||
$sut->setData('slkudfhalksjdhfkljsahdf');
|
$sut->setData('slkudfhalksjdhfkljsahdf');
|
||||||
self::assertEquals('slkudfhalksjdhfkljsahdf', $sut->getData());
|
self::assertEquals('slkudfhalksjdhfkljsahdf', $sut->getData());
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
namespace App\Tests\Widget\Type;
|
namespace App\Tests\Widget\Type;
|
||||||
|
|
||||||
|
use App\Repository\TimesheetRepository;
|
||||||
use App\Widget\Type\AbstractWidgetType;
|
use App\Widget\Type\AbstractWidgetType;
|
||||||
use App\Widget\Type\Counter;
|
use App\Widget\Type\Counter;
|
||||||
use App\Widget\Type\SimpleWidget;
|
use App\Widget\Type\SimpleWidget;
|
||||||
@@ -17,11 +18,14 @@ use App\Widget\Type\SimpleWidget;
|
|||||||
* @covers \App\Widget\Type\Counter
|
* @covers \App\Widget\Type\Counter
|
||||||
* @covers \App\Widget\Type\SimpleWidget
|
* @covers \App\Widget\Type\SimpleWidget
|
||||||
*/
|
*/
|
||||||
class CounterTest extends AbstractWidgetTypeTest
|
class CounterTest extends AbstractSimpleStatisticsWidgetTypeTest
|
||||||
{
|
{
|
||||||
public function createSut(): AbstractWidgetType
|
public function createSut(): AbstractWidgetType
|
||||||
{
|
{
|
||||||
return new Counter();
|
$sut = new Counter($this->createMock(TimesheetRepository::class));
|
||||||
|
$sut->setQuery(TimesheetRepository::STATS_QUERY_ACTIVE);
|
||||||
|
|
||||||
|
return $sut;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDefaultOptions(): array
|
public function getDefaultOptions(): array
|
||||||
@@ -37,7 +41,8 @@ class CounterTest extends AbstractWidgetTypeTest
|
|||||||
|
|
||||||
public function testTemplateName()
|
public function testTemplateName()
|
||||||
{
|
{
|
||||||
$sut = new Counter();
|
/** @var Counter $sut */
|
||||||
|
$sut = $this->createSut();
|
||||||
self::assertEquals('widget/widget-counter.html.twig', $sut->getTemplateName());
|
self::assertEquals('widget/widget-counter.html.twig', $sut->getTemplateName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ namespace App\Tests\Widget\Type;
|
|||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use App\Model\Statistic\Day;
|
use App\Model\Statistic\Day;
|
||||||
use App\Repository\TimesheetRepository;
|
use App\Repository\TimesheetRepository;
|
||||||
use App\Tests\Mocks\Security\CurrentUserFactory;
|
|
||||||
use App\Tests\Mocks\Security\UserDateTimeFactoryFactory;
|
|
||||||
use App\Widget\Type\AbstractWidgetType;
|
use App\Widget\Type\AbstractWidgetType;
|
||||||
use App\Widget\Type\DailyWorkingTimeChart;
|
use App\Widget\Type\DailyWorkingTimeChart;
|
||||||
use App\Widget\Type\SimpleWidget;
|
use App\Widget\Type\SimpleWidget;
|
||||||
@@ -30,11 +28,11 @@ class DailyWorkingTimeChartTest extends TestCase
|
|||||||
public function createSut(): AbstractWidgetType
|
public function createSut(): AbstractWidgetType
|
||||||
{
|
{
|
||||||
$repository = $this->createMock(TimesheetRepository::class);
|
$repository = $this->createMock(TimesheetRepository::class);
|
||||||
$mockFactory = new UserDateTimeFactoryFactory($this);
|
|
||||||
$userFactory = new CurrentUserFactory($this);
|
|
||||||
$user = $userFactory->create(new User(), 'Europe/Berlin');
|
|
||||||
|
|
||||||
return new DailyWorkingTimeChart($repository, $user, $mockFactory->create('Europe/Berlin'));
|
$sut = new DailyWorkingTimeChart($repository);
|
||||||
|
$sut->setUser(new User());
|
||||||
|
|
||||||
|
return $sut;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testExtendsSimpleWidget()
|
public function testExtendsSimpleWidget()
|
||||||
@@ -107,12 +105,8 @@ class DailyWorkingTimeChartTest extends TestCase
|
|||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
$userFactory = new CurrentUserFactory($this);
|
$sut = new DailyWorkingTimeChart($repository);
|
||||||
$user = $userFactory->create(new User(), 'Europe/Berlin');
|
$sut->setUser(new User());
|
||||||
|
|
||||||
$mockFactory = new UserDateTimeFactoryFactory($this);
|
|
||||||
|
|
||||||
$sut = new DailyWorkingTimeChart($repository, $user, $mockFactory->create('Europe/Berlin'));
|
|
||||||
$data = $sut->getData([]);
|
$data = $sut->getData([]);
|
||||||
self::assertCount(2, $data);
|
self::assertCount(2, $data);
|
||||||
self::assertArrayHasKey('activities', $data);
|
self::assertArrayHasKey('activities', $data);
|
||||||
|
|||||||
@@ -9,17 +9,21 @@
|
|||||||
|
|
||||||
namespace App\Tests\Widget\Type;
|
namespace App\Tests\Widget\Type;
|
||||||
|
|
||||||
|
use App\Repository\TimesheetRepository;
|
||||||
use App\Widget\Type\AbstractWidgetType;
|
use App\Widget\Type\AbstractWidgetType;
|
||||||
use App\Widget\Type\YearChart;
|
use App\Widget\Type\YearChart;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \App\Widget\Type\YearChart
|
* @covers \App\Widget\Type\YearChart
|
||||||
*/
|
*/
|
||||||
class YearChartTest extends AbstractWidgetTypeTest
|
class YearChartTest extends AbstractSimpleStatisticsWidgetTypeTest
|
||||||
{
|
{
|
||||||
public function createSut(): AbstractWidgetType
|
public function createSut(): AbstractWidgetType
|
||||||
{
|
{
|
||||||
return new YearChart();
|
$sut = new YearChart($this->createMock(TimesheetRepository::class));
|
||||||
|
$sut->setQuery(TimesheetRepository::STATS_QUERY_ACTIVE);
|
||||||
|
|
||||||
|
return $sut;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDefaultOptions(): array
|
public function getDefaultOptions(): array
|
||||||
|
|||||||
Reference in New Issue
Block a user