forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleScheduledEventTest.php
More file actions
147 lines (116 loc) · 5.91 KB
/
ConsoleScheduledEventTest.php
File metadata and controls
147 lines (116 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace Illuminate\Tests\Console;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\EventMutex;
use Illuminate\Foundation\Application;
use Illuminate\Support\Carbon;
use Mockery as m;
use PHPUnit\Framework\TestCase;
class ConsoleScheduledEventTest extends TestCase
{
/**
* The default configuration timezone.
*
* @var string
*/
protected $defaultTimezone;
protected function setUp(): void
{
$this->defaultTimezone = date_default_timezone_get();
date_default_timezone_set('UTC');
}
protected function tearDown(): void
{
date_default_timezone_set($this->defaultTimezone);
Carbon::setTestNow(null);
m::close();
}
public function testBasicCronCompilation()
{
$app = m::mock(Application::class.'[isDownForMaintenance,environment]');
$app->shouldReceive('isDownForMaintenance')->andReturn(false);
$app->shouldReceive('environment')->andReturn('production');
$event = new Event(m::mock(EventMutex::class), 'php foo');
$this->assertSame('* * * * *', $event->getExpression());
$this->assertTrue($event->isDue($app));
$this->assertTrue($event->skip(function () {
return true;
})->isDue($app));
$this->assertFalse($event->skip(function () {
return true;
})->filtersPass($app));
$event = new Event(m::mock(EventMutex::class), 'php foo');
$this->assertSame('* * * * *', $event->getExpression());
$this->assertFalse($event->environments('local')->isDue($app));
$event = new Event(m::mock(EventMutex::class), 'php foo');
$this->assertSame('* * * * *', $event->getExpression());
$this->assertFalse($event->when(function () {
return false;
})->filtersPass($app));
$event = new Event(m::mock(EventMutex::class), 'php foo');
$this->assertSame('* * * * *', $event->getExpression());
$this->assertFalse($event->when(false)->filtersPass($app));
// chained rules should be commutative
$eventA = new Event(m::mock(EventMutex::class), 'php foo');
$eventB = new Event(m::mock(EventMutex::class), 'php foo');
$this->assertEquals(
$eventA->daily()->hourly()->getExpression(),
$eventB->hourly()->daily()->getExpression());
$eventA = new Event(m::mock(EventMutex::class), 'php foo');
$eventB = new Event(m::mock(EventMutex::class), 'php foo');
$this->assertEquals(
$eventA->weekdays()->hourly()->getExpression(),
$eventB->hourly()->weekdays()->getExpression());
}
public function testEventIsDueCheck()
{
$app = m::mock(Application::class.'[isDownForMaintenance,environment]');
$app->shouldReceive('isDownForMaintenance')->andReturn(false);
$app->shouldReceive('environment')->andReturn('production');
Carbon::setTestNow(Carbon::create(2015, 1, 1, 0, 0, 0));
$event = new Event(m::mock(EventMutex::class), 'php foo');
$this->assertSame('* * * * 4', $event->thursdays()->getExpression());
$this->assertTrue($event->isDue($app));
$event = new Event(m::mock(EventMutex::class), 'php foo');
$this->assertSame('0 19 * * 3', $event->wednesdays()->at('19:00')->timezone('EST')->getExpression());
$this->assertTrue($event->isDue($app));
}
public function testTimeBetweenChecks()
{
$app = m::mock(Application::class.'[isDownForMaintenance,environment]');
$app->shouldReceive('isDownForMaintenance')->andReturn(false);
$app->shouldReceive('environment')->andReturn('production');
Carbon::setTestNow(Carbon::now()->startOfDay()->addHours(9));
$event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
$this->assertTrue($event->between('8:00', '10:00')->filtersPass($app));
$event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
$this->assertTrue($event->between('9:00', '9:00')->filtersPass($app));
$event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
$this->assertTrue($event->between('23:00', '10:00')->filtersPass($app));
$event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
$this->assertTrue($event->between('8:00', '6:00')->filtersPass($app));
$event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
$this->assertFalse($event->between('10:00', '11:00')->filtersPass($app));
$event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
$this->assertFalse($event->between('10:00', '8:00')->filtersPass($app));
}
public function testTimeUnlessBetweenChecks()
{
$app = m::mock(Application::class.'[isDownForMaintenance,environment]');
$app->shouldReceive('isDownForMaintenance')->andReturn(false);
$app->shouldReceive('environment')->andReturn('production');
Carbon::setTestNow(Carbon::now()->startOfDay()->addHours(9));
$event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
$this->assertFalse($event->unlessBetween('8:00', '10:00')->filtersPass($app));
$event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
$this->assertFalse($event->unlessBetween('9:00', '9:00')->filtersPass($app));
$event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
$this->assertFalse($event->unlessBetween('23:00', '10:00')->filtersPass($app));
$event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
$this->assertFalse($event->unlessBetween('8:00', '6:00')->filtersPass($app));
$event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
$this->assertTrue($event->unlessBetween('10:00', '11:00')->filtersPass($app));
$event = new Event(m::mock(EventMutex::class), 'php foo', 'UTC');
$this->assertTrue($event->unlessBetween('10:00', '8:00')->filtersPass($app));
}
}