Skip to content

Commit

Permalink
Add command admin:user:verify
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Sep 13, 2024
1 parent 50902e7 commit 37400d2
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
59 changes: 59 additions & 0 deletions app/Console/Commands/Admin/UserVerifyCommand.php
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 tests/Unit/Console/Commands/Admin/UserVerifyCommandTest.php
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);
}
}

0 comments on commit 37400d2

Please sign in to comment.