Skip to content

Test Improvements #490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"require-dev": {
"mockery/mockery": "^1.0",
"orchestra/testbench": "^6.0|^7.0|^8.0",
"orchestra/testbench": "^6.34|^7.31|^8.11",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.3"
},
Expand All @@ -33,9 +33,11 @@
},
"autoload-dev": {
"psr-4": {
"App\\Models\\": "tests/Fixtures/Models",
"Laravel\\Fortify\\Tests\\": "tests/"
}
"Laravel\\Fortify\\Tests\\": "tests/",
"App\\": "workbench/app/",
"Database\\Factories\\": "workbench/database/factories/"
},
"classmap": ["stubs/"]
},
"extra": {
"branch-alias": {
Expand All @@ -51,5 +53,11 @@
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"scripts": {
"post-autoload-dump": "@prepare",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"lint": "@php vendor/bin/phpstan analyse",
"test": "@php vendor/bin/phpunit"
}
}
5 changes: 5 additions & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
providers:
- Laravel\Fortify\FortifyServiceProvider

migrations:
- database/migrations
71 changes: 7 additions & 64 deletions tests/AuthenticatedSessionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
use Illuminate\Cache\RateLimiter;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Foundation\Auth\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Laravel\Fortify\Contracts\LoginViewResponse;
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
use Laravel\Fortify\Features;
use Laravel\Fortify\FortifyServiceProvider;
use Laravel\Fortify\LoginRateLimiter;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Mockery;
use PragmaRX\Google2FA\Google2FA;

class AuthenticatedSessionControllerTest extends OrchestraTestCase
{
use RefreshDatabase;

public function test_the_login_view_is_returned()
{
$this->mock(LoginViewResponse::class)
Expand All @@ -34,8 +36,6 @@ public function test_the_login_view_is_returned()

public function test_user_can_authenticate()
{
$this->loadLaravelMigrations(['--database' => 'testbench']);

TestAuthenticationSessionUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
Expand All @@ -56,12 +56,6 @@ public function test_user_is_redirected_to_challenge_when_using_two_factor_authe

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

$this->loadLaravelMigrations(['--database' => 'testbench']);

Schema::table('users', function ($table) {
$table->text('two_factor_secret')->nullable();
});

TestTwoFactorAuthenticationSessionUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
Expand Down Expand Up @@ -89,12 +83,6 @@ public function test_user_is_not_redirected_to_challenge_when_using_two_factor_a
Features::twoFactorAuthentication(['confirm' => true]),
]);

$this->loadLaravelMigrations(['--database' => 'testbench']);

Schema::table('users', function ($table) {
$table->text('two_factor_secret')->nullable();
});

TestTwoFactorAuthenticationSessionUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
Expand All @@ -120,10 +108,7 @@ public function test_user_is_redirected_to_challenge_when_using_two_factor_authe
Features::twoFactorAuthentication(['confirm' => true]),
]);

$this->loadLaravelMigrations(['--database' => 'testbench']);

Schema::table('users', function ($table) {
$table->text('two_factor_secret')->nullable();
$table->timestamp('two_factor_confirmed_at')->nullable();
});

Expand Down Expand Up @@ -153,12 +138,6 @@ public function test_user_can_authenticate_when_two_factor_challenge_is_disabled

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

$this->loadLaravelMigrations(['--database' => 'testbench']);

Schema::table('users', function ($table) {
$table->text('two_factor_secret')->nullable();
});

TestTwoFactorAuthenticationSessionUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
Expand All @@ -176,8 +155,6 @@ public function test_user_can_authenticate_when_two_factor_challenge_is_disabled

public function test_validation_exception_returned_on_failure()
{
$this->loadLaravelMigrations(['--database' => 'testbench']);

TestAuthenticationSessionUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
Expand Down Expand Up @@ -271,9 +248,6 @@ public function test_two_factor_challenge_can_be_passed_via_code()
{
app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);

$this->loadLaravelMigrations(['--database' => 'testbench']);
$this->artisan('migrate', ['--database' => 'testbench'])->run();

$tfaEngine = app(Google2FA::class);
$userSecret = $tfaEngine->generateSecretKey();
$validOtp = $tfaEngine->getCurrentOtp($userSecret);
Expand Down Expand Up @@ -302,12 +276,6 @@ public function test_two_factor_authentication_preserves_remember_me_selection()

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

$this->loadLaravelMigrations(['--database' => 'testbench']);

Schema::table('users', function ($table) {
$table->text('two_factor_secret')->nullable();
});

TestTwoFactorAuthenticationSessionUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
Expand All @@ -334,9 +302,6 @@ public function test_two_factor_challenge_fails_for_old_otp_and_zero_window()
Features::twoFactorAuthentication(['window' => 0]),
]);

$this->loadLaravelMigrations(['--database' => 'testbench']);
$this->artisan('migrate', ['--database' => 'testbench'])->run();

$tfaEngine = app(Google2FA::class);
$userSecret = $tfaEngine->generateSecretKey();
$currentTs = $tfaEngine->getTimestamp();
Expand Down Expand Up @@ -365,9 +330,6 @@ public function test_two_factor_challenge_can_be_passed_via_recovery_code()
{
app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);

$this->loadLaravelMigrations(['--database' => 'testbench']);
$this->artisan('migrate', ['--database' => 'testbench'])->run();

$user = TestTwoFactorAuthenticationSessionUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
Expand All @@ -392,9 +354,6 @@ public function test_two_factor_challenge_can_fail_via_recovery_code()
{
app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);

$this->loadLaravelMigrations(['--database' => 'testbench']);
$this->artisan('migrate', ['--database' => 'testbench'])->run();

$user = TestTwoFactorAuthenticationSessionUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
Expand All @@ -419,9 +378,6 @@ public function test_two_factor_challenge_requires_a_challenged_user()
{
app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);

$this->loadLaravelMigrations(['--database' => 'testbench']);
$this->artisan('migrate', ['--database' => 'testbench'])->run();

$response = $this->withSession([])->withoutExceptionHandling()->get('/two-factor-challenge');

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

$this->loadLaravelMigrations(['--database' => 'testbench']);

TestAuthenticationSessionUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
Expand All @@ -448,23 +402,12 @@ public function test_case_insensitive_usernames_can_be_used()
$response->assertRedirect('/home');
}

protected function getPackageProviders($app)
protected function defineEnvironment($app)
{
return [FortifyServiceProvider::class];
}

protected function getEnvironmentSetUp($app)
{
$app['migrator']->path(__DIR__.'/../database/migrations');

$app['config']->set('auth.providers.users.model', TestAuthenticationSessionUser::class);

$app['config']->set('database.default', 'testbench');
parent::defineEnvironment($app);

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
$app['config']->set([
'auth.providers.users.model' => TestAuthenticationSessionUser::class,
]);
}
}
Expand Down
35 changes: 15 additions & 20 deletions tests/ConfirmablePasswordControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
namespace Laravel\Fortify\Tests;

use Illuminate\Foundation\Auth\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Fortify\Contracts\ConfirmPasswordViewResponse;
use Laravel\Fortify\Fortify;

class ConfirmablePasswordControllerTest extends OrchestraTestCase
{
use RefreshDatabase;

protected $user;

protected function setUp(): void
{
parent::setUp();

$this->loadLaravelMigrations(['--database' => 'testbench']);

$this->artisan('migrate', ['--database' => 'testbench'])->run();
$this->afterApplicationCreated(function () {
$this->user = TestConfirmPasswordUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => bcrypt('secret'),
]);
});

$this->user = TestConfirmPasswordUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => bcrypt('secret'),
]);
parent::setUp();
}

public function test_the_confirm_password_view_is_returned()
Expand Down Expand Up @@ -147,18 +148,12 @@ public function test_password_confirmation_can_fail_with_json()
$response->assertJsonValidationErrors('password');
}

protected function getEnvironmentSetUp($app)
protected function defineEnvironment($app)
{
$app['migrator']->path(__DIR__.'/../database/migrations');

$app['config']->set('auth.providers.users.model', TestConfirmPasswordUser::class);

$app['config']->set('database.default', 'testbench');
parent::defineEnvironment($app);

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
$app['config']->set([
'auth.providers.users.model' => TestConfirmPasswordUser::class,
]);
}
}
Expand Down
17 changes: 11 additions & 6 deletions tests/OrchestraTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

namespace Laravel\Fortify\Tests;

use Laravel\Fortify\FortifyServiceProvider;
use Mockery;
use Laravel\Fortify\Features;
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;

abstract class OrchestraTestCase extends TestCase
{
public function tearDown(): void
use WithLaravelMigrations, WithWorkbench;

protected function defineEnvironment($app)
{
Mockery::close();
$app['config']->set(['database.default' => 'testing']);
}

protected function getPackageProviders($app)
protected function withConfirmedTwoFactorAuthentication($app)
{
return [FortifyServiceProvider::class];
$app['config']->set('fortify.features', [
Features::twoFactorAuthentication(['confirm' => true]),
]);
}
}
18 changes: 7 additions & 11 deletions tests/PasswordControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
namespace Laravel\Fortify\Tests;

use App\Actions\Fortify\UpdateUserPassword;
use App\Models\User;
use Database\Factories\UserFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Validation\ValidationException;
use Laravel\Fortify\Contracts\UpdatesUserPasswords;
use Mockery;

class PasswordControllerTest extends OrchestraTestCase
{
use RefreshDatabase;

public function test_passwords_can_be_updated()
{
$user = Mockery::mock(User::class);
$user = UserFactory::new()->create();

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

public function test_passwords_cannot_be_updated_without_current_password()
{
$user = Mockery::mock(User::class);

require_once __DIR__.'/../stubs/PasswordValidationRules.php';
require_once __DIR__.'/../stubs/UpdateUserPassword.php';
$user = UserFactory::new()->create();

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

public function test_passwords_cannot_be_updated_without_current_password_confirmation()
{
$user = Mockery::mock(User::class);

require_once __DIR__.'/../stubs/PasswordValidationRules.php';
require_once __DIR__.'/../stubs/UpdateUserPassword.php';
$user = UserFactory::new()->create();

try {
(new UpdateUserPassword())->update($user, [
Expand Down
Loading