Skip to content

Commit

Permalink
Override Illuminate up/down commands by default, refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rdarcy1 committed Sep 8, 2018
1 parent f6fba32 commit 01baa91
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 51 deletions.
14 changes: 14 additions & 0 deletions config/maintenance_mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
'driver' => 'file',


/*
|--------------------------------------------------------------------------
| Override Illuminate commands
|--------------------------------------------------------------------------
|
| If enabled, the default Laravel `artisan up` and `artisan down` commands
| will be overridden and aliased to the `artisan site:up` and `artisan
| site:down` commands from this package.
|
*/

'override_illuminate_commands' => true,


/*
|--------------------------------------------------------------------------
| Driver configuration
Expand Down
39 changes: 35 additions & 4 deletions src/MaintenanceModeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Konsulting\Laravel\MaintenanceMode;

use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Konsulting\Laravel\MaintenanceMode\Commands\SiteDownCommand;
use Konsulting\Laravel\MaintenanceMode\Commands\SiteUpCommand;
Expand All @@ -24,10 +25,9 @@ public function boot()
return new MaintenanceMode($this->getDriver());
});

$this->commands([
SiteDownCommand::class,
SiteUpCommand::class,
]);
if (config('maintenance_mode.override_illuminate_commands')) {
$this->overrideIlluminateCommands();
}
}

/**
Expand All @@ -38,6 +38,8 @@ public function boot()
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/maintenance_mode.php', 'maintenance_mode');

$this->registerCommands();
}

/**
Expand All @@ -53,4 +55,33 @@ protected function getDriver()

return $this->app->make($driverClass)->setConfig($config);
}

/**
* Override the default artisan up/down commands in the container.
*
* @return void
*/
protected function overrideIlluminateCommands()
{
$this->app->extend('command.up', function ($upCommand, Application $app) {
return $app->make(SiteUpCommand::class);
});

$this->app->extend('command.down', function ($downCommand, Application $app) {
return $app->make(SiteDownCommand::class);
});
}

/**
* Register the site:up and site:down commands.
*
* @return void
*/
protected function registerCommands()
{
$this->commands([
SiteDownCommand::class,
SiteUpCommand::class,
]);
}
}
26 changes: 0 additions & 26 deletions tests/Commands/SiteDownCommandTest.php

This file was deleted.

19 changes: 0 additions & 19 deletions tests/Commands/SiteUpCommandTest.php

This file was deleted.

42 changes: 42 additions & 0 deletions tests/Integration/CommandsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Konsulting\Laravel\MaintenanceMode\Tests\Integration;

use Konsulting\Laravel\MaintenanceMode\Commands\SiteDownCommand;
use Konsulting\Laravel\MaintenanceMode\Commands\SiteUpCommand;

class CommandsTest extends IntegrationTestCase
{
/** @test */
public function it_activates_maintenance_mode()
{
$this->artisan('site:down', [
'--message' => 'Test',
'--retry' => 123,
'--allow' => ['1.1.1.1', '2.2.2.2'],
]);

$this->assertTrue($this->maintenanceMode->isOn());
$data = $this->maintenanceMode->getDownInformation();
$this->assertSame('Test', $data->getMessage());
$this->assertSame(123, $data->getRetryTime());
$this->assertSame(['1.1.1.1', '2.2.2.2'], $data->getAllowedAddresses());
}

/** @test */
public function it_deactivates_maintenance_mode()
{
$this->maintenanceMode->on();
$this->assertTrue($this->maintenanceMode->isOn());

$this->artisan('site:up');
$this->assertFalse($this->maintenanceMode->isOn());
}

/** @test */
public function it_overrides_the_default_illuminate_commands_by_default()
{
$this->assertInstanceOf(SiteUpCommand::class, $this->app->make('command.up'));
$this->assertInstanceOf(SiteDownCommand::class, $this->app->make('command.down'));
}
}
23 changes: 23 additions & 0 deletions tests/Integration/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Konsulting\Laravel\MaintenanceMode\Tests\Integration;

use Illuminate\Foundation\Console\DownCommand;
use Illuminate\Foundation\Console\UpCommand;

class ConfigTest extends IntegrationTestCase
{
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app['config']->set('maintenance_mode.override_illuminate_commands', false);
}

/** @test */
public function it_does_not_override_the_default_illuminate_commands()
{
$this->assertInstanceOf(UpCommand::class, $this->app->make('command.up'));
$this->assertInstanceOf(DownCommand::class, $this->app->make('command.down'));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Konsulting\Laravel\MaintenanceMode\Tests\Commands;
namespace Konsulting\Laravel\MaintenanceMode\Tests\Integration;

use Konsulting\Laravel\MaintenanceMode\MaintenanceMode;
use Konsulting\Laravel\MaintenanceMode\MaintenanceModeProvider;
use Konsulting\Laravel\MaintenanceMode\Tests\TestCase;

class CommandTestCase extends TestCase
class IntegrationTestCase extends TestCase
{
/** @var MaintenanceMode */
protected $maintenanceMode;
Expand Down

0 comments on commit 01baa91

Please sign in to comment.