Skip to content

Commit 1ad9354

Browse files
authored
[10.x] Test Improvements for Auth tests (#50683)
* Test Improvements for Auth tests 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]>
1 parent 8a1a5a2 commit 1ad9354

File tree

4 files changed

+41
-43
lines changed

4 files changed

+41
-43
lines changed

tests/Integration/Auth/ApiAuthenticationWithEloquentTest.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@
33
namespace Illuminate\Tests\Integration\Auth;
44

55
use Illuminate\Database\QueryException;
6-
use Illuminate\Foundation\Auth\User as FoundationUser;
76
use Illuminate\Support\Facades\Route;
87
use Illuminate\Support\Str;
98
use Orchestra\Testbench\TestCase;
9+
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
1010

11-
/**
12-
* @requires extension pdo_mysql
13-
*/
11+
#[RequiresPhpExtension('pdo_mysql')]
1412
class ApiAuthenticationWithEloquentTest extends TestCase
1513
{
16-
protected function getEnvironmentSetUp($app)
14+
protected function defineEnvironment($app)
1715
{
1816
// Auth configuration
1917
$app['config']->set('auth.defaults.guard', 'api');
20-
$app['config']->set('auth.providers.users.model', User::class);
2118

2219
$app['config']->set('auth.guards.api', [
2320
'driver' => 'token',
@@ -59,8 +56,3 @@ public function testAuthenticationViaApiWithEloquentUsingWrongDatabaseCredential
5956
}
6057
}
6158
}
62-
63-
class User extends FoundationUser
64-
{
65-
//
66-
}

tests/Integration/Auth/AuthenticationTest.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,50 @@
1313
use Illuminate\Auth\SessionGuard;
1414
use Illuminate\Database\Schema\Blueprint;
1515
use Illuminate\Events\Dispatcher;
16+
use Illuminate\Foundation\Testing\RefreshDatabase;
1617
use Illuminate\Support\Facades\Auth;
1718
use Illuminate\Support\Facades\Event;
1819
use Illuminate\Support\Facades\Schema;
1920
use Illuminate\Support\Str;
2021
use Illuminate\Support\Testing\Fakes\EventFake;
2122
use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser;
2223
use InvalidArgumentException;
24+
use Orchestra\Testbench\Attributes\WithMigration;
2325
use Orchestra\Testbench\TestCase;
2426

27+
#[WithMigration]
2528
class AuthenticationTest extends TestCase
2629
{
27-
protected function getEnvironmentSetUp($app)
30+
use RefreshDatabase;
31+
32+
protected function defineEnvironment($app)
2833
{
29-
$app['config']->set('auth.providers.users.model', AuthenticationTestUser::class);
34+
$app['config']->set([
35+
'auth.providers.users.model' => AuthenticationTestUser::class,
36+
'hashing.driver' => 'bcrypt',
37+
]);
38+
}
3039

31-
$app['config']->set('hashing', ['driver' => 'bcrypt']);
40+
protected function defineRoutes($router)
41+
{
42+
$router->get('basic', function () {
43+
return $this->app['auth']->guard()->basic()
44+
?: $this->app['auth']->user()->toJson();
45+
});
46+
47+
$router->get('basicWithCondition', function () {
48+
return $this->app['auth']->guard()->basic('email', ['is_active' => true])
49+
?: $this->app['auth']->user()->toJson();
50+
});
3251
}
3352

34-
protected function setUp(): void
53+
protected function afterRefreshingDatabase()
3554
{
36-
parent::setUp();
37-
38-
Schema::create('users', function (Blueprint $table) {
39-
$table->increments('id');
40-
$table->string('email');
41-
$table->string('username');
42-
$table->string('password');
43-
$table->string('remember_token')->default(null)->nullable();
55+
Schema::table('users', function (Blueprint $table) {
56+
$table->renameColumn('name', 'username');
57+
});
58+
59+
Schema::table('users', function (Blueprint $table) {
4460
$table->tinyInteger('is_active')->default(0);
4561
});
4662

@@ -50,16 +66,6 @@ protected function setUp(): void
5066
'password' => bcrypt('password'),
5167
'is_active' => true,
5268
]);
53-
54-
$this->app->make('router')->get('basic', function () {
55-
return $this->app['auth']->guard()->basic()
56-
?: $this->app['auth']->user()->toJson();
57-
});
58-
59-
$this->app->make('router')->get('basicWithCondition', function () {
60-
return $this->app['auth']->guard()->basic('email', ['is_active' => true])
61-
?: $this->app['auth']->user()->toJson();
62-
});
6369
}
6470

6571
public function testBasicAuthProtectsRoute()

tests/Integration/Auth/ForgotPasswordTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
namespace Illuminate\Tests\Integration\Auth;
44

55
use Illuminate\Auth\Notifications\ResetPassword;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
67
use Illuminate\Notifications\Messages\MailMessage;
78
use Illuminate\Support\Facades\Notification;
89
use Illuminate\Support\Facades\Password;
910
use Illuminate\Support\Str;
1011
use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser;
12+
use Orchestra\Testbench\Attributes\WithMigration;
1113
use Orchestra\Testbench\Factories\UserFactory;
1214
use Orchestra\Testbench\TestCase;
1315

16+
#[WithMigration]
1417
class ForgotPasswordTest extends TestCase
1518
{
19+
use RefreshDatabase;
20+
1621
protected function tearDown(): void
1722
{
1823
ResetPassword::$createUrlCallback = null;
@@ -27,11 +32,6 @@ protected function defineEnvironment($app)
2732
$app['config']->set('auth.providers.users.model', AuthenticationTestUser::class);
2833
}
2934

30-
protected function defineDatabaseMigrations()
31-
{
32-
$this->loadLaravelMigrations();
33-
}
34-
3535
protected function defineRoutes($router)
3636
{
3737
$router->get('password/reset/{token}', function ($token) {

tests/Integration/Auth/ForgotPasswordWithoutDefaultRoutesTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
namespace Illuminate\Tests\Integration\Auth;
44

55
use Illuminate\Auth\Notifications\ResetPassword;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
67
use Illuminate\Notifications\Messages\MailMessage;
78
use Illuminate\Support\Facades\Notification;
89
use Illuminate\Support\Facades\Password;
910
use Illuminate\Support\Str;
1011
use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser;
12+
use Orchestra\Testbench\Attributes\WithMigration;
1113
use Orchestra\Testbench\Factories\UserFactory;
1214
use Orchestra\Testbench\TestCase;
1315

16+
#[WithMigration]
1417
class ForgotPasswordWithoutDefaultRoutesTest extends TestCase
1518
{
19+
use RefreshDatabase;
20+
1621
protected function tearDown(): void
1722
{
1823
ResetPassword::$createUrlCallback = null;
@@ -27,11 +32,6 @@ protected function defineEnvironment($app)
2732
$app['config']->set('auth.providers.users.model', AuthenticationTestUser::class);
2833
}
2934

30-
protected function defineDatabaseMigrations()
31-
{
32-
$this->loadLaravelMigrations();
33-
}
34-
3535
protected function defineRoutes($router)
3636
{
3737
$router->get('custom/password/reset/{token}', function ($token) {

0 commit comments

Comments
 (0)