Files
monica/tests/Commands/Scheduling/CronEventTest.php
T
2019-12-31 11:58:45 -05:00

120 lines
3.0 KiB
PHP

<?php
namespace Tests\Commands\Scheduling;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\Instance\Cron;
use App\Console\Scheduling\CronEvent;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class CronEventTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_get_the_right_command()
{
$cron = factory(Cron::class)->create();
$event = CronEvent::command($cron->command);
$this->assertEquals($event->cron()->id, $cron->id);
}
/** @test */
public function now_not_due()
{
$cron = factory(Cron::class)->create();
$event = new CronEvent($cron);
$this->assertFalse($event->isDue());
}
/** @test */
public function next_minute_is_due()
{
Carbon::setTestNow(Carbon::create(2019, 5, 1, 7, 0, 0));
$cron = factory(Cron::class)->create();
$event = new CronEvent($cron);
$this->assertFalse($event->isDue());
Carbon::setTestNow(Carbon::create(2019, 5, 1, 7, 1, 0));
$this->assertTrue($event->isDue());
$this->assertDatabaseHas('crons', [
'command' => $cron->command,
'last_run' => '2019-05-01 07:01:00',
]);
}
/** @test */
public function hourly_cron()
{
Carbon::setTestNow(Carbon::create(2019, 5, 1, 7, 0, 0));
$cron = factory(Cron::class)->create();
$event = new CronEvent($cron);
$event->hourly();
$this->assertFalse($event->isDue());
Carbon::setTestNow(Carbon::create(2019, 5, 1, 8, 22, 0));
$this->assertTrue($event->isDue());
$this->assertDatabaseHas('crons', [
'command' => $cron->command,
'last_run' => '2019-05-01 08:22:00',
]);
Carbon::setTestNow(Carbon::create(2019, 5, 1, 8, 59, 0));
$this->assertFalse($event->isDue());
Carbon::setTestNow(Carbon::create(2019, 5, 1, 9, 01, 0));
$this->assertTrue($event->isDue());
$this->assertDatabaseHas('crons', [
'command' => $cron->command,
'last_run' => '2019-05-01 09:01:00',
]);
}
/** @test */
public function daily_cron()
{
Carbon::setTestNow(Carbon::create(2019, 5, 1, 7, 0, 0));
$cron = factory(Cron::class)->create();
$event = new CronEvent($cron);
$event->daily();
Carbon::setTestNow(Carbon::create(2019, 5, 2, 8, 10, 0));
$this->assertTrue($event->isDue());
$this->assertDatabaseHas('crons', [
'command' => $cron->command,
'last_run' => '2019-05-02 08:10:00',
]);
Carbon::setTestNow(Carbon::create(2019, 5, 2, 10, 0, 0));
$this->assertFalse($event->isDue());
Carbon::setTestNow(Carbon::create(2019, 5, 3, 0, 0, 0));
$this->assertTrue($event->isDue());
$this->assertDatabaseHas('crons', [
'command' => $cron->command,
'last_run' => '2019-05-03 00:00:00',
]);
}
}