Skip to content

Commit

Permalink
[8.x] Dispatch events when maintenance mode is enabled and disabled (#…
Browse files Browse the repository at this point in the history
…38826)

* Add event classes

* Add tests

* Dispatch events when maintenance mode is enabled and disabled

* Remove empty constructors from event classes
  • Loading branch information
adevade authored Sep 15, 2021
1 parent b86e57e commit 5398fbb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Illuminate/Foundation/Console/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Middleware\PreventRequestsDuringMaintenance;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Foundation\Events\MaintenanceModeEnabled;
use Illuminate\Foundation\Exceptions\RegisterErrorViewPaths;
use Throwable;

Expand Down Expand Up @@ -53,6 +54,8 @@ public function handle()
file_get_contents(__DIR__.'/stubs/maintenance-mode.stub')
);

$this->laravel->get('events')->dispatch(MaintenanceModeEnabled::class);

$this->comment('Application is now in maintenance mode.');
} catch (Exception $e) {
$this->error('Failed to enter maintenance mode.');
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Foundation/Console/UpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Console\Command;
use Illuminate\Foundation\Events\MaintenanceModeDisabled;

class UpCommand extends Command
{
Expand Down Expand Up @@ -41,6 +42,8 @@ public function handle()
unlink(storage_path('framework/maintenance.php'));
}

$this->laravel->get('events')->dispatch(MaintenanceModeDisabled::class);

$this->info('Application is now live.');
} catch (Exception $e) {
$this->error('Failed to disable maintenance mode.');
Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Foundation/Events/MaintenanceModeDisabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Foundation\Events;

class MaintenanceModeDisabled
{
//
}
8 changes: 8 additions & 0 deletions src/Illuminate/Foundation/Events/MaintenanceModeEnabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Foundation\Events;

class MaintenanceModeEnabled
{
//
}
28 changes: 28 additions & 0 deletions tests/Integration/Foundation/MaintenanceModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace Illuminate\Tests\Integration\Foundation;

use Illuminate\Foundation\Console\DownCommand;
use Illuminate\Foundation\Console\UpCommand;
use Illuminate\Foundation\Events\MaintenanceModeDisabled;
use Illuminate\Foundation\Events\MaintenanceModeEnabled;
use Illuminate\Foundation\Http\MaintenanceModeBypassCookie;
use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Orchestra\Testbench\TestCase;
use Symfony\Component\HttpFoundation\Cookie;
Expand Down Expand Up @@ -146,4 +151,27 @@ public function testCanCreateBypassCookies()

Carbon::setTestNow(null);
}

public function testDispatchEventWhenMaintenanceModeIsEnabled()
{
Event::fake();

Event::assertNotDispatched(MaintenanceModeEnabled::class);
$this->artisan(DownCommand::class);
Event::assertDispatched(MaintenanceModeEnabled::class);
}

public function testDispatchEventWhenMaintenanceModeIsDisabled()
{
file_put_contents(storage_path('framework/down'), json_encode([
'retry' => 60,
'refresh' => 60,
]));

Event::fake();

Event::assertNotDispatched(MaintenanceModeDisabled::class);
$this->artisan(UpCommand::class);
Event::assertDispatched(MaintenanceModeDisabled::class);
}
}

0 comments on commit 5398fbb

Please sign in to comment.