|
2 | 2 |
|
3 | 3 | namespace Illuminate\Tests\Integration\Foundation; |
4 | 4 |
|
| 5 | +use DateTimeInterface; |
5 | 6 | use Illuminate\Foundation\Console\DownCommand; |
6 | 7 | use Illuminate\Foundation\Console\UpCommand; |
7 | 8 | use Illuminate\Foundation\Events\MaintenanceModeDisabled; |
|
12 | 13 | use Illuminate\Support\Facades\Event; |
13 | 14 | use Illuminate\Support\Facades\Route; |
14 | 15 | use Orchestra\Testbench\TestCase; |
| 16 | +use PHPUnit\Framework\Attributes\DataProvider; |
15 | 17 | use Symfony\Component\HttpFoundation\Cookie; |
16 | 18 |
|
17 | 19 | class MaintenanceModeTest extends TestCase |
@@ -192,4 +194,62 @@ public function testDispatchEventWhenMaintenanceModeIsDisabled() |
192 | 194 | $this->artisan(UpCommand::class); |
193 | 195 | Event::assertDispatched(MaintenanceModeDisabled::class); |
194 | 196 | } |
| 197 | + |
| 198 | + #[DataProvider('retryAfterDatetimeProvider')] |
| 199 | + public function testMaintenanceModeRetryCanAcceptDatetime(string $datetime): void |
| 200 | + { |
| 201 | + $this->artisan(DownCommand::class, ['--retry' => $datetime]); |
| 202 | + |
| 203 | + $data = json_decode(file_get_contents(storage_path('framework/down')), true); |
| 204 | + |
| 205 | + $expectedDate = Carbon::parse($datetime)->format(DateTimeInterface::RFC7231); |
| 206 | + $this->assertSame($expectedDate, $data['retry']); |
| 207 | + } |
| 208 | + |
| 209 | + public static function retryAfterDatetimeProvider(): array |
| 210 | + { |
| 211 | + return [ |
| 212 | + 'ISO 8601 format' => [date('Y-m-d H:i:s', strtotime('+1 week'))], |
| 213 | + 'natural language' => ['tomorrow 14:00'], |
| 214 | + 'relative time' => ['+2 hours'], |
| 215 | + ]; |
| 216 | + } |
| 217 | + |
| 218 | + public function testMaintenanceModeRetryWithHttpDateHeader(): void |
| 219 | + { |
| 220 | + $retryDate = Carbon::now()->addWeek(); |
| 221 | + $expectedHeader = $retryDate->format(DateTimeInterface::RFC7231); |
| 222 | + |
| 223 | + file_put_contents(storage_path('framework/down'), json_encode([ |
| 224 | + 'retry' => $expectedHeader, |
| 225 | + ])); |
| 226 | + |
| 227 | + Route::get('/foo', fn () => 'Hello World')->middleware(PreventRequestsDuringMaintenance::class); |
| 228 | + |
| 229 | + $response = $this->get('/foo'); |
| 230 | + |
| 231 | + $response->assertStatus(503); |
| 232 | + $response->assertHeader('Retry-After', $expectedHeader); |
| 233 | + } |
| 234 | + |
| 235 | + public function testMaintenanceModeRetryWithInvalidDatetimeReturnsNull(): void |
| 236 | + { |
| 237 | + $this->artisan(DownCommand::class, ['--retry' => 'not-a-valid-date']); |
| 238 | + |
| 239 | + $data = json_decode(file_get_contents(storage_path('framework/down')), true); |
| 240 | + |
| 241 | + $this->assertNull($data['retry']); |
| 242 | + } |
| 243 | + |
| 244 | + public function testMaintenanceModeRetryWithAtTimestampNotation(): void |
| 245 | + { |
| 246 | + $futureTimestamp = time() + 3600; |
| 247 | + |
| 248 | + $this->artisan(DownCommand::class, ['--retry' => '@'.$futureTimestamp]); |
| 249 | + |
| 250 | + $data = json_decode(file_get_contents(storage_path('framework/down')), true); |
| 251 | + |
| 252 | + $expectedDate = Carbon::createFromTimestamp($futureTimestamp)->format(DateTimeInterface::RFC7231); |
| 253 | + $this->assertSame($expectedDate, $data['retry']); |
| 254 | + } |
195 | 255 | } |
0 commit comments