Install Pest

This commit is contained in:
Bob Molitor committed 2025-10-16 14:22:58 +02:00
1 parent 81a98dd9f4
commit e8db0d9a5a
16 files changed
+1354 -541

No files matched your search

+1 -1
View File
@@ -51,4 +51,4 @@ jobs:
run: npm run build run: npm run build
- name: Run Tests - name: Run Tests
run: ./vendor/bin/phpunit run: ./vendor/bin/pest
+2 -1
View File
@@ -23,7 +23,8 @@
"laravel/sail": "^1.41", "laravel/sail": "^1.41",
"mockery/mockery": "^1.6", "mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.6", "nunomaduro/collision": "^8.6",
"phpunit/phpunit": "^11.5.3" "pestphp/pest": "^4.1",
"pestphp/pest-plugin-laravel": "^4.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
Generated
+1224 -313
View File
File diff suppressed because it is too large. Load diff
+10 -24
View File
@@ -1,26 +1,16 @@
<?php <?php
namespace Tests\Feature\Auth;
use App\Models\User; use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Fortify\Features; use Laravel\Fortify\Features;
use Livewire\Volt\Volt as LivewireVolt; use Livewire\Volt\Volt as LivewireVolt;
use Tests\TestCase;
class AuthenticationTest extends TestCase test('login screen can be rendered', function () {
{
use RefreshDatabase;
public function test_login_screen_can_be_rendered(): void
{
$response = $this->get(route('login')); $response = $this->get(route('login'));
$response->assertStatus(200); $response->assertStatus(200);
} });
public function test_users_can_authenticate_using_the_login_screen(): void test('users can authenticate using the login screen', function () {
{
$user = User::factory()->withoutTwoFactor()->create(); $user = User::factory()->withoutTwoFactor()->create();
$response = LivewireVolt::test('auth.login') $response = LivewireVolt::test('auth.login')
@@ -33,10 +23,9 @@ class AuthenticationTest extends TestCase
->assertRedirect(route('dashboard', absolute: false)); ->assertRedirect(route('dashboard', absolute: false));
$this->assertAuthenticated(); $this->assertAuthenticated();
} });
public function test_users_can_not_authenticate_with_invalid_password(): void test('users can not authenticate with invalid password', function () {
{
$user = User::factory()->create(); $user = User::factory()->create();
$response = LivewireVolt::test('auth.login') $response = LivewireVolt::test('auth.login')
@@ -47,10 +36,9 @@ class AuthenticationTest extends TestCase
$response->assertHasErrors('email'); $response->assertHasErrors('email');
$this->assertGuest(); $this->assertGuest();
} });
public function test_users_with_two_factor_enabled_are_redirected_to_two_factor_challenge(): void test('users with two factor enabled are redirected to two factor challenge', function () {
{
if (! Features::canManageTwoFactorAuthentication()) { if (! Features::canManageTwoFactorAuthentication()) {
$this->markTestSkipped('Two-factor authentication is not enabled.'); $this->markTestSkipped('Two-factor authentication is not enabled.');
} }
@@ -76,10 +64,9 @@ class AuthenticationTest extends TestCase
$response->assertRedirect(route('two-factor.login')); $response->assertRedirect(route('two-factor.login'));
$response->assertSessionHas('login.id', $user->id); $response->assertSessionHas('login.id', $user->id);
$this->assertGuest(); $this->assertGuest();
} });
public function test_users_can_logout(): void test('users can logout', function () {
{
$user = User::factory()->create(); $user = User::factory()->create();
$response = $this->actingAs($user)->post(route('logout')); $response = $this->actingAs($user)->post(route('logout'));
@@ -87,5 +74,4 @@ class AuthenticationTest extends TestCase
$response->assertRedirect(route('home')); $response->assertRedirect(route('home'));
$this->assertGuest(); $this->assertGuest();
} });
}
+11 -24
View File
@@ -1,29 +1,19 @@
<?php <?php
namespace Tests\Feature\Auth;
use App\Models\User; use App\Models\User;
use Illuminate\Auth\Events\Verified; use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL; use Illuminate\Support\Facades\URL;
use Tests\TestCase;
class EmailVerificationTest extends TestCase test('email verification screen can be rendered', function () {
{
use RefreshDatabase;
public function test_email_verification_screen_can_be_rendered(): void
{
$user = User::factory()->unverified()->create(); $user = User::factory()->unverified()->create();
$response = $this->actingAs($user)->get(route('verification.notice')); $response = $this->actingAs($user)->get(route('verification.notice'));
$response->assertStatus(200); $response->assertStatus(200);
} });
public function test_email_can_be_verified(): void test('email can be verified', function () {
{
$user = User::factory()->unverified()->create(); $user = User::factory()->unverified()->create();
Event::fake(); Event::fake();
@@ -38,12 +28,11 @@ class EmailVerificationTest extends TestCase
Event::assertDispatched(Verified::class); Event::assertDispatched(Verified::class);
$this->assertTrue($user->fresh()->hasVerifiedEmail()); expect($user->fresh()->hasVerifiedEmail())->toBeTrue();
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1'); $response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
} });
public function test_email_is_not_verified_with_invalid_hash(): void test('email is not verified with invalid hash', function () {
{
$user = User::factory()->unverified()->create(); $user = User::factory()->unverified()->create();
$verificationUrl = URL::temporarySignedRoute( $verificationUrl = URL::temporarySignedRoute(
@@ -54,11 +43,10 @@ class EmailVerificationTest extends TestCase
$this->actingAs($user)->get($verificationUrl); $this->actingAs($user)->get($verificationUrl);
$this->assertFalse($user->fresh()->hasVerifiedEmail()); expect($user->fresh()->hasVerifiedEmail())->toBeFalse();
} });
public function test_already_verified_user_visiting_verification_link_is_redirected_without_firing_event_again(): void test('already verified user visiting verification link is redirected without firing event again', function () {
{
$user = User::factory()->create([ $user = User::factory()->create([
'email_verified_at' => now(), 'email_verified_at' => now(),
]); ]);
@@ -74,7 +62,6 @@ class EmailVerificationTest extends TestCase
$this->actingAs($user)->get($verificationUrl) $this->actingAs($user)->get($verificationUrl)
->assertRedirect(route('dashboard', absolute: false).'?verified=1'); ->assertRedirect(route('dashboard', absolute: false).'?verified=1');
$this->assertTrue($user->fresh()->hasVerifiedEmail()); expect($user->fresh()->hasVerifiedEmail())->toBeTrue();
Event::assertNotDispatched(Verified::class); Event::assertNotDispatched(Verified::class);
} });
}
@@ -1,21 +1,11 @@
<?php <?php
namespace Tests\Feature\Auth;
use App\Models\User; use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PasswordConfirmationTest extends TestCase test('confirm password screen can be rendered', function () {
{
use RefreshDatabase;
public function test_confirm_password_screen_can_be_rendered(): void
{
$user = User::factory()->create(); $user = User::factory()->create();
$response = $this->actingAs($user)->get(route('password.confirm')); $response = $this->actingAs($user)->get(route('password.confirm'));
$response->assertStatus(200); $response->assertStatus(200);
} });
}
+8 -21
View File
@@ -1,27 +1,17 @@
<?php <?php
namespace Tests\Feature\Auth;
use App\Models\User; use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword; use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Notification;
use Livewire\Volt\Volt; use Livewire\Volt\Volt;
use Tests\TestCase;
class PasswordResetTest extends TestCase test('reset password link screen can be rendered', function () {
{
use RefreshDatabase;
public function test_reset_password_link_screen_can_be_rendered(): void
{
$response = $this->get(route('password.request')); $response = $this->get(route('password.request'));
$response->assertStatus(200); $response->assertStatus(200);
} });
public function test_reset_password_link_can_be_requested(): void test('reset password link can be requested', function () {
{
Notification::fake(); Notification::fake();
$user = User::factory()->create(); $user = User::factory()->create();
@@ -31,10 +21,9 @@ class PasswordResetTest extends TestCase
->call('sendPasswordResetLink'); ->call('sendPasswordResetLink');
Notification::assertSentTo($user, ResetPassword::class); Notification::assertSentTo($user, ResetPassword::class);
} });
public function test_reset_password_screen_can_be_rendered(): void test('reset password screen can be rendered', function () {
{
Notification::fake(); Notification::fake();
$user = User::factory()->create(); $user = User::factory()->create();
@@ -50,10 +39,9 @@ class PasswordResetTest extends TestCase
return true; return true;
}); });
} });
public function test_password_can_be_reset_with_valid_token(): void test('password can be reset with valid token', function () {
{
Notification::fake(); Notification::fake();
$user = User::factory()->create(); $user = User::factory()->create();
@@ -75,5 +63,4 @@ class PasswordResetTest extends TestCase
return true; return true;
}); });
} });
}
+4 -15
View File
@@ -1,24 +1,14 @@
<?php <?php
namespace Tests\Feature\Auth;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Volt\Volt; use Livewire\Volt\Volt;
use Tests\TestCase;
class RegistrationTest extends TestCase test('registration screen can be rendered', function () {
{
use RefreshDatabase;
public function test_registration_screen_can_be_rendered(): void
{
$response = $this->get(route('register')); $response = $this->get(route('register'));
$response->assertStatus(200); $response->assertStatus(200);
} });
public function test_new_users_can_register(): void test('new users can register', function () {
{
$response = Volt::test('auth.register') $response = Volt::test('auth.register')
->set('name', 'Test User') ->set('name', 'Test User')
->set('email', 'test@example.com') ->set('email', 'test@example.com')
@@ -31,5 +21,4 @@ class RegistrationTest extends TestCase
->assertRedirect(route('dashboard', absolute: false)); ->assertRedirect(route('dashboard', absolute: false));
$this->assertAuthenticated(); $this->assertAuthenticated();
} });
}
+4 -15
View File
@@ -1,19 +1,10 @@
<?php <?php
namespace Tests\Feature\Auth;
use App\Models\User; use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Fortify\Features; use Laravel\Fortify\Features;
use Livewire\Volt\Volt; use Livewire\Volt\Volt;
use Tests\TestCase;
class TwoFactorChallengeTest extends TestCase test('two factor challenge redirects to login when not authenticated', function () {
{
use RefreshDatabase;
public function test_two_factor_challenge_redirects_to_login_when_not_authenticated(): void
{
if (! Features::canManageTwoFactorAuthentication()) { if (! Features::canManageTwoFactorAuthentication()) {
$this->markTestSkipped('Two-factor authentication is not enabled.'); $this->markTestSkipped('Two-factor authentication is not enabled.');
} }
@@ -21,10 +12,9 @@ class TwoFactorChallengeTest extends TestCase
$response = $this->get(route('two-factor.login')); $response = $this->get(route('two-factor.login'));
$response->assertRedirect(route('login')); $response->assertRedirect(route('login'));
} });
public function test_two_factor_challenge_can_be_rendered(): void test('two factor challenge can be rendered', function () {
{
if (! Features::canManageTwoFactorAuthentication()) { if (! Features::canManageTwoFactorAuthentication()) {
$this->markTestSkipped('Two-factor authentication is not enabled.'); $this->markTestSkipped('Two-factor authentication is not enabled.');
} }
@@ -48,5 +38,4 @@ class TwoFactorChallengeTest extends TestCase
->call('login') ->call('login')
->assertRedirect(route('two-factor.login')) ->assertRedirect(route('two-factor.login'))
->assertOk(); ->assertOk();
} });
}
+4 -15
View File
@@ -1,27 +1,16 @@
<?php <?php
namespace Tests\Feature;
use App\Models\User; use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class DashboardTest extends TestCase test('guests are redirected to the login page', function () {
{
use RefreshDatabase;
public function test_guests_are_redirected_to_the_login_page(): void
{
$response = $this->get(route('dashboard')); $response = $this->get(route('dashboard'));
$response->assertRedirect(route('login')); $response->assertRedirect(route('login'));
} });
public function test_authenticated_users_can_visit_the_dashboard(): void test('authenticated users can visit the dashboard', function () {
{
$user = User::factory()->create(); $user = User::factory()->create();
$this->actingAs($user); $this->actingAs($user);
$response = $this->get(route('dashboard')); $response = $this->get(route('dashboard'));
$response->assertStatus(200); $response->assertStatus(200);
} });
}
+2 -13
View File
@@ -1,18 +1,7 @@
<?php <?php
namespace Tests\Feature; test('returns a successful response', function () {
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
use RefreshDatabase;
public function test_returns_a_successful_response(): void
{
$response = $this->get(route('home')); $response = $this->get(route('home'));
$response->assertStatus(200); $response->assertStatus(200);
} });
}
+5 -16
View File
@@ -1,19 +1,10 @@
<?php <?php
namespace Tests\Feature\Settings;
use App\Models\User; use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Livewire\Volt\Volt; use Livewire\Volt\Volt;
use Tests\TestCase;
class PasswordUpdateTest extends TestCase test('password can be updated', function () {
{
use RefreshDatabase;
public function test_password_can_be_updated(): void
{
$user = User::factory()->create([ $user = User::factory()->create([
'password' => Hash::make('password'), 'password' => Hash::make('password'),
]); ]);
@@ -28,11 +19,10 @@ class PasswordUpdateTest extends TestCase
$response->assertHasNoErrors(); $response->assertHasNoErrors();
$this->assertTrue(Hash::check('new-password', $user->refresh()->password)); expect(Hash::check('new-password', $user->refresh()->password))->toBeTrue();
} });
public function test_correct_password_must_be_provided_to_update_password(): void test('correct password must be provided to update password', function () {
{
$user = User::factory()->create([ $user = User::factory()->create([
'password' => Hash::make('password'), 'password' => Hash::make('password'),
]); ]);
@@ -46,5 +36,4 @@ class PasswordUpdateTest extends TestCase
->call('updatePassword'); ->call('updatePassword');
$response->assertHasErrors(['current_password']); $response->assertHasErrors(['current_password']);
} });
}
+17 -31
View File
@@ -1,25 +1,15 @@
<?php <?php
namespace Tests\Feature\Settings;
use App\Models\User; use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Volt\Volt; use Livewire\Volt\Volt;
use Tests\TestCase;
class ProfileUpdateTest extends TestCase test('profile page is displayed', function () {
{
use RefreshDatabase;
public function test_profile_page_is_displayed(): void
{
$this->actingAs($user = User::factory()->create()); $this->actingAs($user = User::factory()->create());
$this->get(route('profile.edit'))->assertOk(); $this->get(route('profile.edit'))->assertOk();
} });
public function test_profile_information_can_be_updated(): void test('profile information can be updated', function () {
{
$user = User::factory()->create(); $user = User::factory()->create();
$this->actingAs($user); $this->actingAs($user);
@@ -33,13 +23,12 @@ class ProfileUpdateTest extends TestCase
$user->refresh(); $user->refresh();
$this->assertEquals('Test User', $user->name); expect($user->name)->toEqual('Test User');
$this->assertEquals('test@example.com', $user->email); expect($user->email)->toEqual('test@example.com');
$this->assertNull($user->email_verified_at); expect($user->email_verified_at)->toBeNull();
} });
public function test_email_verification_status_is_unchanged_when_email_address_is_unchanged(): void test('email verification status is unchanged when email address is unchanged', function () {
{
$user = User::factory()->create(); $user = User::factory()->create();
$this->actingAs($user); $this->actingAs($user);
@@ -51,11 +40,10 @@ class ProfileUpdateTest extends TestCase
$response->assertHasNoErrors(); $response->assertHasNoErrors();
$this->assertNotNull($user->refresh()->email_verified_at); expect($user->refresh()->email_verified_at)->not->toBeNull();
} });
public function test_user_can_delete_their_account(): void test('user can delete their account', function () {
{
$user = User::factory()->create(); $user = User::factory()->create();
$this->actingAs($user); $this->actingAs($user);
@@ -68,12 +56,11 @@ class ProfileUpdateTest extends TestCase
->assertHasNoErrors() ->assertHasNoErrors()
->assertRedirect('/'); ->assertRedirect('/');
$this->assertNull($user->fresh()); expect($user->fresh())->toBeNull();
$this->assertFalse(auth()->check()); expect(auth()->check())->toBeFalse();
} });
public function test_correct_password_must_be_provided_to_delete_account(): void test('correct password must be provided to delete account', function () {
{
$user = User::factory()->create(); $user = User::factory()->create();
$this->actingAs($user); $this->actingAs($user);
@@ -84,6 +71,5 @@ class ProfileUpdateTest extends TestCase
$response->assertHasErrors(['password']); $response->assertHasErrors(['password']);
$this->assertNotNull($user->fresh()); expect($user->fresh())->not->toBeNull();
} });
}
@@ -1,21 +1,10 @@
<?php <?php
namespace Tests\Feature\Settings;
use App\Models\User; use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Fortify\Features; use Laravel\Fortify\Features;
use Livewire\Volt\Volt; use Livewire\Volt\Volt;
use Tests\TestCase;
class TwoFactorAuthenticationTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
beforeEach(function () {
if (! Features::canManageTwoFactorAuthentication()) { if (! Features::canManageTwoFactorAuthentication()) {
$this->markTestSkipped('Two-factor authentication is not enabled.'); $this->markTestSkipped('Two-factor authentication is not enabled.');
} }
@@ -24,10 +13,9 @@ class TwoFactorAuthenticationTest extends TestCase
'confirm' => true, 'confirm' => true,
'confirmPassword' => true, 'confirmPassword' => true,
]); ]);
} });
public function test_two_factor_settings_page_can_be_rendered(): void test('two factor settings page can be rendered', function () {
{
$user = User::factory()->withoutTwoFactor()->create(); $user = User::factory()->withoutTwoFactor()->create();
$this->actingAs($user) $this->actingAs($user)
@@ -36,20 +24,18 @@ class TwoFactorAuthenticationTest extends TestCase
->assertOk() ->assertOk()
->assertSee('Two Factor Authentication') ->assertSee('Two Factor Authentication')
->assertSee('Disabled'); ->assertSee('Disabled');
} });
public function test_two_factor_settings_page_requires_password_confirmation_when_enabled(): void test('two factor settings page requires password confirmation when enabled', function () {
{
$user = User::factory()->create(); $user = User::factory()->create();
$response = $this->actingAs($user) $response = $this->actingAs($user)
->get(route('two-factor.show')); ->get(route('two-factor.show'));
$response->assertRedirect(route('password.confirm')); $response->assertRedirect(route('password.confirm'));
} });
public function test_two_factor_settings_page_returns_forbidden_response_when_two_factor_is_disabled(): void test('two factor settings page returns forbidden response when two factor is disabled', function () {
{
config(['fortify.features' => []]); config(['fortify.features' => []]);
$user = User::factory()->create(); $user = User::factory()->create();
@@ -59,10 +45,9 @@ class TwoFactorAuthenticationTest extends TestCase
->get(route('two-factor.show')); ->get(route('two-factor.show'));
$response->assertForbidden(); $response->assertForbidden();
} });
public function test_two_factor_authentication_disabled_when_confirmation_abandoned_between_requests(): void test('two factor authentication disabled when confirmation abandoned between requests', function () {
{
$user = User::factory()->create(); $user = User::factory()->create();
$user->forceFill([ $user->forceFill([
@@ -82,5 +67,4 @@ class TwoFactorAuthenticationTest extends TestCase
'two_factor_secret' => null, 'two_factor_secret' => null,
'two_factor_recovery_codes' => null, 'two_factor_recovery_codes' => null,
]); ]);
} });
}
+47
View File
@@ -0,0 +1,47 @@
<?php
/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "pest()" function to bind a different classes or traits.
|
*/
pest()->extend(Tests\TestCase::class)
->use(Illuminate\Foundation\Testing\RefreshDatabase::class)
->in('Feature');
/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/
expect()->extend('toBeOne', function () {
return $this->toBe(1);
});
/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/
function something()
{
// ..
}
+3 -14
View File
@@ -1,16 +1,5 @@
<?php <?php
namespace Tests\Unit; test('that true is true', function () {
expect(true)->toBeTrue();
use Illuminate\Foundation\Testing\RefreshDatabase; });
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
use RefreshDatabase;
public function test_that_true_is_true(): void
{
$this->assertTrue(true);
}
}