Skip to content

Commit b4a7565

Browse files
committed
Autoload middleware if StageFront is enabled
1 parent 9e41b60 commit b4a7565

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

src/StageFrontServiceProvider.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace CodeZero\StageFront;
44

5+
use CodeZero\StageFront\Middleware\RedirectIfStageFrontIsEnabled;
6+
use Illuminate\Contracts\Http\Kernel;
57
use Illuminate\Support\ServiceProvider;
68

79
class StageFrontServiceProvider extends ServiceProvider
@@ -23,6 +25,7 @@ public function boot()
2325
$this->loadRoutes();
2426
$this->loadViews();
2527
$this->loadTranslations();
28+
$this->loadMiddleware();
2629
$this->registerPublishableFiles();
2730
}
2831

@@ -66,6 +69,20 @@ protected function loadTranslations()
6669
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', $this->name);
6770
}
6871

72+
/**
73+
* Load package middleware.
74+
*
75+
* @return void
76+
*/
77+
protected function loadMiddleware()
78+
{
79+
if (config('stagefront.enabled') === true) {
80+
app(Kernel::class)->prependMiddleware(
81+
RedirectIfStageFrontIsEnabled::class
82+
);
83+
}
84+
}
85+
6986
/**
7087
* Register the publishable files.
7188
*

tests/Feature/StageFrontTest.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace CodeZero\StageFront\Tests\Feature;
44

5+
use CodeZero\StageFront\Middleware\RedirectIfStageFrontIsEnabled;
56
use CodeZero\StageFront\Tests\Stubs\User;
67
use CodeZero\StageFront\Tests\TestCase;
8+
use Illuminate\Contracts\Http\Kernel;
79
use Route;
810

911
class StageFrontTest extends TestCase
@@ -12,9 +14,9 @@ public function setUp()
1214
{
1315
parent::setUp();
1416

15-
// Note that changing the URL or middleware config in the tests has no effect.
16-
// These settings are used in the routes file, which has already been
17-
// loaded before the tests run.
17+
// Note that changing the URL or middleware config in the tests
18+
// has no effect until you call $this->enableStageFront().
19+
// It is disabled by default so nothing is loaded by default.
1820
$this->url = config('stagefront.url');
1921

2022
Route::get('/page', function () {
@@ -45,11 +47,10 @@ public function the_login_route_does_not_exist_when_stagefront_is_disabled()
4547
/** @test */
4648
public function it_redirects_to_the_intended_url_when_you_provide_valid_credentials()
4749
{
48-
$this->enableStageFront();
49-
5050
config()->set('stagefront.login', 'tester');
5151
config()->set('stagefront.password', 'p4ssw0rd');
5252

53+
$this->enableStageFront();
5354
$this->setIntendedUrl('/page');
5455

5556
$response = $this->submitForm([
@@ -63,11 +64,10 @@ public function it_redirects_to_the_intended_url_when_you_provide_valid_credenti
6364
/** @test */
6465
public function it_does_not_allow_access_when_you_provide_invalid_credentials()
6566
{
66-
$this->enableStageFront();
67-
6867
config()->set('stagefront.login', 'tester');
6968
config()->set('stagefront.password', 'p4ssw0rd');
7069

70+
$this->enableStageFront();
7171
$this->setIntendedUrl('/page');
7272

7373
$response = $this->submitForm([
@@ -82,12 +82,11 @@ public function it_does_not_allow_access_when_you_provide_invalid_credentials()
8282
/** @test */
8383
public function the_password_may_be_stored_encrypted()
8484
{
85-
$this->enableStageFront();
86-
8785
config()->set('stagefront.login', 'tester');
8886
config()->set('stagefront.password', bcrypt('p4ssw0rd'));
8987
config()->set('stagefront.encrypted', true);
9088

89+
$this->enableStageFront();
9190
$this->setIntendedUrl('/page');
9291

9392
$response = $this->submitForm([
@@ -109,13 +108,12 @@ public function the_users_in_the_database_can_be_used_for_logging_in()
109108
'password' => bcrypt('str0ng p4ssw0rd'),
110109
]);
111110

112-
$this->enableStageFront();
113-
114111
config()->set('stagefront.database', true);
115112
config()->set('stagefront.database_table', 'users');
116113
config()->set('stagefront.database_login_field', 'email');
117114
config()->set('stagefront.database_password_field', 'password');
118115

116+
$this->enableStageFront();
119117
$this->setIntendedUrl('/page');
120118

121119
$response = $this->submitForm([
@@ -147,14 +145,14 @@ public function you_can_limit_which_database_users_have_access()
147145
'password' => bcrypt('str0ng p4ssw0rd'),
148146
]);
149147

150-
$this->enableStageFront();
151-
152148
config()->set('stagefront.database', true);
153149
config()->set('stagefront.database_whitelist', 'john@doe.io,jane@doe.io');
154150
config()->set('stagefront.database_table', 'users');
155151
config()->set('stagefront.database_login_field', 'email');
156152
config()->set('stagefront.database_password_field', 'password');
157153

154+
$this->enableStageFront();
155+
158156
$this->setIntendedUrl('/page')->submitForm([
159157
'login' => 'john@doe.io',
160158
'password' => 'str0ng p4ssw0rd',
@@ -208,7 +206,7 @@ protected function submitForm(array $credentials)
208206

209207
/**
210208
* Enable StageFront.
211-
* Routes haven't been loaded, so we should do this now.
209+
* Routes and middleware haven't been loaded, so we should do this now.
212210
*
213211
* @return void
214212
*/
@@ -217,5 +215,9 @@ protected function enableStageFront()
217215
config()->set('stagefront.enabled', true);
218216

219217
include __DIR__.'/../../routes/routes.php';
218+
219+
app(Kernel::class)->prependMiddleware(
220+
RedirectIfStageFrontIsEnabled::class
221+
);
220222
}
221223
}

tests/TestCase.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace CodeZero\StageFront\Tests;
44

5-
use CodeZero\StageFront\Middleware\RedirectIfStageFrontIsEnabled;
65
use CodeZero\StageFront\StageFrontServiceProvider;
7-
use Illuminate\Contracts\Http\Kernel;
86
use Orchestra\Testbench\TestCase as BaseTestCase;
97

108
abstract class TestCase extends BaseTestCase
@@ -17,10 +15,6 @@ public function setUp()
1715
parent::setUp();
1816

1917
config()->set('app.key', str_random(32));
20-
21-
app(Kernel::class)->prependMiddleware(
22-
RedirectIfStageFrontIsEnabled::class
23-
);
2418
}
2519

2620
/**

0 commit comments

Comments
 (0)