Skip to content

Commit

Permalink
before in hours (#2562)
Browse files Browse the repository at this point in the history
* fix: restrict to laravel 9

* fix: upgrade

* fix: wip

* fix: wip

* fix: wip

* fix: wip

* fix: phpstan

* fix: wip

* fix: wip

* fix: wip

* fix: wip

* fix: allow hours before event to schedule mailator

* Fix styling

* fix: fixing after or equal

Co-authored-by: binaryk <[email protected]>
  • Loading branch information
binaryk and binaryk authored Jun 27, 2022
1 parent 7934ab2 commit e165af9
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Constraints/AfterConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function canSend(MailatorSchedule $schedule, Collection $logs): bool
: $schedule->timestamp_target->diffInHours(now()->floorSeconds()) > $schedule->toHours();
}

if (now()->floorSeconds()->lt($schedule->timestampTarget()->addMinutes($schedule->delay_minutes))) {
if (now()->floorSeconds()->lte($schedule->timestampTarget()->addMinutes($schedule->delay_minutes))) {
return false;
}

Expand Down
32 changes: 28 additions & 4 deletions src/Constraints/BeforeConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,42 @@ public function canSend(MailatorSchedule $schedule, Collection $logs): bool
return true;
}

if (is_null($schedule->timestamp_target)) {
if (is_null($schedule->timestampTarget())) {
return true;
}

// if already expired
if ($schedule->timestamp_target->lte(now()->floorSeconds())) {
if ($schedule->timestampTarget()->lte(now()->floorSeconds())) {
return false;
}

if ($schedule->toDays() > 0) {
if (now()->floorSeconds()->gt($schedule->timestampTarget()->addDays($schedule->toDays()))) {
return false;
}

//till ends we should have at least toDays days
return $schedule->isOnce()
? $schedule->timestampTarget()->diffInDays(now()->floorSeconds()) === $schedule->toDays()
: $schedule->timestampTarget()->diffInDays(now()->floorSeconds()) < $schedule->toDays();
}

if ($schedule->toHours() > 0) {
if (now()->floorSeconds()->gt($schedule->timestampTarget()->addHours($schedule->toHours()))) {
return false;
}

//till ends we should have at least toHours days
return $schedule->isOnce()
? $schedule->timestamp_target->diffInHours(now()->floorSeconds()) === $schedule->toHours()
: $schedule->timestamp_target->diffInHours(now()->floorSeconds()) < $schedule->toHours();
}



//till ends we should have at least toDays days
return $schedule->isOnce()
? $schedule->timestamp_target->diffInDays(now()->floorSeconds()) === $schedule->toDays()
: $schedule->timestamp_target->diffInDays(now()->floorSeconds()) < $schedule->toDays();
? $schedule->timestampTarget()->diffInDays(now()->floorSeconds()) === $schedule->toDays()
: $schedule->timestampTarget()->diffInDays(now()->floorSeconds()) < $schedule->toDays();
}
}
91 changes: 91 additions & 0 deletions tests/Feature/BeforeConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Binarcode\LaravelMailator\Tests\Feature;

use Binarcode\LaravelMailator\Constraints\BeforeConstraint;
use Binarcode\LaravelMailator\Models\MailatorSchedule;
use Binarcode\LaravelMailator\Tests\Fixtures\InvoiceReminderMailable;
use Binarcode\LaravelMailator\Tests\TestCase;
use Illuminate\Support\Facades\Mail;

class BeforeConstraintTest extends TestCase
{
public function test_past_target_with_before_now_passed_after_constraint_day_bases(): void
{
Mail::fake();
Mail::assertNothingSent();

$scheduler = MailatorSchedule::init('Invoice reminder.')
->recipients($mail = '[email protected]')
->mailable(
(new InvoiceReminderMailable())->to('[email protected]')
)
->days(1)
->before(now()->addDays(2));

$scheduler->save();

$this->travel(1)->days();

self::assertFalse(
$scheduler->fresh()->isFutureAction()
);

$can = app(BeforeConstraint::class)->canSend(
$scheduler,
$scheduler->logs
);

self::assertTrue(
$can
);
}

public function test_past_target_with_before_now_passed_after_constraint_hourly_bases(): void
{
Mail::fake();
Mail::assertNothingSent();

$scheduler = MailatorSchedule::init('Invoice reminder.')
->recipients($mail = '[email protected]')
->mailable(
(new InvoiceReminderMailable())->to('[email protected]')
)
->hours(1)
->before(now()->addHours(3));

$scheduler->save();

$this->travel(1)->hours();

$this->travel(1)->hours();

$can = app(
BeforeConstraint::class
)->canSend(
$scheduler,
$scheduler->logs
);

self::assertTrue(
$can
);

$this->travel(1)->hours();

$can = app(
BeforeConstraint::class
)->canSend(
$scheduler,
$scheduler->logs
);

self::assertFalse(
$scheduler->fresh()->isFutureAction()
);

self::assertFalse(
$can
);
}
}

0 comments on commit e165af9

Please sign in to comment.