From f04bb65b49eec6a65cdf6ed9de35e1828d5c49a9 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Mon, 16 Oct 2023 09:12:31 +0200 Subject: [PATCH 01/14] Getter for 'from', 'to' event's configuration --- src/Event.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/Event.php b/src/Event.php index fef75a6..a221e11 100644 --- a/src/Event.php +++ b/src/Event.php @@ -88,6 +88,20 @@ class Event implements PingableInterface */ protected $timezone; + /** + * Datetime or time since the task is evaluated and possibly executed only for display purposes. + * + * @var \DateTime|string|null + */ + protected $disply_from = null; + + /** + * Datetime or time until the task is evaluated and possibly executed only for display purposes. + * + * @var \DateTime|string|null + */ + protected $disply_to = null; + /** * The user the command should run as. * @@ -447,6 +461,8 @@ public function between($from, $to) */ public function from($datetime) { + $this->disply_from = $datetime; + return $this->skip(fn () => $this->notYet($datetime)); } @@ -459,6 +475,8 @@ public function from($datetime) */ public function to($datetime) { + $this->disply_to = $datetime; + return $this->skip(fn () => $this->past($datetime)); } @@ -921,6 +939,26 @@ public function getExpression() return $this->expression; } + /** + * Get the 'from' configuration for the event if present. + * + * @return \DateTime|string|null + */ + public function getFrom() + { + return $this->disply_from; + } + + /** + * Get the 'to' configuration for the event if present. + * + * @return \DateTime|string|null + */ + public function getTo() + { + return $this->disply_to; + } + /** * Set the event's command. * From 2053772c7aebe4214098e05879f4dbc8cfa4c3ce Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Wed, 18 Oct 2023 15:46:36 +0200 Subject: [PATCH 02/14] Tests for lifetime configurations getters --- tests/Unit/EventTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index c59d9c6..ea351c4 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -154,6 +154,35 @@ public function test_cron_life_time(): void ); } + public function test_cron_life_time_getters(): void + { + $timezone = new \DateTimeZone('UTC'); + + $date_from = '2023-10-01'; + $date_to = '2023-10-30'; + + $event = new Event($this->id, 'php foo'); + $event->between($date_from, $date_to); + + self::assertTrue( + ($date_from == $event->getFrom() && $date_to == $event->getTo()) + ); + + $event = new Event($this->id, 'php foo'); + $event->from($date_from); + + self::assertTrue( + ($date_from == $event->getFrom()) + ); + + $event = new Event($this->id, 'php foo'); + $event->to($date_to); + + self::assertTrue( + ($date_to == $event->getTo()) + ); + } + public function test_cron_conditions(): void { $timezone = new \DateTimeZone('UTC'); From 7d05c371f82d5f3d7ecb2cff2e0f37bd2948d3c1 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Wed, 18 Oct 2023 15:55:24 +0200 Subject: [PATCH 03/14] Lifetime getters variables change --- src/Event.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Event.php b/src/Event.php index a221e11..c94f41a 100644 --- a/src/Event.php +++ b/src/Event.php @@ -93,14 +93,14 @@ class Event implements PingableInterface * * @var \DateTime|string|null */ - protected $disply_from = null; + protected $from = null; /** * Datetime or time until the task is evaluated and possibly executed only for display purposes. * * @var \DateTime|string|null */ - protected $disply_to = null; + protected $to = null; /** * The user the command should run as. @@ -461,7 +461,7 @@ public function between($from, $to) */ public function from($datetime) { - $this->disply_from = $datetime; + $this->from = $datetime; return $this->skip(fn () => $this->notYet($datetime)); } @@ -475,7 +475,7 @@ public function from($datetime) */ public function to($datetime) { - $this->disply_to = $datetime; + $this->to = $datetime; return $this->skip(fn () => $this->past($datetime)); } @@ -946,7 +946,7 @@ public function getExpression() */ public function getFrom() { - return $this->disply_from; + return $this->from; } /** @@ -956,7 +956,7 @@ public function getFrom() */ public function getTo() { - return $this->disply_to; + return $this->to; } /** From 7e12163b5b66115532a98fb196f42ee057103b44 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Thu, 19 Oct 2023 15:11:44 +0200 Subject: [PATCH 04/14] Test fix as requeste by Code_Checks / Static analysis --- tests/Unit/EventTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index ea351c4..ba589ac 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -165,21 +165,21 @@ public function test_cron_life_time_getters(): void $event->between($date_from, $date_to); self::assertTrue( - ($date_from == $event->getFrom() && $date_to == $event->getTo()) + ($date_from === $event->getFrom() && $date_to === $event->getTo()) ); $event = new Event($this->id, 'php foo'); $event->from($date_from); self::assertTrue( - ($date_from == $event->getFrom()) + ($date_from === $event->getFrom()) ); $event = new Event($this->id, 'php foo'); $event->to($date_to); self::assertTrue( - ($date_to == $event->getTo()) + ($date_to === $event->getTo()) ); } From 6369223654631b858fefc559fc96e14050d69efc Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Thu, 19 Oct 2023 15:12:47 +0200 Subject: [PATCH 05/14] Use of union type in lifetime getters --- src/Event.php | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Event.php b/src/Event.php index c94f41a..5969cee 100644 --- a/src/Event.php +++ b/src/Event.php @@ -90,17 +90,13 @@ class Event implements PingableInterface /** * Datetime or time since the task is evaluated and possibly executed only for display purposes. - * - * @var \DateTime|string|null */ - protected $from = null; + protected \DateTime|string|null $from = null; /** * Datetime or time until the task is evaluated and possibly executed only for display purposes. - * - * @var \DateTime|string|null */ - protected $to = null; + protected \DateTime|string|null $to = null; /** * The user the command should run as. @@ -941,20 +937,16 @@ public function getExpression() /** * Get the 'from' configuration for the event if present. - * - * @return \DateTime|string|null */ - public function getFrom() + public function getFrom() : \DateTime | string | null { return $this->from; } /** * Get the 'to' configuration for the event if present. - * - * @return \DateTime|string|null */ - public function getTo() + public function getTo() : \DateTime | string | null { return $this->to; } From a8728b51f80d4ff9955434c91f90b3922ae15bb5 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Thu, 19 Oct 2023 15:20:29 +0200 Subject: [PATCH 06/14] Split of test_cron_life_time, one for getFrom and one for getTo --- tests/Unit/EventTest.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index ba589ac..fb227d1 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -154,7 +154,7 @@ public function test_cron_life_time(): void ); } - public function test_cron_life_time_getters(): void + public function test_cron_life_time_getFrom(): void { $timezone = new \DateTimeZone('UTC'); @@ -165,7 +165,7 @@ public function test_cron_life_time_getters(): void $event->between($date_from, $date_to); self::assertTrue( - ($date_from === $event->getFrom() && $date_to === $event->getTo()) + ($date_from === $event->getFrom()) ); $event = new Event($this->id, 'php foo'); @@ -174,6 +174,21 @@ public function test_cron_life_time_getters(): void self::assertTrue( ($date_from === $event->getFrom()) ); + } + + public function test_cron_life_time_getTo(): void + { + $timezone = new \DateTimeZone('UTC'); + + $date_from = '2023-10-01'; + $date_to = '2023-10-30'; + + $event = new Event($this->id, 'php foo'); + $event->between($date_from, $date_to); + + self::assertTrue( + ($date_to === $event->getTo()) + ); $event = new Event($this->id, 'php foo'); $event->to($date_to); From 152eef9d77ea9fbf9b80bc94b69c912c45dcbfe4 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Thu, 19 Oct 2023 15:28:29 +0200 Subject: [PATCH 07/14] Removed unnecessary parentheses --- tests/Unit/EventTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index fb227d1..f597a9a 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -165,14 +165,14 @@ public function test_cron_life_time_getFrom(): void $event->between($date_from, $date_to); self::assertTrue( - ($date_from === $event->getFrom()) + $date_from === $event->getFrom() ); $event = new Event($this->id, 'php foo'); $event->from($date_from); self::assertTrue( - ($date_from === $event->getFrom()) + $date_from === $event->getFrom() ); } @@ -187,14 +187,14 @@ public function test_cron_life_time_getTo(): void $event->between($date_from, $date_to); self::assertTrue( - ($date_to === $event->getTo()) + $date_to === $event->getTo() ); $event = new Event($this->id, 'php foo'); $event->to($date_to); self::assertTrue( - ($date_to === $event->getTo()) + $date_to === $event->getTo() ); } From af2bf61e2f4de744668272e7ad7cdc841d2e2910 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Thu, 19 Oct 2023 16:01:09 +0200 Subject: [PATCH 08/14] Fix static analysis issue --- src/Event.php | 4 ++-- tests/Unit/EventTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Event.php b/src/Event.php index 5969cee..b57aaff 100644 --- a/src/Event.php +++ b/src/Event.php @@ -938,7 +938,7 @@ public function getExpression() /** * Get the 'from' configuration for the event if present. */ - public function getFrom() : \DateTime | string | null + public function getFrom(): \DateTime|string|null { return $this->from; } @@ -946,7 +946,7 @@ public function getFrom() : \DateTime | string | null /** * Get the 'to' configuration for the event if present. */ - public function getTo() : \DateTime | string | null + public function getTo(): \DateTime|string|null { return $this->to; } diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index f597a9a..3796a34 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -154,7 +154,7 @@ public function test_cron_life_time(): void ); } - public function test_cron_life_time_getFrom(): void + public function test_cron_life_time_get_from(): void { $timezone = new \DateTimeZone('UTC'); @@ -176,7 +176,7 @@ public function test_cron_life_time_getFrom(): void ); } - public function test_cron_life_time_getTo(): void + public function test_cron_life_time_get_to(): void { $timezone = new \DateTimeZone('UTC'); From d50bec694842422e306b66dbf51195838f74a3c6 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Fri, 3 Nov 2023 11:56:17 +0100 Subject: [PATCH 09/14] Code cleaning and between test added --- tests/Unit/EventTest.php | 43 ++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index 3796a34..e076d0c 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -154,48 +154,43 @@ public function test_cron_life_time(): void ); } - public function test_cron_life_time_get_from(): void + public function test_get_from(): void { $timezone = new \DateTimeZone('UTC'); - $date_from = '2023-10-01'; - $date_to = '2023-10-30'; + $dateFrom = '2023-10-01'; $event = new Event($this->id, 'php foo'); - $event->between($date_from, $date_to); + $event->from($dateFrom); - self::assertTrue( - $date_from === $event->getFrom() - ); + self::assertSame($dateFrom, $event->getFrom()); + } + + public function test_get_to(): void + { + $timezone = new \DateTimeZone('UTC'); + + $dateTo = '2023-10-30'; $event = new Event($this->id, 'php foo'); - $event->from($date_from); + $event->to($dateTo); - self::assertTrue( - $date_from === $event->getFrom() - ); + self::assertSame($dateTo, $event->getTo()); } - public function test_cron_life_time_get_to(): void + public function test_get_between(): void { $timezone = new \DateTimeZone('UTC'); - $date_from = '2023-10-01'; - $date_to = '2023-10-30'; + $dateFrom = '2023-10-01'; + $dateTo = '2023-10-30'; $event = new Event($this->id, 'php foo'); - $event->between($date_from, $date_to); + $event->between($dateFrom, $dateTo); - self::assertTrue( - $date_to === $event->getTo() - ); + self::assertSame($dateFrom, $event->getTo()); - $event = new Event($this->id, 'php foo'); - $event->to($date_to); - - self::assertTrue( - $date_to === $event->getTo() - ); + self::assertSame($dateTo, $event->getTo()); } public function test_cron_conditions(): void From acbf221b5ec9d419b4c723cd7c325280d637f2c5 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Fri, 3 Nov 2023 12:04:06 +0100 Subject: [PATCH 10/14] Typo --- tests/Unit/EventTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index e076d0c..2edfdab 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -188,7 +188,7 @@ public function test_get_between(): void $event = new Event($this->id, 'php foo'); $event->between($dateFrom, $dateTo); - self::assertSame($dateFrom, $event->getTo()); + self::assertSame($dateFrom, $event->getFrom()); self::assertSame($dateTo, $event->getTo()); } From 0420760b9c3d373dc61dddcc38960db486430ba9 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Fri, 3 Nov 2023 12:20:03 +0100 Subject: [PATCH 11/14] Removed unused code --- tests/Unit/EventTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index 2edfdab..a0334d6 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -156,8 +156,6 @@ public function test_cron_life_time(): void public function test_get_from(): void { - $timezone = new \DateTimeZone('UTC'); - $dateFrom = '2023-10-01'; $event = new Event($this->id, 'php foo'); @@ -168,8 +166,6 @@ public function test_get_from(): void public function test_get_to(): void { - $timezone = new \DateTimeZone('UTC'); - $dateTo = '2023-10-30'; $event = new Event($this->id, 'php foo'); From fe37b71c8e322a78d49f95b781eafc6bff565c35 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Fri, 3 Nov 2023 12:20:21 +0100 Subject: [PATCH 12/14] Removed unused code --- tests/Unit/EventTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index a0334d6..7415c47 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -176,8 +176,6 @@ public function test_get_to(): void public function test_get_between(): void { - $timezone = new \DateTimeZone('UTC'); - $dateFrom = '2023-10-01'; $dateTo = '2023-10-30'; From 4d4017d4a075c14357746a4131e7534652afe6f4 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Sun, 5 Nov 2023 21:43:09 +0100 Subject: [PATCH 13/14] Alignment with the production repo --- src/Event.php | 8 +++-- tests/Unit/EventTest.php | 76 ++++++++++++++++++++++++++++++++-------- 2 files changed, 68 insertions(+), 16 deletions(-) diff --git a/src/Event.php b/src/Event.php index f896ad6..b0df06d 100644 --- a/src/Event.php +++ b/src/Event.php @@ -459,7 +459,9 @@ public function from($datetime) { $this->from = $datetime; - return $this->skip(fn () => $this->notYet($datetime)); + return $this->skip( + fn (\DateTimeZone $timeZone) => $this->notYet($datetime, $timeZone) + ); } /** @@ -473,7 +475,9 @@ public function to($datetime) { $this->to = $datetime; - return $this->skip(fn () => $this->past($datetime)); + return $this->skip( + fn (\DateTimeZone $timeZone) => $this->past($datetime, $timeZone), + ); } /** diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index fe0e453..442f4af 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -154,37 +154,60 @@ public function test_cron_life_time(): void ); } - public function test_get_from(): void + /** + * @param \Closure(): array{ + * dateFrom: string, + * dateTo: string + * } $paramsGenerator + * + * @dataProvider dateFromToProvider + */ + public function test_get_from(\Closure $paramsGenerator): void { - $dateFrom = '2023-10-01'; + $params = $paramsGenerator(); $event = new Event($this->id, 'php foo'); - $event->from($dateFrom); + $event->from($params['dateFrom']); - self::assertSame($dateFrom, $event->getFrom()); + self::assertSame($params['dateFrom'], $event->getFrom()); } - public function test_get_to(): void + /** + * @param \Closure(): array{ + * dateFrom: string, + * dateTo: string + * } $paramsGenerator + * + * @dataProvider dateFromToProvider + */ + public function test_get_to(\Closure $paramsGenerator): void { - $dateTo = '2023-10-30'; + $params = $paramsGenerator(); $event = new Event($this->id, 'php foo'); - $event->to($dateTo); + $event->to($params['dateTo']); - self::assertSame($dateTo, $event->getTo()); + self::assertSame($params['dateTo'], $event->getTo()); } - public function test_get_between(): void + /** + * @param \Closure(): array{ + * dateFrom: string, + * dateTo: string + * } $paramsGenerator + * + * @dataProvider dateFromToProvider + */ + public function test_get_between(\Closure $paramsGenerator): void { - $dateFrom = '2023-10-01'; - $dateTo = '2023-10-30'; + $params = $paramsGenerator(); $event = new Event($this->id, 'php foo'); - $event->between($dateFrom, $dateTo); + $event->between($params['dateFrom'], $params['dateTo']); - self::assertSame($dateFrom, $event->getFrom()); + self::assertSame($params['dateFrom'], $event->getFrom()); - self::assertSame($dateTo, $event->getTo()); + self::assertSame($params['dateTo'], $event->getTo()); } public function test_cron_conditions(): void @@ -617,6 +640,31 @@ public function hourlyAtInvalidProvider(): iterable ]; } + /** @return iterable */ + public function dateFromToProvider(): iterable + { + yield 'dateFrom, dateTo with format yyyy-mm-dd' => [ + static fn (): array => [ + 'dateFrom' => (new \DateTime('+'. rand(1,59) .' days'))->format('Y-m-d'), + 'dateTo' => (new \DateTime('+'. rand(60,120) .' days'))->format('Y-m-d'), + ] + ]; + + yield 'dateFrom, dateTo with format H:i' => [ + static fn (): array => [ + 'dateFrom' => (new \DateTime('+'. rand(1, 29) .' minutes'))->format('H:i'), + 'dateTo' => (new \DateTime('+'. rand(30, 60) .' minutes'))->format('H:i'), + ] + ]; + + yield 'dateFrom, dateTo with format yyyy-mm-dd hh:mm' => [ + static fn (): array => [ + 'dateFrom' => (new \DateTime('+'. rand(1,59) .' days +'. rand(1, 29) .' minutes'))->format('Y-m-d H:i'), + 'dateTo' => (new \DateTime('+'. rand(60,120) .' days +'. rand(30, 60) .' minutes'))->format('Y-m-d H:i'), + ] + ]; + } + private function assertPreventOverlapping(PersistingStoreInterface $store = null): void { $event = $this->createPreventOverlappingEvent($store); From 4bbbd01b2e3fd72f97da2b8a7f553fe402e8ded6 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Sun, 5 Nov 2023 21:48:22 +0100 Subject: [PATCH 14/14] php-cs-fixer Issue fix --- tests/Unit/EventTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Unit/EventTest.php b/tests/Unit/EventTest.php index 442f4af..fcf6064 100644 --- a/tests/Unit/EventTest.php +++ b/tests/Unit/EventTest.php @@ -645,23 +645,23 @@ public function dateFromToProvider(): iterable { yield 'dateFrom, dateTo with format yyyy-mm-dd' => [ static fn (): array => [ - 'dateFrom' => (new \DateTime('+'. rand(1,59) .' days'))->format('Y-m-d'), - 'dateTo' => (new \DateTime('+'. rand(60,120) .' days'))->format('Y-m-d'), - ] + 'dateFrom' => (new \DateTime('+' . \mt_rand(1, 59) . ' days'))->format('Y-m-d'), + 'dateTo' => (new \DateTime('+' . \mt_rand(60, 120) . ' days'))->format('Y-m-d'), + ], ]; yield 'dateFrom, dateTo with format H:i' => [ static fn (): array => [ - 'dateFrom' => (new \DateTime('+'. rand(1, 29) .' minutes'))->format('H:i'), - 'dateTo' => (new \DateTime('+'. rand(30, 60) .' minutes'))->format('H:i'), - ] + 'dateFrom' => (new \DateTime('+' . \mt_rand(1, 29) . ' minutes'))->format('H:i'), + 'dateTo' => (new \DateTime('+' . \mt_rand(30, 60) . ' minutes'))->format('H:i'), + ], ]; yield 'dateFrom, dateTo with format yyyy-mm-dd hh:mm' => [ static fn (): array => [ - 'dateFrom' => (new \DateTime('+'. rand(1,59) .' days +'. rand(1, 29) .' minutes'))->format('Y-m-d H:i'), - 'dateTo' => (new \DateTime('+'. rand(60,120) .' days +'. rand(30, 60) .' minutes'))->format('Y-m-d H:i'), - ] + 'dateFrom' => (new \DateTime('+' . \mt_rand(1, 59) . ' days +' . \mt_rand(1, 29) . ' minutes'))->format('Y-m-d H:i'), + 'dateTo' => (new \DateTime('+' . \mt_rand(60, 120) . ' days +' . \mt_rand(30, 60) . ' minutes'))->format('Y-m-d H:i'), + ], ]; }