From 2f34ecef86670726685d7321076cffb515bd896e Mon Sep 17 00:00:00 2001 From: T1k3 Date: Wed, 19 Jun 2019 20:55:28 +0200 Subject: [PATCH 1/4] Add tests for nthweekday (rmenner) --- README.md | 10 ++++ src/Enums/RecurringFrequenceType.php | 1 + .../Command/GenerateCalendarEventTest.php | 48 +++++++++++++++++++ tests/Unit/Models/CalendarEventTest.php | 31 ++++++++++++ 4 files changed, 90 insertions(+) diff --git a/README.md b/README.md index 011f740..a9e86b6 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,13 @@ php artisan migrate ## Usage +#### Recurring options +- DAY +- WEEK +- MONTH +- YEAR +- NTHWEEKDAY: nth weekday per month, example 2nd Monday + #### Create CalendarEvent If you like to attach `User` and/or `Place` then must have: * configurate `config/calendar-event.php` @@ -134,3 +141,6 @@ start_date/start_time - end_date/end_time ### TODO - [ ] Name conventions, example: `TemplateCalendarEvent::events()` to `TemplateCalendarEvent::calendarEvents()` - [ ] [Custom validation rule](https://laravel.com/docs/master/validation#custom-validation-rules) to date/time diff + +### Special thanks +- [Bit and Pixel](https://bitandpixel.hu) diff --git a/src/Enums/RecurringFrequenceType.php b/src/Enums/RecurringFrequenceType.php index c4bf47f..641c831 100644 --- a/src/Enums/RecurringFrequenceType.php +++ b/src/Enums/RecurringFrequenceType.php @@ -12,4 +12,5 @@ abstract class RecurringFrequenceType const WEEK = 'week'; const MONTH = 'month'; const YEAR = 'year'; + const NTHWEEKDAY = 'nthweekday'; } \ No newline at end of file diff --git a/tests/Unit/Console/Command/GenerateCalendarEventTest.php b/tests/Unit/Console/Command/GenerateCalendarEventTest.php index c4e671c..1142fe5 100644 --- a/tests/Unit/Console/Command/GenerateCalendarEventTest.php +++ b/tests/Unit/Console/Command/GenerateCalendarEventTest.php @@ -195,6 +195,54 @@ public function dataProvider_for_handle_recurring_generated() Carbon::parse('2017-08-27'), Carbon::parse('2017-08-28'), ], + [ + [ + 'start_date' => '2019-06-19', + 'start_time' => '19:30', + 'end_date' => '2019-06-19', + 'end_time' => '20:00', + 'description' => str_random(32), + 'is_recurring' => true, + 'frequence_number_of_recurring' => 1, + 'frequence_type_of_recurring' => RecurringFrequenceType::NTHWEEKDAY, + 'is_public' => true, + ], + Carbon::parse('2019-07-15'), + Carbon::parse('2019-07-17'), + Carbon::parse('2019-07-17'), + ], + [ + [ + 'start_date' => '2019-06-19', + 'start_time' => '19:30', + 'end_date' => '2019-06-19', + 'end_time' => '20:00', + 'description' => str_random(32), + 'is_recurring' => true, + 'frequence_number_of_recurring' => 1, + 'frequence_type_of_recurring' => RecurringFrequenceType::NTHWEEKDAY, + 'is_public' => true, + ], + Carbon::parse('2019-08-19'), + Carbon::parse('2019-08-21'), + Carbon::parse('2019-08-21'), + ], + [ + [ + 'start_date' => '2019-06-19', + 'start_time' => '19:30', + 'end_date' => '2019-06-19', + 'end_time' => '20:00', + 'description' => str_random(32), + 'is_recurring' => true, + 'frequence_number_of_recurring' => 1, + 'frequence_type_of_recurring' => RecurringFrequenceType::NTHWEEKDAY, + 'is_public' => true, + ], + Carbon::parse('2019-09-16'), + Carbon::parse('2019-09-18'), + Carbon::parse('2019-09-18'), + ], ]; } diff --git a/tests/Unit/Models/CalendarEventTest.php b/tests/Unit/Models/CalendarEventTest.php index 5233b20..147b9c2 100644 --- a/tests/Unit/Models/CalendarEventTest.php +++ b/tests/Unit/Models/CalendarEventTest.php @@ -493,6 +493,37 @@ public function eventsOfMonth() $this->assertTrue($isExist); } } + + /** + * @test + */ + public function getEventsOfMonth_for_nthweekRecurring() + { + $this->calendarEvent->createCalendarEvent([ + 'start_date' => '2019-06-19', + 'start_time' => '19:30', + 'end_date' => '2019-06-19', + 'end_time' => '20:00', + 'description' => str_random(32), + 'is_recurring' => true, + 'frequence_number_of_recurring' => 1, + 'frequence_type_of_recurring' => RecurringFrequenceType::NTHWEEKDAY, + 'is_public' => true, + ]); + + + $calendarEvents = CalendarEvent::showPotentialCalendarEventsOfMonth(Carbon::parse('2019-07')); + $this->assertEquals(1, $calendarEvents->count()); + $this->assertEquals('2019-07-17', $calendarEvents->first()->start_date->format('Y-m-d')); + + $calendarEvents = CalendarEvent::showPotentialCalendarEventsOfMonth(Carbon::parse('2019-08')); + $this->assertEquals(1, $calendarEvents->count()); + $this->assertEquals('2019-08-21', $calendarEvents->first()->start_date->format('Y-m-d')); + + $calendarEvents = CalendarEvent::showPotentialCalendarEventsOfMonth(Carbon::parse('2019-09')); + $this->assertEquals(1, $calendarEvents->count()); + $this->assertEquals('2019-09-18', $calendarEvents->first()->start_date->format('Y-m-d')); + } /** * @test From 7488e6bf264babff7bcd35e8d632932fd7b9baf1 Mon Sep 17 00:00:00 2001 From: Ryan Menner Date: Tue, 25 Jun 2019 17:16:12 -0400 Subject: [PATCH 2/4] Nth-week logic created with helper function to get correct weekday --- .../Commands/GenerateCalendarEvent.php | 11 ++++++++++ src/Models/CalendarEvent.php | 10 ++++++++++ src/Models/TemplateCalendarEvent.php | 8 ++++++++ src/Support/helpers.php | 20 +++++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/src/Console/Commands/GenerateCalendarEvent.php b/src/Console/Commands/GenerateCalendarEvent.php index 591cd0d..65f4ddf 100644 --- a/src/Console/Commands/GenerateCalendarEvent.php +++ b/src/Console/Commands/GenerateCalendarEvent.php @@ -89,6 +89,17 @@ public function handle() // $endOfRecurring = (clone $date)->addYear(); $diff = $templateCalendarEvent->start_date->diffInYears($endOfRecurring); $dateNext = $templateCalendarEvent->start_date->addYears($diff); + break; + case RecurringFrequenceType::NTHWEEKDAY: + $diff = $date->firstOfMonth()->diffInMonths($templateCalendarEvent->start_date); + + $nextMonth = $templateCalendarEvent->start_date->addMonths($diff); + $weekdays = getWeekdaysInMonth( + $templateCalendarEvent->start_date->format('l'), + $nextMonth, + ); + + $dateNext = $weekdays[$templateCalendarEvent->start_date->weekOfMonth - 1]; } if ($dateNext !== null diff --git a/src/Models/CalendarEvent.php b/src/Models/CalendarEvent.php index d7dd6f1..fc714f4 100644 --- a/src/Models/CalendarEvent.php +++ b/src/Models/CalendarEvent.php @@ -241,6 +241,16 @@ public static function showPotentialCalendarEventsOfMonth(\DateTimeInterface $da case RecurringFrequenceType::YEAR: $diff = $date->firstOfMonth()->diffInYears($calendarEventTmpLast->start_date); $dateNext = $calendarEventTmpLast->start_date->addYears($diff); + break; + case RecurringFrequenceType::NTHWEEKDAY: + $diff = $date->firstOfMonth()->diffInMonths($calendarEventTmpLast->start_date); + $nextMonth = $calendarEventTmpLast->start_date->addMonths($diff); + + $weekdays = getWeekdaysInMonth( + $calendarEventTmpLast->start_date->format('l'), + $nextMonth, + ); + $dateNext = $weekdays[$calendarEventTmpLast->start_date->weekOfMonth - 1]; } } diff --git a/src/Models/TemplateCalendarEvent.php b/src/Models/TemplateCalendarEvent.php index 666c647..685d942 100644 --- a/src/Models/TemplateCalendarEvent.php +++ b/src/Models/TemplateCalendarEvent.php @@ -225,6 +225,14 @@ public function getNextCalendarEventStartDate(\DateTimeInterface $startDate) break; case RecurringFrequenceType::YEAR: $startDate->addYears($this->frequence_number_of_recurring); + break; + case RecurringFrequenceType::NTHWEEKDAY: + $nextMonth = $startDate->copy()->addMonths($this->frequence_number_of_recurring); + $weekdays = getWeekdaysInMonth( + $startDate->format('l'), + $nextMonth, + ); + $startDate = $weekdays[$startDate->weekOfMonth - 1]; } if (($this->end_of_recurring !== null diff --git a/src/Support/helpers.php b/src/Support/helpers.php index dbd819a..e7b1250 100644 --- a/src/Support/helpers.php +++ b/src/Support/helpers.php @@ -1,4 +1,6 @@ copy()->addMonths(1); + return collect(new \DatePeriod( + + Carbon::parse("first $weekday of $date"), + CarbonInterval::week(), + Carbon::parse("first $weekday of $next") + )); + } } \ No newline at end of file From 7bbe25fc7039dbdec6af634ed91145622f10cdea Mon Sep 17 00:00:00 2001 From: Ryan Menner Date: Tue, 25 Jun 2019 17:23:37 -0400 Subject: [PATCH 3/4] fix: removed extra comma --- src/Console/Commands/GenerateCalendarEvent.php | 4 ++-- src/Models/CalendarEvent.php | 2 +- src/Models/TemplateCalendarEvent.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Console/Commands/GenerateCalendarEvent.php b/src/Console/Commands/GenerateCalendarEvent.php index 65f4ddf..9be271e 100644 --- a/src/Console/Commands/GenerateCalendarEvent.php +++ b/src/Console/Commands/GenerateCalendarEvent.php @@ -96,9 +96,9 @@ public function handle() $nextMonth = $templateCalendarEvent->start_date->addMonths($diff); $weekdays = getWeekdaysInMonth( $templateCalendarEvent->start_date->format('l'), - $nextMonth, + $nextMonth ); - + $dateNext = $weekdays[$templateCalendarEvent->start_date->weekOfMonth - 1]; } diff --git a/src/Models/CalendarEvent.php b/src/Models/CalendarEvent.php index fc714f4..ee798ff 100644 --- a/src/Models/CalendarEvent.php +++ b/src/Models/CalendarEvent.php @@ -248,7 +248,7 @@ public static function showPotentialCalendarEventsOfMonth(\DateTimeInterface $da $weekdays = getWeekdaysInMonth( $calendarEventTmpLast->start_date->format('l'), - $nextMonth, + $nextMonth ); $dateNext = $weekdays[$calendarEventTmpLast->start_date->weekOfMonth - 1]; } diff --git a/src/Models/TemplateCalendarEvent.php b/src/Models/TemplateCalendarEvent.php index 685d942..dd09ea9 100644 --- a/src/Models/TemplateCalendarEvent.php +++ b/src/Models/TemplateCalendarEvent.php @@ -230,7 +230,7 @@ public function getNextCalendarEventStartDate(\DateTimeInterface $startDate) $nextMonth = $startDate->copy()->addMonths($this->frequence_number_of_recurring); $weekdays = getWeekdaysInMonth( $startDate->format('l'), - $nextMonth, + $nextMonth ); $startDate = $weekdays[$startDate->weekOfMonth - 1]; } From 6f607a948d3761f294ee9ac97b865fdfc753ccd7 Mon Sep 17 00:00:00 2001 From: Ryan Menner Date: Wed, 26 Jun 2019 12:19:30 -0400 Subject: [PATCH 4/4] fix: updated test to correct dates --- .../Console/Command/GenerateCalendarEventTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Unit/Console/Command/GenerateCalendarEventTest.php b/tests/Unit/Console/Command/GenerateCalendarEventTest.php index 1142fe5..c13ddfe 100644 --- a/tests/Unit/Console/Command/GenerateCalendarEventTest.php +++ b/tests/Unit/Console/Command/GenerateCalendarEventTest.php @@ -197,9 +197,9 @@ public function dataProvider_for_handle_recurring_generated() ], [ [ - 'start_date' => '2019-06-19', + 'start_date' => '2019-07-17', 'start_time' => '19:30', - 'end_date' => '2019-06-19', + 'end_date' => '2019-07-17', 'end_time' => '20:00', 'description' => str_random(32), 'is_recurring' => true, @@ -213,9 +213,9 @@ public function dataProvider_for_handle_recurring_generated() ], [ [ - 'start_date' => '2019-06-19', + 'start_date' => '2019-08-21', 'start_time' => '19:30', - 'end_date' => '2019-06-19', + 'end_date' => '2019-08-21', 'end_time' => '20:00', 'description' => str_random(32), 'is_recurring' => true, @@ -229,9 +229,9 @@ public function dataProvider_for_handle_recurring_generated() ], [ [ - 'start_date' => '2019-06-19', + 'start_date' => '2019-09-18', 'start_time' => '19:30', - 'end_date' => '2019-06-19', + 'end_date' => '2019-09-18', 'end_time' => '20:00', 'description' => str_random(32), 'is_recurring' => true,