diff --git a/src/Event/RevenueStatisticEvent.php b/src/Event/RevenueStatisticEvent.php new file mode 100644 index 00000000..c9750ffb --- /dev/null +++ b/src/Event/RevenueStatisticEvent.php @@ -0,0 +1,48 @@ +begin = $begin; + $this->end = $end; + } + + public function getBegin(): ?\DateTime + { + return $this->begin; + } + + public function getEnd(): ?\DateTime + { + return $this->end; + } + + public function getRevenue(): float + { + return $this->revenue; + } + + public function addRevenue(float $revenue): void + { + $this->revenue += $revenue; + } +} diff --git a/src/Event/UserRevenueStatisticEvent.php b/src/Event/UserRevenueStatisticEvent.php new file mode 100644 index 00000000..1b1ab311 --- /dev/null +++ b/src/Event/UserRevenueStatisticEvent.php @@ -0,0 +1,53 @@ +user = $user; + $this->event = new RevenueStatisticEvent($begin, $end); + } + + public function getBegin(): ?\DateTime + { + return $this->event->getBegin(); + } + + public function getEnd(): ?\DateTime + { + return $this->event->getEnd(); + } + + public function getRevenue(): float + { + return $this->event->getRevenue(); + } + + public function addRevenue(float $revenue): void + { + $this->event->addRevenue($revenue); + } + + public function getUser(): User + { + return $this->user; + } +} diff --git a/src/Repository/WidgetRepository.php b/src/Repository/WidgetRepository.php index 4b7ca694..371500b4 100644 --- a/src/Repository/WidgetRepository.php +++ b/src/Repository/WidgetRepository.php @@ -181,44 +181,6 @@ class WidgetRepository 'color' => 'red', 'type' => Counter::class, ], - 'userAmountToday' => [ - 'title' => 'stats.amountToday', - 'query' => TimesheetRepository::STATS_QUERY_RATE, - 'user' => true, - 'begin' => '00:00:00', - 'end' => '23:59:59', - 'icon' => 'money', - 'color' => 'green', - 'type' => Counter::class, - ], - 'userAmountWeek' => [ - 'title' => 'stats.amountWeek', - 'query' => TimesheetRepository::STATS_QUERY_RATE, - 'user' => true, - 'begin' => 'monday this week 00:00:00', - 'end' => 'sunday this week 23:59:59', - 'icon' => 'money', - 'color' => 'blue', - 'type' => Counter::class, - ], - 'userAmountMonth' => [ - 'title' => 'stats.amountMonth', - 'query' => TimesheetRepository::STATS_QUERY_RATE, - 'user' => true, - 'begin' => 'first day of this month 00:00:00', - 'end' => 'last day of this month 23:59:59', - 'icon' => 'money', - 'color' => 'purple', - 'type' => Counter::class, - ], - 'userAmountTotal' => [ - 'title' => 'stats.amountTotal', - 'query' => TimesheetRepository::STATS_QUERY_RATE, - 'user' => true, - 'icon' => 'money', - 'color' => 'red', - 'type' => Counter::class, - ], 'durationToday' => [ 'title' => 'stats.durationToday', 'query' => TimesheetRepository::STATS_QUERY_DURATION, @@ -257,44 +219,6 @@ class WidgetRepository 'user' => false, 'type' => Counter::class, ], - 'amountToday' => [ - 'title' => 'stats.amountToday', - 'query' => TimesheetRepository::STATS_QUERY_RATE, - 'begin' => '00:00:00', - 'end' => '23:59:59', - 'icon' => 'money', - 'color' => 'green', - 'user' => false, - 'type' => Counter::class, - ], - 'amountWeek' => [ - 'title' => 'stats.amountWeek', - 'query' => TimesheetRepository::STATS_QUERY_RATE, - 'begin' => 'monday this week 00:00:00', - 'end' => 'sunday this week 23:59:59', - 'icon' => 'money', - 'color' => 'blue', - 'user' => false, - 'type' => Counter::class, - ], - 'amountMonth' => [ - 'title' => 'stats.amountMonth', - 'query' => TimesheetRepository::STATS_QUERY_RATE, - 'begin' => 'first day of this month 00:00:00', - 'end' => 'last day of this month 23:59:59', - 'icon' => 'money', - 'color' => 'purple', - 'user' => false, - 'type' => Counter::class, - ], - 'amountTotal' => [ - 'title' => 'stats.amountTotal', - 'query' => TimesheetRepository::STATS_QUERY_RATE, - 'icon' => 'money', - 'color' => 'red', - 'user' => false, - 'type' => Counter::class, - ], 'activeUsersToday' => [ 'title' => 'stats.userActiveToday', 'query' => TimesheetRepository::STATS_QUERY_USER, diff --git a/src/Widget/Type/AbstractAmountPeriod.php b/src/Widget/Type/AbstractAmountPeriod.php new file mode 100644 index 00000000..dce8429b --- /dev/null +++ b/src/Widget/Type/AbstractAmountPeriod.php @@ -0,0 +1,59 @@ +dispatcher = $dispatcher; + } + + public function getTitle(): string + { + return 'stats.' . $this->getId(); + } + + public function getTemplateName(): string + { + return 'widget/widget-counter.html.twig'; + } + + public function getOptions(array $options = []): array + { + return array_merge([ + 'icon' => 'money', + 'dataType' => 'money', + ], parent::getOptions($options)); + } + + public function getData(array $options = []) + { + $this->setQuery(TimesheetRepository::STATS_QUERY_RATE); + $this->setQueryWithUser(false); + + $data = parent::getData($options); + + $event = new RevenueStatisticEvent($this->begin, $this->end); + if ($data !== null) { + $event->addRevenue($data); + } + $this->dispatcher->dispatch($event); + + return $event->getRevenue(); + } +} diff --git a/src/Widget/Type/AbstractUserAmountPeriod.php b/src/Widget/Type/AbstractUserAmountPeriod.php new file mode 100644 index 00000000..6a239deb --- /dev/null +++ b/src/Widget/Type/AbstractUserAmountPeriod.php @@ -0,0 +1,59 @@ +dispatcher = $dispatcher; + } + + public function getTitle(): string + { + return 'stats.' . str_replace('userA', 'a', $this->getId()); + } + + public function getTemplateName(): string + { + return 'widget/widget-counter.html.twig'; + } + + public function getOptions(array $options = []): array + { + return array_merge([ + 'icon' => 'money', + 'dataType' => 'money', + ], parent::getOptions($options)); + } + + public function getData(array $options = []) + { + $this->setQuery(TimesheetRepository::STATS_QUERY_RATE); + $this->setQueryWithUser(true); + + $data = parent::getData($options); + + $event = new UserRevenueStatisticEvent($this->user, $this->begin, $this->end); + if ($data !== null) { + $event->addRevenue($data); + } + $this->dispatcher->dispatch($event); + + return $event->getRevenue(); + } +} diff --git a/src/Widget/Type/AmountMonth.php b/src/Widget/Type/AmountMonth.php new file mode 100644 index 00000000..19a6d083 --- /dev/null +++ b/src/Widget/Type/AmountMonth.php @@ -0,0 +1,33 @@ + WidgetInterface::COLOR_MONTH], parent::getOptions($options)); + } + + public function getId(): string + { + return 'amountMonth'; + } + + public function getData(array $options = []) + { + $this->setBegin('first day of this month 00:00:00'); + $this->setEnd('last day of this month 23:59:59'); + + return parent::getData($options); + } +} diff --git a/src/Widget/Type/AmountToday.php b/src/Widget/Type/AmountToday.php new file mode 100644 index 00000000..da72e8ff --- /dev/null +++ b/src/Widget/Type/AmountToday.php @@ -0,0 +1,33 @@ + WidgetInterface::COLOR_TODAY], parent::getOptions($options)); + } + + public function getId(): string + { + return 'amountToday'; + } + + public function getData(array $options = []) + { + $this->setBegin('00:00:00'); + $this->setEnd('23:59:59'); + + return parent::getData($options); + } +} diff --git a/src/Widget/Type/AmountTotal.php b/src/Widget/Type/AmountTotal.php new file mode 100644 index 00000000..8b154389 --- /dev/null +++ b/src/Widget/Type/AmountTotal.php @@ -0,0 +1,25 @@ + WidgetInterface::COLOR_TOTAL], parent::getOptions($options)); + } + + public function getId(): string + { + return 'amountTotal'; + } +} diff --git a/src/Widget/Type/AmountWeek.php b/src/Widget/Type/AmountWeek.php new file mode 100644 index 00000000..964d40b4 --- /dev/null +++ b/src/Widget/Type/AmountWeek.php @@ -0,0 +1,33 @@ + WidgetInterface::COLOR_WEEK], parent::getOptions($options)); + } + + public function getId(): string + { + return 'amountWeek'; + } + + public function getData(array $options = []) + { + $this->setBegin('monday this week 00:00:00'); + $this->setEnd('sunday this week 23:59:59'); + + return parent::getData($options); + } +} diff --git a/src/Widget/Type/AmountYear.php b/src/Widget/Type/AmountYear.php index 21511afc..eb7489ad 100644 --- a/src/Widget/Type/AmountYear.php +++ b/src/Widget/Type/AmountYear.php @@ -10,17 +10,23 @@ namespace App\Widget\Type; use App\Configuration\SystemConfiguration; +use App\Event\RevenueStatisticEvent; use App\Repository\TimesheetRepository; +use App\Widget\WidgetInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; final class AmountYear extends CounterYear { - public function __construct(TimesheetRepository $repository, SystemConfiguration $systemConfiguration) + private $dispatcher; + + public function __construct(TimesheetRepository $repository, SystemConfiguration $systemConfiguration, EventDispatcherInterface $dispatcher) { parent::__construct($repository, $systemConfiguration); + $this->dispatcher = $dispatcher; $this->setId('amountYear'); $this->setOption('dataType', 'money'); $this->setOption('icon', 'money'); - $this->setOption('color', 'yellow'); + $this->setOption('color', WidgetInterface::COLOR_YEAR); $this->setTitle('stats.amountYear'); } @@ -30,6 +36,14 @@ final class AmountYear extends CounterYear $this->setQuery(TimesheetRepository::STATS_QUERY_RATE); $this->setQueryWithUser(false); - return parent::getData($options); + $data = parent::getData($options); + + $event = new RevenueStatisticEvent($this->begin, $this->end); + if ($data !== null) { + $event->addRevenue($data); + } + $this->dispatcher->dispatch($event); + + return $event->getRevenue(); } } diff --git a/src/Widget/Type/SimpleStatisticChart.php b/src/Widget/Type/SimpleStatisticChart.php index d12789f7..206284ab 100644 --- a/src/Widget/Type/SimpleStatisticChart.php +++ b/src/Widget/Type/SimpleStatisticChart.php @@ -34,7 +34,7 @@ class SimpleStatisticChart extends SimpleWidget implements UserWidget /** * @var User|null */ - private $user; + protected $user; /** * @var bool */ @@ -106,11 +106,11 @@ class SimpleStatisticChart extends SimpleWidget implements UserWidget $end = $this->end; if (!empty($begin) && \is_string($begin)) { - $begin = new \DateTime($begin, $timezone); + $this->begin = new \DateTime($begin, $timezone); } if (!empty($end) && \is_string($end)) { - $end = new \DateTime($end, $timezone); + $this->end = new \DateTime($end, $timezone); } try { @@ -119,7 +119,7 @@ class SimpleStatisticChart extends SimpleWidget implements UserWidget $user = $this->user; } - return $this->repository->getStatistic($this->query, $begin, $end, $user); + return $this->repository->getStatistic($this->query, $this->begin, $this->end, $user); } catch (\Exception $ex) { throw new WidgetException( 'Failed loading widget data: ' . $ex->getMessage() diff --git a/src/Widget/Type/UserAmountMonth.php b/src/Widget/Type/UserAmountMonth.php new file mode 100644 index 00000000..a279e609 --- /dev/null +++ b/src/Widget/Type/UserAmountMonth.php @@ -0,0 +1,33 @@ + WidgetInterface::COLOR_MONTH], parent::getOptions($options)); + } + + public function getId(): string + { + return 'userAmountMonth'; + } + + public function getData(array $options = []) + { + $this->setBegin('first day of this month 00:00:00'); + $this->setEnd('last day of this month 23:59:59'); + + return parent::getData($options); + } +} diff --git a/src/Widget/Type/UserAmountToday.php b/src/Widget/Type/UserAmountToday.php new file mode 100644 index 00000000..726a53d5 --- /dev/null +++ b/src/Widget/Type/UserAmountToday.php @@ -0,0 +1,33 @@ + WidgetInterface::COLOR_TODAY], parent::getOptions($options)); + } + + public function getId(): string + { + return 'userAmountToday'; + } + + public function getData(array $options = []) + { + $this->setBegin('00:00:00'); + $this->setEnd('23:59:59'); + + return parent::getData($options); + } +} diff --git a/src/Widget/Type/UserAmountTotal.php b/src/Widget/Type/UserAmountTotal.php new file mode 100644 index 00000000..65d88a17 --- /dev/null +++ b/src/Widget/Type/UserAmountTotal.php @@ -0,0 +1,25 @@ + WidgetInterface::COLOR_TOTAL], parent::getOptions($options)); + } + + public function getId(): string + { + return 'userAmountTotal'; + } +} diff --git a/src/Widget/Type/UserAmountWeek.php b/src/Widget/Type/UserAmountWeek.php new file mode 100644 index 00000000..66995b4a --- /dev/null +++ b/src/Widget/Type/UserAmountWeek.php @@ -0,0 +1,33 @@ + WidgetInterface::COLOR_WEEK], parent::getOptions($options)); + } + + public function getId(): string + { + return 'userAmountWeek'; + } + + public function getData(array $options = []) + { + $this->setBegin('monday this week 00:00:00'); + $this->setEnd('sunday this week 23:59:59'); + + return parent::getData($options); + } +} diff --git a/src/Widget/Type/UserAmountYear.php b/src/Widget/Type/UserAmountYear.php index 3068d0aa..fb480708 100644 --- a/src/Widget/Type/UserAmountYear.php +++ b/src/Widget/Type/UserAmountYear.php @@ -10,17 +10,23 @@ namespace App\Widget\Type; use App\Configuration\SystemConfiguration; +use App\Event\UserRevenueStatisticEvent; use App\Repository\TimesheetRepository; +use App\Widget\WidgetInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; final class UserAmountYear extends CounterYear { - public function __construct(TimesheetRepository $repository, SystemConfiguration $systemConfiguration) + private $dispatcher; + + public function __construct(TimesheetRepository $repository, SystemConfiguration $systemConfiguration, EventDispatcherInterface $dispatcher) { parent::__construct($repository, $systemConfiguration); + $this->dispatcher = $dispatcher; $this->setId('userAmountYear'); $this->setOption('dataType', 'money'); $this->setOption('icon', 'money'); - $this->setOption('color', 'yellow'); + $this->setOption('color', WidgetInterface::COLOR_YEAR); $this->setTitle('stats.amountYear'); } @@ -30,6 +36,14 @@ final class UserAmountYear extends CounterYear $this->setQuery(TimesheetRepository::STATS_QUERY_RATE); $this->setQueryWithUser(true); - return parent::getData($options); + $data = parent::getData($options); + + $event = new UserRevenueStatisticEvent($this->user, $this->begin, $this->end); + if ($data !== null) { + $event->addRevenue($data); + } + $this->dispatcher->dispatch($event); + + return $event->getRevenue(); } } diff --git a/src/Widget/WidgetInterface.php b/src/Widget/WidgetInterface.php index 679fe7f5..9fafd861 100644 --- a/src/Widget/WidgetInterface.php +++ b/src/Widget/WidgetInterface.php @@ -11,6 +11,12 @@ namespace App\Widget; interface WidgetInterface { + public const COLOR_TODAY = 'green'; + public const COLOR_WEEK = 'blue'; + public const COLOR_MONTH = 'purple'; + public const COLOR_YEAR = 'yellow'; + public const COLOR_TOTAL = 'red'; + /** * Returns a unique ID for this widget. * diff --git a/tests/Event/RevenueStatisticEventTest.php b/tests/Event/RevenueStatisticEventTest.php new file mode 100644 index 00000000..00e7bea4 --- /dev/null +++ b/tests/Event/RevenueStatisticEventTest.php @@ -0,0 +1,44 @@ +getEnd()); + self::assertNull($sut->getBegin()); + self::assertSame(0.0, $sut->getRevenue()); + + $end = new \DateTime(); + $begin = new \DateTime(); + + $sut = new RevenueStatisticEvent($begin, $end); + + self::assertSame($begin, $sut->getBegin()); + self::assertSame($end, $sut->getEnd()); + self::assertSame(0.0, $sut->getRevenue()); + + $sut->addRevenue(13.45); + $sut->addRevenue(6.55); + $sut->addRevenue(111); + $sut->addRevenue(2222.22); + + self::assertSame(2353.22, $sut->getRevenue()); + } +} diff --git a/tests/Event/UserRevenueStatisticEventTest.php b/tests/Event/UserRevenueStatisticEventTest.php new file mode 100644 index 00000000..d42a059e --- /dev/null +++ b/tests/Event/UserRevenueStatisticEventTest.php @@ -0,0 +1,48 @@ +getUser()); + self::assertNull($sut->getEnd()); + self::assertNull($sut->getBegin()); + self::assertSame(0.0, $sut->getRevenue()); + + $end = new \DateTime(); + $begin = new \DateTime(); + + $sut = new UserRevenueStatisticEvent($user, $begin, $end); + + self::assertSame($user, $sut->getUser()); + self::assertSame($begin, $sut->getBegin()); + self::assertSame($end, $sut->getEnd()); + self::assertSame(0.0, $sut->getRevenue()); + + $sut->addRevenue(13.45); + $sut->addRevenue(6.55); + $sut->addRevenue(111); + $sut->addRevenue(2222.22); + + self::assertSame(2353.22, $sut->getRevenue()); + } +} diff --git a/tests/Widget/Type/AbstractWidgetTypeTest.php b/tests/Widget/Type/AbstractWidgetTypeTest.php index 74afe980..3349a507 100644 --- a/tests/Widget/Type/AbstractWidgetTypeTest.php +++ b/tests/Widget/Type/AbstractWidgetTypeTest.php @@ -58,10 +58,6 @@ abstract class AbstractWidgetTypeTest extends TestCase self::assertEquals('blab', $options['blub']); self::assertEquals('money', $options['dataType']); self::assertEquals('trääääää', $options['föööö']); - - // id - $sut->setId('cvbnmyx'); - self::assertEquals('cvbnmyx', $sut->getId()); } public function testData() diff --git a/tests/Widget/Type/AmountMonthTest.php b/tests/Widget/Type/AmountMonthTest.php new file mode 100644 index 00000000..12e47922 --- /dev/null +++ b/tests/Widget/Type/AmountMonthTest.php @@ -0,0 +1,68 @@ +getData()); + } + + /** + * @return AbstractAmountPeriod + */ + public function createSut(): AbstractWidgetType + { + $repository = $this->createMock(TimesheetRepository::class); + $dispatcher = $this->createMock(EventDispatcherInterface::class); + + return new AmountMonth($repository, $dispatcher); + } + + public function getDefaultOptions(): array + { + return [ + 'dataType' => 'money', + 'icon' => 'money', + 'color' => WidgetInterface::COLOR_MONTH, + ]; + } + + public function testData() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Cannot set data on instances of SimpleStatisticChart'); + + $sut = $this->createSut(); + self::assertInstanceOf(SimpleStatisticChart::class, $sut); + $sut->setData(10); + } + + public function testSettings() + { + $sut = $this->createSut(); + + self::assertEquals('widget/widget-counter.html.twig', $sut->getTemplateName()); + self::assertEquals('amountMonth', $sut->getId()); + } +} diff --git a/tests/Widget/Type/AmountTodayTest.php b/tests/Widget/Type/AmountTodayTest.php new file mode 100644 index 00000000..c138738c --- /dev/null +++ b/tests/Widget/Type/AmountTodayTest.php @@ -0,0 +1,68 @@ +getData()); + } + + /** + * @return AbstractAmountPeriod + */ + public function createSut(): AbstractWidgetType + { + $repository = $this->createMock(TimesheetRepository::class); + $dispatcher = $this->createMock(EventDispatcherInterface::class); + + return new AmountToday($repository, $dispatcher); + } + + public function getDefaultOptions(): array + { + return [ + 'dataType' => 'money', + 'icon' => 'money', + 'color' => WidgetInterface::COLOR_TODAY, + ]; + } + + public function testData() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Cannot set data on instances of SimpleStatisticChart'); + + $sut = $this->createSut(); + self::assertInstanceOf(SimpleStatisticChart::class, $sut); + $sut->setData(10); + } + + public function testSettings() + { + $sut = $this->createSut(); + + self::assertEquals('widget/widget-counter.html.twig', $sut->getTemplateName()); + self::assertEquals('amountToday', $sut->getId()); + } +} diff --git a/tests/Widget/Type/AmountTotalTest.php b/tests/Widget/Type/AmountTotalTest.php new file mode 100644 index 00000000..cf70851b --- /dev/null +++ b/tests/Widget/Type/AmountTotalTest.php @@ -0,0 +1,68 @@ +getData()); + } + + /** + * @return AbstractAmountPeriod + */ + public function createSut(): AbstractWidgetType + { + $repository = $this->createMock(TimesheetRepository::class); + $dispatcher = $this->createMock(EventDispatcherInterface::class); + + return new AmountTotal($repository, $dispatcher); + } + + public function getDefaultOptions(): array + { + return [ + 'dataType' => 'money', + 'icon' => 'money', + 'color' => WidgetInterface::COLOR_TOTAL, + ]; + } + + public function testData() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Cannot set data on instances of SimpleStatisticChart'); + + $sut = $this->createSut(); + self::assertInstanceOf(SimpleStatisticChart::class, $sut); + $sut->setData(10); + } + + public function testSettings() + { + $sut = $this->createSut(); + + self::assertEquals('widget/widget-counter.html.twig', $sut->getTemplateName()); + self::assertEquals('amountTotal', $sut->getId()); + } +} diff --git a/tests/Widget/Type/AmountWeekTest.php b/tests/Widget/Type/AmountWeekTest.php new file mode 100644 index 00000000..2904509b --- /dev/null +++ b/tests/Widget/Type/AmountWeekTest.php @@ -0,0 +1,68 @@ +getData()); + } + + /** + * @return AbstractAmountPeriod + */ + public function createSut(): AbstractWidgetType + { + $repository = $this->createMock(TimesheetRepository::class); + $dispatcher = $this->createMock(EventDispatcherInterface::class); + + return new AmountWeek($repository, $dispatcher); + } + + public function getDefaultOptions(): array + { + return [ + 'dataType' => 'money', + 'icon' => 'money', + 'color' => WidgetInterface::COLOR_WEEK, + ]; + } + + public function testData() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Cannot set data on instances of SimpleStatisticChart'); + + $sut = $this->createSut(); + self::assertInstanceOf(SimpleStatisticChart::class, $sut); + $sut->setData(10); + } + + public function testSettings() + { + $sut = $this->createSut(); + + self::assertEquals('widget/widget-counter.html.twig', $sut->getTemplateName()); + self::assertEquals('amountWeek', $sut->getId()); + } +} diff --git a/tests/Widget/Type/AmountYearTest.php b/tests/Widget/Type/AmountYearTest.php index edf2422f..49dad263 100644 --- a/tests/Widget/Type/AmountYearTest.php +++ b/tests/Widget/Type/AmountYearTest.php @@ -15,6 +15,8 @@ use App\Widget\Type\AbstractWidgetType; use App\Widget\Type\AmountYear; use App\Widget\Type\CounterYear; use App\Widget\Type\SimpleStatisticChart; +use App\Widget\WidgetInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; /** * @covers \App\Widget\Type\AmountYear @@ -22,6 +24,11 @@ use App\Widget\Type\SimpleStatisticChart; */ class AmountYearTest extends AbstractWidgetTypeTest { + protected function assertDefaultData(AbstractWidgetType $sut) + { + self::assertEquals(0.0, $sut->getData()); + } + /** * @return CounterYear */ @@ -29,8 +36,9 @@ class AmountYearTest extends AbstractWidgetTypeTest { $repository = $this->createMock(TimesheetRepository::class); $configuration = $this->createMock(SystemConfiguration::class); + $dispatcher = $this->createMock(EventDispatcherInterface::class); - return new AmountYear($repository, $configuration); + return new AmountYear($repository, $configuration, $dispatcher); } public function getDefaultOptions(): array @@ -38,7 +46,7 @@ class AmountYearTest extends AbstractWidgetTypeTest return [ 'dataType' => 'money', 'icon' => 'money', - 'color' => 'yellow', + 'color' => WidgetInterface::COLOR_YEAR, ]; } diff --git a/tests/Widget/Type/UserAmountMonthTest.php b/tests/Widget/Type/UserAmountMonthTest.php new file mode 100644 index 00000000..4cbddfea --- /dev/null +++ b/tests/Widget/Type/UserAmountMonthTest.php @@ -0,0 +1,72 @@ +getData()); + } + + /** + * @return AbstractUserAmountPeriod + */ + public function createSut(): AbstractWidgetType + { + $repository = $this->createMock(TimesheetRepository::class); + $dispatcher = $this->createMock(EventDispatcherInterface::class); + + $widget = new UserAmountMonth($repository, $dispatcher); + $widget->setUser(new User()); + + return $widget; + } + + public function getDefaultOptions(): array + { + return [ + 'dataType' => 'money', + 'icon' => 'money', + 'color' => WidgetInterface::COLOR_MONTH, + ]; + } + + public function testData() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Cannot set data on instances of SimpleStatisticChart'); + + $sut = $this->createSut(); + self::assertInstanceOf(SimpleStatisticChart::class, $sut); + $sut->setData(10); + } + + public function testSettings() + { + $sut = $this->createSut(); + + self::assertEquals('widget/widget-counter.html.twig', $sut->getTemplateName()); + self::assertEquals('userAmountMonth', $sut->getId()); + } +} diff --git a/tests/Widget/Type/UserAmountTodayTest.php b/tests/Widget/Type/UserAmountTodayTest.php new file mode 100644 index 00000000..cd1669ab --- /dev/null +++ b/tests/Widget/Type/UserAmountTodayTest.php @@ -0,0 +1,72 @@ +getData()); + } + + /** + * @return AbstractUserAmountPeriod + */ + public function createSut(): AbstractWidgetType + { + $repository = $this->createMock(TimesheetRepository::class); + $dispatcher = $this->createMock(EventDispatcherInterface::class); + + $widget = new UserAmountToday($repository, $dispatcher); + $widget->setUser(new User()); + + return $widget; + } + + public function getDefaultOptions(): array + { + return [ + 'dataType' => 'money', + 'icon' => 'money', + 'color' => WidgetInterface::COLOR_TODAY, + ]; + } + + public function testData() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Cannot set data on instances of SimpleStatisticChart'); + + $sut = $this->createSut(); + self::assertInstanceOf(SimpleStatisticChart::class, $sut); + $sut->setData(10); + } + + public function testSettings() + { + $sut = $this->createSut(); + + self::assertEquals('widget/widget-counter.html.twig', $sut->getTemplateName()); + self::assertEquals('userAmountToday', $sut->getId()); + } +} diff --git a/tests/Widget/Type/UserAmountTotalTest.php b/tests/Widget/Type/UserAmountTotalTest.php new file mode 100644 index 00000000..cbf470ea --- /dev/null +++ b/tests/Widget/Type/UserAmountTotalTest.php @@ -0,0 +1,72 @@ +getData()); + } + + /** + * @return AbstractUserAmountPeriod + */ + public function createSut(): AbstractWidgetType + { + $repository = $this->createMock(TimesheetRepository::class); + $dispatcher = $this->createMock(EventDispatcherInterface::class); + + $widget = new UserAmountTotal($repository, $dispatcher); + $widget->setUser(new User()); + + return $widget; + } + + public function getDefaultOptions(): array + { + return [ + 'dataType' => 'money', + 'icon' => 'money', + 'color' => WidgetInterface::COLOR_TOTAL, + ]; + } + + public function testData() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Cannot set data on instances of SimpleStatisticChart'); + + $sut = $this->createSut(); + self::assertInstanceOf(SimpleStatisticChart::class, $sut); + $sut->setData(10); + } + + public function testSettings() + { + $sut = $this->createSut(); + + self::assertEquals('widget/widget-counter.html.twig', $sut->getTemplateName()); + self::assertEquals('userAmountTotal', $sut->getId()); + } +} diff --git a/tests/Widget/Type/UserAmountWeekTest.php b/tests/Widget/Type/UserAmountWeekTest.php new file mode 100644 index 00000000..503e30c3 --- /dev/null +++ b/tests/Widget/Type/UserAmountWeekTest.php @@ -0,0 +1,72 @@ +getData()); + } + + /** + * @return AbstractUserAmountPeriod + */ + public function createSut(): AbstractWidgetType + { + $repository = $this->createMock(TimesheetRepository::class); + $dispatcher = $this->createMock(EventDispatcherInterface::class); + + $widget = new UserAmountWeek($repository, $dispatcher); + $widget->setUser(new User()); + + return $widget; + } + + public function getDefaultOptions(): array + { + return [ + 'dataType' => 'money', + 'icon' => 'money', + 'color' => WidgetInterface::COLOR_WEEK, + ]; + } + + public function testData() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Cannot set data on instances of SimpleStatisticChart'); + + $sut = $this->createSut(); + self::assertInstanceOf(SimpleStatisticChart::class, $sut); + $sut->setData(10); + } + + public function testSettings() + { + $sut = $this->createSut(); + + self::assertEquals('widget/widget-counter.html.twig', $sut->getTemplateName()); + self::assertEquals('userAmountWeek', $sut->getId()); + } +} diff --git a/tests/Widget/Type/UserAmountYearTest.php b/tests/Widget/Type/UserAmountYearTest.php index f35f30f7..b61fa891 100644 --- a/tests/Widget/Type/UserAmountYearTest.php +++ b/tests/Widget/Type/UserAmountYearTest.php @@ -10,11 +10,14 @@ namespace App\Tests\Widget\Type; use App\Configuration\SystemConfiguration; +use App\Entity\User; use App\Repository\TimesheetRepository; use App\Widget\Type\AbstractWidgetType; use App\Widget\Type\CounterYear; use App\Widget\Type\SimpleStatisticChart; use App\Widget\Type\UserAmountYear; +use App\Widget\WidgetInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; /** * @covers \App\Widget\Type\UserAmountYear @@ -22,6 +25,11 @@ use App\Widget\Type\UserAmountYear; */ class UserAmountYearTest extends AbstractWidgetTypeTest { + protected function assertDefaultData(AbstractWidgetType $sut) + { + self::assertEquals(0.0, $sut->getData()); + } + /** * @return CounterYear */ @@ -29,8 +37,12 @@ class UserAmountYearTest extends AbstractWidgetTypeTest { $repository = $this->createMock(TimesheetRepository::class); $configuration = $this->createMock(SystemConfiguration::class); + $dispatcher = $this->createMock(EventDispatcherInterface::class); - return new UserAmountYear($repository, $configuration); + $widget = new UserAmountYear($repository, $configuration, $dispatcher); + $widget->setUser(new User()); + + return $widget; } public function getDefaultOptions(): array @@ -38,7 +50,7 @@ class UserAmountYearTest extends AbstractWidgetTypeTest return [ 'dataType' => 'money', 'icon' => 'money', - 'color' => 'yellow', + 'color' => WidgetInterface::COLOR_YEAR, ]; }