Skip to content

Commit 2ae4865

Browse files
Test Improvements (#490)
* Test Improvements Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * Test Improvements Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]>
1 parent d6c05e2 commit 2ae4865

10 files changed

+90
-171
lines changed

composer.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"require-dev": {
2424
"mockery/mockery": "^1.0",
25-
"orchestra/testbench": "^6.0|^7.0|^8.0",
25+
"orchestra/testbench": "^6.34|^7.31|^8.11",
2626
"phpstan/phpstan": "^1.10",
2727
"phpunit/phpunit": "^9.3"
2828
},
@@ -33,9 +33,11 @@
3333
},
3434
"autoload-dev": {
3535
"psr-4": {
36-
"App\\Models\\": "tests/Fixtures/Models",
37-
"Laravel\\Fortify\\Tests\\": "tests/"
38-
}
36+
"Laravel\\Fortify\\Tests\\": "tests/",
37+
"App\\": "workbench/app/",
38+
"Database\\Factories\\": "workbench/database/factories/"
39+
},
40+
"classmap": ["stubs/"]
3941
},
4042
"extra": {
4143
"branch-alias": {
@@ -51,5 +53,11 @@
5153
"sort-packages": true
5254
},
5355
"minimum-stability": "dev",
54-
"prefer-stable": true
56+
"prefer-stable": true,
57+
"scripts": {
58+
"post-autoload-dump": "@prepare",
59+
"prepare": "@php vendor/bin/testbench package:discover --ansi",
60+
"lint": "@php vendor/bin/phpstan analyse",
61+
"test": "@php vendor/bin/phpunit"
62+
}
5563
}

testbench.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
providers:
2+
- Laravel\Fortify\FortifyServiceProvider
3+
4+
migrations:
5+
- database/migrations

tests/AuthenticatedSessionControllerTest.php

Lines changed: 7 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@
55
use Illuminate\Cache\RateLimiter;
66
use Illuminate\Contracts\Auth\Authenticatable;
77
use Illuminate\Foundation\Auth\User;
8+
use Illuminate\Foundation\Testing\RefreshDatabase;
89
use Illuminate\Http\Request;
910
use Illuminate\Support\Facades\Auth;
1011
use Illuminate\Support\Facades\Event;
1112
use Illuminate\Support\Facades\Schema;
1213
use Laravel\Fortify\Contracts\LoginViewResponse;
1314
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
1415
use Laravel\Fortify\Features;
15-
use Laravel\Fortify\FortifyServiceProvider;
1616
use Laravel\Fortify\LoginRateLimiter;
1717
use Laravel\Fortify\TwoFactorAuthenticatable;
1818
use Mockery;
1919
use PragmaRX\Google2FA\Google2FA;
2020

2121
class AuthenticatedSessionControllerTest extends OrchestraTestCase
2222
{
23+
use RefreshDatabase;
24+
2325
public function test_the_login_view_is_returned()
2426
{
2527
$this->mock(LoginViewResponse::class)
@@ -34,8 +36,6 @@ public function test_the_login_view_is_returned()
3436

3537
public function test_user_can_authenticate()
3638
{
37-
$this->loadLaravelMigrations(['--database' => 'testbench']);
38-
3939
TestAuthenticationSessionUser::forceCreate([
4040
'name' => 'Taylor Otwell',
4141
'email' => '[email protected]',
@@ -56,12 +56,6 @@ public function test_user_is_redirected_to_challenge_when_using_two_factor_authe
5656

5757
app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);
5858

59-
$this->loadLaravelMigrations(['--database' => 'testbench']);
60-
61-
Schema::table('users', function ($table) {
62-
$table->text('two_factor_secret')->nullable();
63-
});
64-
6559
TestTwoFactorAuthenticationSessionUser::forceCreate([
6660
'name' => 'Taylor Otwell',
6761
'email' => '[email protected]',
@@ -89,12 +83,6 @@ public function test_user_is_not_redirected_to_challenge_when_using_two_factor_a
8983
Features::twoFactorAuthentication(['confirm' => true]),
9084
]);
9185

92-
$this->loadLaravelMigrations(['--database' => 'testbench']);
93-
94-
Schema::table('users', function ($table) {
95-
$table->text('two_factor_secret')->nullable();
96-
});
97-
9886
TestTwoFactorAuthenticationSessionUser::forceCreate([
9987
'name' => 'Taylor Otwell',
10088
'email' => '[email protected]',
@@ -120,10 +108,7 @@ public function test_user_is_redirected_to_challenge_when_using_two_factor_authe
120108
Features::twoFactorAuthentication(['confirm' => true]),
121109
]);
122110

123-
$this->loadLaravelMigrations(['--database' => 'testbench']);
124-
125111
Schema::table('users', function ($table) {
126-
$table->text('two_factor_secret')->nullable();
127112
$table->timestamp('two_factor_confirmed_at')->nullable();
128113
});
129114

@@ -153,12 +138,6 @@ public function test_user_can_authenticate_when_two_factor_challenge_is_disabled
153138

154139
app('config')->set('fortify.features', $features);
155140

156-
$this->loadLaravelMigrations(['--database' => 'testbench']);
157-
158-
Schema::table('users', function ($table) {
159-
$table->text('two_factor_secret')->nullable();
160-
});
161-
162141
TestTwoFactorAuthenticationSessionUser::forceCreate([
163142
'name' => 'Taylor Otwell',
164143
'email' => '[email protected]',
@@ -176,8 +155,6 @@ public function test_user_can_authenticate_when_two_factor_challenge_is_disabled
176155

177156
public function test_validation_exception_returned_on_failure()
178157
{
179-
$this->loadLaravelMigrations(['--database' => 'testbench']);
180-
181158
TestAuthenticationSessionUser::forceCreate([
182159
'name' => 'Taylor Otwell',
183160
'email' => '[email protected]',
@@ -271,9 +248,6 @@ public function test_two_factor_challenge_can_be_passed_via_code()
271248
{
272249
app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);
273250

274-
$this->loadLaravelMigrations(['--database' => 'testbench']);
275-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
276-
277251
$tfaEngine = app(Google2FA::class);
278252
$userSecret = $tfaEngine->generateSecretKey();
279253
$validOtp = $tfaEngine->getCurrentOtp($userSecret);
@@ -302,12 +276,6 @@ public function test_two_factor_authentication_preserves_remember_me_selection()
302276

303277
app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);
304278

305-
$this->loadLaravelMigrations(['--database' => 'testbench']);
306-
307-
Schema::table('users', function ($table) {
308-
$table->text('two_factor_secret')->nullable();
309-
});
310-
311279
TestTwoFactorAuthenticationSessionUser::forceCreate([
312280
'name' => 'Taylor Otwell',
313281
'email' => '[email protected]',
@@ -334,9 +302,6 @@ public function test_two_factor_challenge_fails_for_old_otp_and_zero_window()
334302
Features::twoFactorAuthentication(['window' => 0]),
335303
]);
336304

337-
$this->loadLaravelMigrations(['--database' => 'testbench']);
338-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
339-
340305
$tfaEngine = app(Google2FA::class);
341306
$userSecret = $tfaEngine->generateSecretKey();
342307
$currentTs = $tfaEngine->getTimestamp();
@@ -365,9 +330,6 @@ public function test_two_factor_challenge_can_be_passed_via_recovery_code()
365330
{
366331
app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);
367332

368-
$this->loadLaravelMigrations(['--database' => 'testbench']);
369-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
370-
371333
$user = TestTwoFactorAuthenticationSessionUser::forceCreate([
372334
'name' => 'Taylor Otwell',
373335
'email' => '[email protected]',
@@ -392,9 +354,6 @@ public function test_two_factor_challenge_can_fail_via_recovery_code()
392354
{
393355
app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);
394356

395-
$this->loadLaravelMigrations(['--database' => 'testbench']);
396-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
397-
398357
$user = TestTwoFactorAuthenticationSessionUser::forceCreate([
399358
'name' => 'Taylor Otwell',
400359
'email' => '[email protected]',
@@ -419,9 +378,6 @@ public function test_two_factor_challenge_requires_a_challenged_user()
419378
{
420379
app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);
421380

422-
$this->loadLaravelMigrations(['--database' => 'testbench']);
423-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
424-
425381
$response = $this->withSession([])->withoutExceptionHandling()->get('/two-factor-challenge');
426382

427383
$response->assertRedirect('/login');
@@ -432,8 +388,6 @@ public function test_case_insensitive_usernames_can_be_used()
432388
{
433389
app('config')->set('fortify.lowercase_usernames', true);
434390

435-
$this->loadLaravelMigrations(['--database' => 'testbench']);
436-
437391
TestAuthenticationSessionUser::forceCreate([
438392
'name' => 'Taylor Otwell',
439393
'email' => '[email protected]',
@@ -448,23 +402,12 @@ public function test_case_insensitive_usernames_can_be_used()
448402
$response->assertRedirect('/home');
449403
}
450404

451-
protected function getPackageProviders($app)
405+
protected function defineEnvironment($app)
452406
{
453-
return [FortifyServiceProvider::class];
454-
}
455-
456-
protected function getEnvironmentSetUp($app)
457-
{
458-
$app['migrator']->path(__DIR__.'/../database/migrations');
459-
460-
$app['config']->set('auth.providers.users.model', TestAuthenticationSessionUser::class);
461-
462-
$app['config']->set('database.default', 'testbench');
407+
parent::defineEnvironment($app);
463408

464-
$app['config']->set('database.connections.testbench', [
465-
'driver' => 'sqlite',
466-
'database' => ':memory:',
467-
'prefix' => '',
409+
$app['config']->set([
410+
'auth.providers.users.model' => TestAuthenticationSessionUser::class,
468411
]);
469412
}
470413
}

tests/ConfirmablePasswordControllerTest.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@
33
namespace Laravel\Fortify\Tests;
44

55
use Illuminate\Foundation\Auth\User;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
67
use Laravel\Fortify\Contracts\ConfirmPasswordViewResponse;
78
use Laravel\Fortify\Fortify;
89

910
class ConfirmablePasswordControllerTest extends OrchestraTestCase
1011
{
12+
use RefreshDatabase;
13+
1114
protected $user;
1215

1316
protected function setUp(): void
1417
{
15-
parent::setUp();
16-
17-
$this->loadLaravelMigrations(['--database' => 'testbench']);
18-
19-
$this->artisan('migrate', ['--database' => 'testbench'])->run();
18+
$this->afterApplicationCreated(function () {
19+
$this->user = TestConfirmPasswordUser::forceCreate([
20+
'name' => 'Taylor Otwell',
21+
'email' => '[email protected]',
22+
'password' => bcrypt('secret'),
23+
]);
24+
});
2025

21-
$this->user = TestConfirmPasswordUser::forceCreate([
22-
'name' => 'Taylor Otwell',
23-
'email' => '[email protected]',
24-
'password' => bcrypt('secret'),
25-
]);
26+
parent::setUp();
2627
}
2728

2829
public function test_the_confirm_password_view_is_returned()
@@ -147,18 +148,12 @@ public function test_password_confirmation_can_fail_with_json()
147148
$response->assertJsonValidationErrors('password');
148149
}
149150

150-
protected function getEnvironmentSetUp($app)
151+
protected function defineEnvironment($app)
151152
{
152-
$app['migrator']->path(__DIR__.'/../database/migrations');
153-
154-
$app['config']->set('auth.providers.users.model', TestConfirmPasswordUser::class);
155-
156-
$app['config']->set('database.default', 'testbench');
153+
parent::defineEnvironment($app);
157154

158-
$app['config']->set('database.connections.testbench', [
159-
'driver' => 'sqlite',
160-
'database' => ':memory:',
161-
'prefix' => '',
155+
$app['config']->set([
156+
'auth.providers.users.model' => TestConfirmPasswordUser::class,
162157
]);
163158
}
164159
}

tests/OrchestraTestCase.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22

33
namespace Laravel\Fortify\Tests;
44

5-
use Laravel\Fortify\FortifyServiceProvider;
6-
use Mockery;
5+
use Laravel\Fortify\Features;
6+
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
7+
use Orchestra\Testbench\Concerns\WithWorkbench;
78
use Orchestra\Testbench\TestCase;
89

910
abstract class OrchestraTestCase extends TestCase
1011
{
11-
public function tearDown(): void
12+
use WithLaravelMigrations, WithWorkbench;
13+
14+
protected function defineEnvironment($app)
1215
{
13-
Mockery::close();
16+
$app['config']->set(['database.default' => 'testing']);
1417
}
1518

16-
protected function getPackageProviders($app)
19+
protected function withConfirmedTwoFactorAuthentication($app)
1720
{
18-
return [FortifyServiceProvider::class];
21+
$app['config']->set('fortify.features', [
22+
Features::twoFactorAuthentication(['confirm' => true]),
23+
]);
1924
}
2025
}

tests/PasswordControllerTest.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
namespace Laravel\Fortify\Tests;
44

55
use App\Actions\Fortify\UpdateUserPassword;
6-
use App\Models\User;
6+
use Database\Factories\UserFactory;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
78
use Illuminate\Validation\ValidationException;
89
use Laravel\Fortify\Contracts\UpdatesUserPasswords;
9-
use Mockery;
1010

1111
class PasswordControllerTest extends OrchestraTestCase
1212
{
13+
use RefreshDatabase;
14+
1315
public function test_passwords_can_be_updated()
1416
{
15-
$user = Mockery::mock(User::class);
17+
$user = UserFactory::new()->create();
1618

1719
$this->mock(UpdatesUserPasswords::class)
1820
->shouldReceive('update')
@@ -29,10 +31,7 @@ public function test_passwords_can_be_updated()
2931

3032
public function test_passwords_cannot_be_updated_without_current_password()
3133
{
32-
$user = Mockery::mock(User::class);
33-
34-
require_once __DIR__.'/../stubs/PasswordValidationRules.php';
35-
require_once __DIR__.'/../stubs/UpdateUserPassword.php';
34+
$user = UserFactory::new()->create();
3635

3736
try {
3837
(new UpdateUserPassword())->update($user, [
@@ -49,10 +48,7 @@ public function test_passwords_cannot_be_updated_without_current_password()
4948

5049
public function test_passwords_cannot_be_updated_without_current_password_confirmation()
5150
{
52-
$user = Mockery::mock(User::class);
53-
54-
require_once __DIR__.'/../stubs/PasswordValidationRules.php';
55-
require_once __DIR__.'/../stubs/UpdateUserPassword.php';
51+
$user = UserFactory::new()->create();
5652

5753
try {
5854
(new UpdateUserPassword())->update($user, [

0 commit comments

Comments
 (0)