From 7873bd812e7659631442d13b1f3df4c1fffd6e1e Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Tue, 10 Aug 2021 14:39:52 +0200 Subject: [PATCH] fix statistics in user profile for first year not starting in january (#2712) --- src/Controller/ProfileController.php | 9 +++++++-- src/Timesheet/DateTimeFactory.php | 26 +++++++++++++++++-------- tests/Timesheet/DateTimeFactoryTest.php | 21 ++++++++++++++++++++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/Controller/ProfileController.php b/src/Controller/ProfileController.php index ccec0f96..8cf17ce5 100644 --- a/src/Controller/ProfileController.php +++ b/src/Controller/ProfileController.php @@ -52,11 +52,16 @@ final class ProfileController extends AbstractController */ public function indexAction(User $profile, TimesheetRepository $repository, TimesheetStatisticService $statisticService): Response { + $dateFactory = $this->getDateTimeFactory(); $userStats = $repository->getUserStatistics($profile, false); $firstEntry = $statisticService->findFirstRecordDate($profile); - $begin = $firstEntry ?? $this->getDateTimeFactory()->getStartOfMonth(); - $end = $this->getDateTimeFactory()->getEndOfMonth(); + $begin = $firstEntry ?? $dateFactory->getStartOfMonth(); + $end = $dateFactory->getEndOfMonth(); + + // statistic service does not fill up the complete year by default! + // but we need a full year, because the chart needs always 12 month + $begin = $dateFactory->createStartOfYear($begin); $viewVars = [ 'tab' => 'charts', diff --git a/src/Timesheet/DateTimeFactory.php b/src/Timesheet/DateTimeFactory.php index b845e3be..5a60ae81 100644 --- a/src/Timesheet/DateTimeFactory.php +++ b/src/Timesheet/DateTimeFactory.php @@ -52,9 +52,10 @@ class DateTimeFactory { if (null === $date) { $date = $this->createDateTime(); + } else { + $date = clone $date; } - $date = clone $date; $date->modify('first day of this month'); $date->setTime(0, 0, 0); @@ -64,11 +65,11 @@ class DateTimeFactory public function getStartOfWeek(?DateTime $date = null): DateTime { if (null === $date) { - $date = $this->createDateTime('now'); + $from = $this->createDateTime('now'); + } else { + $from = clone $date; } - $from = clone $date; - $year = $from->format('o'); $week = $from->format('W'); $firstDay = 1; @@ -118,9 +119,7 @@ class DateTimeFactory public function createDateTime(string $datetime = 'now'): DateTime { - $date = new DateTime($datetime, $this->getTimezone()); - - return $date; + return new DateTime($datetime, $this->getTimezone()); } /** @@ -130,7 +129,18 @@ class DateTimeFactory */ public function createDateTimeFromFormat(string $format, ?string $datetime = 'now') { - $date = DateTime::createFromFormat($format, $datetime, $this->getTimezone()); + return DateTime::createFromFormat($format, $datetime, $this->getTimezone()); + } + + public function createStartOfYear(?DateTime $date = null): DateTime + { + if (null === $date) { + $date = $this->createDateTime(); + } else { + $date = clone $date; + } + + $date->modify('first day of january 00:00:00'); return $date; } diff --git a/tests/Timesheet/DateTimeFactoryTest.php b/tests/Timesheet/DateTimeFactoryTest.php index 53f240c7..dcb4b931 100644 --- a/tests/Timesheet/DateTimeFactoryTest.php +++ b/tests/Timesheet/DateTimeFactoryTest.php @@ -217,4 +217,25 @@ class DateTimeFactoryTest extends TestCase self::assertEquals($expected, $end); } + + public function testCreateStartOfYear() + { + $sut = $this->createDateTimeFactory(self::TEST_TIMEZONE); + + $now = $sut->createDateTime(); + $year = $sut->createStartOfYear(); + self::assertEquals($now->format('Y'), $year->format('Y')); + self::assertEquals('01', $year->format('m')); + self::assertEquals('01', $year->format('d')); + self::assertEquals('00:00:00', $year->format('H:i:s')); + $now->setTime(0, 0, 0); + self::assertEquals($now->format('H:i:s'), $year->format('H:i:s')); + + $begin = $sut->createDateTime('2017-12-31 23:59:59'); + $year = $sut->createStartOfYear($begin); + self::assertEquals('2017', $year->format('Y')); + self::assertEquals('01', $year->format('m')); + self::assertEquals('01', $year->format('d')); + self::assertEquals('00:00:00', $year->format('H:i:s')); + } }