Skip to content

[1.x] Uses PHP Native Type Declarations 🐘 #421

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 2 commits into from
Jan 3, 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"autoload-dev": {
"psr-4": {
"App\\Models\\": "tests/Fixtures/Models",
"Laravel\\Fortify\\Tests\\": "tests/"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->text('two_factor_secret')
Expand All @@ -33,10 +31,8 @@ public function up()

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(array_merge([
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/CreatesNewUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface CreatesNewUsers
* Validate and create a newly registered user.
*
* @param array $input
* @return mixed
* @return \Illuminate\Foundation\Auth\User
*/
public function create(array $input);
}
12 changes: 4 additions & 8 deletions src/Contracts/ResetsUserPasswords.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

namespace Laravel\Fortify\Contracts;

/**
* @method void reset(\Illuminate\Foundation\Auth\User $user, array $input)
*/
interface ResetsUserPasswords
{
/**
* Validate and reset the user's forgotten password.
*
* @param mixed $user
* @param array $input
* @return void
*/
public function reset($user, array $input);
//
}
12 changes: 4 additions & 8 deletions src/Contracts/UpdatesUserPasswords.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

namespace Laravel\Fortify\Contracts;

/**
* @method void update(\Illuminate\Foundation\Auth\User $user, array $input)
*/
interface UpdatesUserPasswords
{
/**
* Validate and update the user's password.
*
* @param mixed $user
* @param array $input
* @return void
*/
public function update($user, array $input);
//
}
12 changes: 4 additions & 8 deletions src/Contracts/UpdatesUserProfileInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

namespace Laravel\Fortify\Contracts;

/**
* @method void update(\Illuminate\Foundation\Auth\User $user, array $input)
*/
interface UpdatesUserProfileInformation
{
/**
* Validate and update the given user's profile information.
*
* @param mixed $user
* @param array $input
* @return void
*/
public function update($user, array $input);
//
}
5 changes: 2 additions & 3 deletions stubs/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ class CreateNewUser implements CreatesNewUsers
/**
* Validate and create a newly registered user.
*
* @param array $input
* @return \App\Models\User
* @param array<string, string> $input
*/
public function create(array $input)
public function create(array $input): User
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
Expand Down
8 changes: 2 additions & 6 deletions stubs/FortifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@ class FortifyServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
public function register(): void
{
//
}

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
public function boot(): void
{
Fortify::createUsersUsing(CreateNewUser::class);
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Expand Down
4 changes: 2 additions & 2 deletions stubs/PasswordValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ trait PasswordValidationRules
/**
* Get the validation rules used to validate passwords.
*
* @return array
* @return array<int, \Illuminate\Contracts\Validation\Rule|array|string>
*/
protected function passwordRules()
protected function passwordRules(): array
{
return ['required', 'string', new Password, 'confirmed'];
}
Expand Down
7 changes: 3 additions & 4 deletions stubs/ResetUserPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Actions\Fortify;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\ResetsUserPasswords;
Expand All @@ -13,11 +14,9 @@ class ResetUserPassword implements ResetsUserPasswords
/**
* Validate and reset the user's forgotten password.
*
* @param mixed $user
* @param array $input
* @return void
* @param array<string, string> $input
*/
public function reset($user, array $input)
public function reset(User $user, array $input): void
{
Validator::make($input, [
'password' => $this->passwordRules(),
Expand Down
7 changes: 3 additions & 4 deletions stubs/UpdateUserPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Actions\Fortify;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\UpdatesUserPasswords;
Expand All @@ -13,11 +14,9 @@ class UpdateUserPassword implements UpdatesUserPasswords
/**
* Validate and update the user's password.
*
* @param mixed $user
* @param array $input
* @return void
* @param array<string, string> $input
*/
public function update($user, array $input)
public function update(User $user, array $input): void
{
Validator::make($input, [
'current_password' => ['required', 'string', 'current_password:web'],
Expand Down
13 changes: 5 additions & 8 deletions stubs/UpdateUserProfileInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Actions\Fortify;

use App\Models\User;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
Expand All @@ -12,11 +13,9 @@ class UpdateUserProfileInformation implements UpdatesUserProfileInformation
/**
* Validate and update the given user's profile information.
*
* @param mixed $user
* @param array $input
* @return void
* @param array<string, string> $input
*/
public function update($user, array $input)
public function update(User $user, array $input): void
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
Expand Down Expand Up @@ -44,11 +43,9 @@ public function update($user, array $input)
/**
* Update the given verified user's profile information.
*
* @param mixed $user
* @param array $input
* @return void
* @param array<string, string> $input
*/
protected function updateVerifiedUser($user, array $input)
protected function updateVerifiedUser(User $user, array $input): void
{
$user->forceFill([
'name' => $input['name'],
Expand Down
38 changes: 38 additions & 0 deletions tests/Fixtures/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Models;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nunomaduro I'd use Laravel\Fortify\Tests\Models for this.


use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];

/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
13 changes: 4 additions & 9 deletions tests/PasswordControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace Laravel\Fortify\Tests;

use App\Actions\Fortify\UpdateUserPassword;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use Illuminate\Validation\ValidationException;
use Laravel\Fortify\Contracts\UpdatesUserPasswords;
use Mockery;
Expand All @@ -13,9 +12,7 @@ class PasswordControllerTest extends OrchestraTestCase
{
public function test_passwords_can_be_updated()
{
$user = Mockery::mock(Authenticatable::class);

$user->password = Hash::make('password');
$user = Mockery::mock(User::class);

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

public function test_passwords_cannot_be_updated_without_current_password()
{
$user = Mockery::mock(Authenticatable::class);
$user->password = '';
$user = Mockery::mock(User::class);

require_once __DIR__.'/../stubs/PasswordValidationRules.php';
require_once __DIR__.'/../stubs/UpdateUserPassword.php';
Expand All @@ -53,8 +49,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(Authenticatable::class);
$user->password = '';
$user = Mockery::mock(User::class);

require_once __DIR__.'/../stubs/PasswordValidationRules.php';
require_once __DIR__.'/../stubs/UpdateUserPassword.php';
Expand Down