Skip to content

Commit

Permalink
Merge pull request #6 from mohammedmanssour/simple-complex-queries
Browse files Browse the repository at this point in the history
adds whereActiveForTheDate, whereHasSimpleRecurringOn and whereHasCom…
  • Loading branch information
mohammedmanssour authored Aug 12, 2023
2 parents f089fb5 + fa41c01 commit 4137f6e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
20 changes: 13 additions & 7 deletions src/Models/Repetition.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,22 @@ public function repeatable(): MorphTo
/*-----------------------------------------------------
* scopes
-----------------------------------------------------*/
public function scopeWhereOccurresOn(Builder $query, Carbon $date): Builder
public function scopeWhereActiveForTheDate(Builder $query, Carbon $date): Builder
{
return $query->where('start_at', '<=', $date->toDateTimeString())
->where(
fn ($query) => $query->whereNull('end_at')
->orWhere('end_at', '>=', $date)
)
);
}

public function scopeWhereOccurresOn(Builder $query, Carbon $date): Builder
{
return $query
->WhereActiveForTheDate($date)
->where(
fn ($query) => $query->where(fn ($query) => $this->simpleQuery($query, $date))
->orWhere(fn ($query) => $this->complexQuery($query, $date))
fn ($query) => $query->whereHasSimpleRecurringOn($date)
->orWhere(fn ($query) => $query->whereHasComplexRecurringOn($date))
);
}

Expand All @@ -86,7 +92,7 @@ public function scopeWhereOccurresBetween(Builder $query, Carbon $start, Carbon
return $query;
}

private function simpleQuery(Builder $query, Carbon $date): Builder
public function scopeWhereHasSimpleRecurringOn(Builder $query, Carbon $date): Builder
{
$secondsInDay = 86400;

Expand All @@ -104,9 +110,9 @@ private function simpleQuery(Builder $query, Carbon $date): Builder
return $query;
}

private function complexQuery(Builder $query, Carbon $date): Builder
public function scopeWhereHasComplexRecurringOn(Builder $query, Carbon $date)
{
return $query->where('type', RepetitionType::Complex)
$query->where('type', RepetitionType::Complex)
->where(fn ($query) => $query->where('year', '*')->orWhere('year', $date->year))
->where(fn ($query) => $query->where('month', '*')->orWhere('month', $date->month))
->where(fn ($query) => $query->where('day', '*')->orWhere('day', $date->day))
Expand Down
45 changes: 36 additions & 9 deletions tests/RepetitionTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Illuminate\Support\Carbon;
use MohammedManssour\LaravelRecurringModels\Enums\RepetitionType;
use MohammedManssour\LaravelRecurringModels\Models\Repetition;
use MohammedManssour\LaravelRecurringModels\Tests\Stubs\Support\HasTask;
use MohammedManssour\LaravelRecurringModels\Tests\TestCase;
Expand All @@ -10,6 +9,25 @@ class RepetitionTest extends TestCase
{
use HasTask;

/**
* @test
* */
public function it_checks_if_recurring_is_active_for_the_date()
{
// repeat start at 2023-04-15 00:00:00 and ends at 2023-04-15
$repetition = $this->repetition($this->task(), '2023-04-30');

$this->assertNull(Repetition::query()->whereActiveForTheDate(Carbon::make('2023-04-14 00:00:00'))->first());

$this->assertNull(Repetition::query()->whereActiveForTheDate(Carbon::make('2023-05-01 00:00:00'))->first());

$activeTestDates = ['2023-04-15', '2023-04-16', '2023-04-30'];
foreach ($activeTestDates as $date) {
$model = Repetition::query()->whereActiveForTheDate(Carbon::make("{$date} 00:00:00"))->first();
$this->assertTrue($model->is($repetition));
}
}

/** @test */
public function it_checks_if_simple_repetition_occurres_on_specific_day()
{
Expand Down Expand Up @@ -40,6 +58,12 @@ public function it_checks_if_simple_repetition_occurres_on_specific_day()

$model = Repetition::whereOccurresOn(Carbon::make('2023-04-25 00:00:00'))->first();
$this->assertTrue($repetition->is($model));

$date = Carbon::make('2023-04-25 00:00:00');
$model = Repetition::whereHasSimpleRecurringOn($date)->first();
$this->assertTrue($repetition->is($model));

$this->assertNull(Repetition::whereHasComplexRecurringOn($date)->first());
}

/** @test */
Expand Down Expand Up @@ -82,15 +106,18 @@ public function it_checks_if_complex_repetition_occurres_on_specific_dates()
// repeats on second Friday of the month
$repetition = Repetition::factory()->morphs($this->task())->complex(week: 2, weekday: Carbon::FRIDAY)->starts($this->task()->repetitionBaseDate())->create();

$model = Repetition::whereOccurresOn(Carbon::make('2023-05-12'))->first();
$this->assertNotNull($model);
$this->assertEquals(RepetitionType::Complex, $model->type);
$this->assertEquals($repetition->id, $model->id);
$date = Carbon::make('2023-05-12');
$model = Repetition::whereOccurresOn($date)->first();
$this->assertTrue($model->is($repetition));

$model = Repetition::whereOccurresOn(Carbon::make('2023-05-05'))->first();
$this->assertNull($model);
$model = Repetition::whereHasComplexRecurringOn($date)->first();
$this->assertTrue($model->is($repetition));

$model = Repetition::whereOccurresOn(Carbon::make('2023-05-19'))->first();
$this->assertNull($model);
$model = Repetition::whereHasComplexRecurringOn($date)->first();
$this->assertTrue($model->is($repetition));

$this->assertNull(Repetition::whereHasSimpleRecurringOn($date)->first());
$this->assertNull(Repetition::whereOccurresOn(Carbon::make('2023-05-05'))->first());
$this->assertNull(Repetition::whereOccurresOn(Carbon::make('2023-05-19'))->first());
}
}

0 comments on commit 4137f6e

Please sign in to comment.