-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Commands\Admin; | ||
|
||
use App\Models\User; | ||
use Illuminate\Auth\Events\Verified; | ||
use Illuminate\Console\Command; | ||
|
||
class UserVerifyCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'admin:user:verify | ||
{ email : The email of the user to verify }'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Verify the email address of an user'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): int | ||
{ | ||
$email = $this->argument('email'); | ||
|
||
$this->info('Start verifying user with email "'.$email.'"'); | ||
|
||
/** @var User|null $user */ | ||
$user = User::where('email', $email)->first(); | ||
|
||
if ($user === null) { | ||
$this->error('User with email "'.$email.'" not found.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
if ($user->hasVerifiedEmail()) { | ||
$this->info('User with email "'.$email.'" already verified.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
$user->markEmailAsVerified(); | ||
event(new Verified($user)); | ||
|
||
$this->info('User with email "'.$email.'" has been verified.'); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
tests/Unit/Console/Commands/Admin/UserVerifyCommandTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Unit\Console\Commands\Admin; | ||
|
||
use App\Console\Commands\Admin\UserVerifyCommand; | ||
use App\Models\Member; | ||
use App\Models\Organization; | ||
use App\Models\User; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\UsesClass; | ||
use Tests\TestCaseWithDatabase; | ||
|
||
#[CoversClass(UserVerifyCommand::class)] | ||
#[UsesClass(UserVerifyCommand::class)] | ||
class UserVerifyCommandTest extends TestCaseWithDatabase | ||
{ | ||
public function test_it_verifies_user_email(): void | ||
{ | ||
// Arrange | ||
$organization = Organization::factory()->create(); | ||
$user = User::factory()->create([ | ||
'email_verified_at' => null, | ||
]); | ||
$member = Member::factory()->forUser($user)->forOrganization($organization)->create(); | ||
|
||
// Act | ||
$command = $this->artisan('admin:user:verify', ['email' => $user->email]); | ||
|
||
// Assert | ||
|
||
$command->expectsOutput('Start verifying user with email "'.$user->email.'"') | ||
->expectsOutput('User with email "'.$user->email.'" has been verified.') | ||
->assertExitCode(0); | ||
} | ||
|
||
public function test_it_fails_if_user_does_not_exist(): void | ||
{ | ||
// Arrange | ||
$email = '[email protected]'; | ||
$organization = Organization::factory()->create(); | ||
$user = User::factory()->create([ | ||
'email' => '[email protected]', | ||
'email_verified_at' => null, | ||
]); | ||
$member = Member::factory()->forUser($user)->forOrganization($organization)->create(); | ||
|
||
// Act | ||
$command = $this->artisan('admin:user:verify', ['email' => $email]); | ||
|
||
// Assert | ||
$command->expectsOutput('User with email "'.$email.'" not found.') | ||
->assertExitCode(1); | ||
} | ||
|
||
public function test_it_fails_if_user_email_is_already_verified(): void | ||
{ | ||
// Arrange | ||
$organization = Organization::factory()->create(); | ||
$user = User::factory()->create([ | ||
'email_verified_at' => now(), | ||
]); | ||
$member = Member::factory()->forUser($user)->forOrganization($organization)->create(); | ||
|
||
// Act | ||
$command = $this->artisan('admin:user:verify', ['email' => $user->email]); | ||
|
||
// Assert | ||
$command->expectsOutput('User with email "'.$user->email.'" already verified.') | ||
->assertExitCode(1); | ||
} | ||
} |