-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
50e3505
commit df18843
Showing
8 changed files
with
258 additions
and
6 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
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
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
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
75 changes: 75 additions & 0 deletions
75
tests/Integration/Http/Controllers/Api/UserEmailVerifyControllerTest.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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Integration\Http\Controllers\Api; | ||
|
||
use App\Domain\Users\Repositories\UserRepository; | ||
use App\Domain\Users\Services\UserEmailVerifier; | ||
use App\Http\Controllers\Api\UserEmailVerifyController; | ||
use App\Http\Requests\EmailVerificationRequest; | ||
use App\Http\Requests\SendVerificationEmailRequest; | ||
use App\Models\User; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Tests\TestCase; | ||
|
||
final class UserEmailVerifyControllerTest extends TestCase | ||
{ | ||
private UserEmailVerifyController $subject; | ||
|
||
private UserRepository & MockObject $userRepository; | ||
|
||
private UserEmailVerifier & MockObject $emailVerifier; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->userRepository = $this->createMock(UserRepository::class); | ||
$this->emailVerifier = $this->createMock(UserEmailVerifier::class); | ||
$this->subject = new UserEmailVerifyController($this->emailVerifier); | ||
} | ||
|
||
public function testItVerifies(): void | ||
{ | ||
/** @var User $model */ | ||
$model = User::factory()->notVerified()->createOne(); | ||
$user = $model->toValueObject(); | ||
|
||
$this->userRepository | ||
->expects($this->once()) | ||
->method('findById') | ||
->with(null) | ||
->willReturn($user); | ||
|
||
$this->emailVerifier | ||
->expects($this->once()) | ||
->method('verify') | ||
->with($this->equalTo($user)); | ||
|
||
$this->subject | ||
->verifyEmail(new EmailVerificationRequest($this->userRepository)) | ||
->isRedirect($user->getOrigin()->withMessage('account.verified')->toString()); | ||
} | ||
|
||
public function testResendSends(): void | ||
{ | ||
/** @var User $model */ | ||
$model = User::factory()->notVerified()->createOne(); | ||
|
||
$this->userRepository | ||
->expects($this->once()) | ||
->method('findByEmail') | ||
->with($model->email) | ||
->willReturn($model->toValueObject()); | ||
|
||
$this->emailVerifier | ||
->expects($this->once()) | ||
->method('resend') | ||
->with($this->equalTo($model->toValueObject())); | ||
|
||
$this->subject->sendVerificationEmail( | ||
new SendVerificationEmailRequest($this->userRepository, [], [ 'email' => $model->email ]) | ||
); | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
tests/Integration/Services/Users/LaravelUserEmailVerifierTest.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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Integration\Services\Users; | ||
|
||
use App\Error\Auth\AlreadyVerified; | ||
use App\Models\User as UserModel; | ||
use App\Services\Users\LaravelUserEmailVerifier; | ||
use Illuminate\Auth\Notifications\VerifyEmail; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use Illuminate\Support\Facades\Notification; | ||
use Tests\TestCase; | ||
|
||
final class LaravelUserEmailVerifierTest extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
|
||
private LaravelUserEmailVerifier $subject; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->subject = new LaravelUserEmailVerifier(); | ||
} | ||
|
||
public function testVerifyThrowsOnVerifiedUser(): void | ||
{ | ||
/** @var UserModel $user */ | ||
$user = UserModel::factory()->createOne(); | ||
|
||
$this->expectException(AlreadyVerified::class); | ||
|
||
$this->subject->verify($user->toValueObject()); | ||
} | ||
|
||
public function testVerifyUser(): void | ||
{ | ||
/** @var UserModel $user */ | ||
$user = UserModel::factory()->notVerified()->createOne(); | ||
|
||
$this->subject->verify($user->toValueObject()); | ||
|
||
$user->refresh(); | ||
self::assertTrue($user->hasVerifiedEmail()); | ||
} | ||
|
||
public function testResendThrowsOnVerifiedUser(): void | ||
{ | ||
/** @var UserModel $user */ | ||
$user = UserModel::factory()->createOne(); | ||
|
||
$this->expectException(AlreadyVerified::class); | ||
|
||
$this->subject->resend($user->toValueObject()); | ||
} | ||
|
||
public function testResendMails(): void | ||
{ | ||
Notification::fake(); | ||
|
||
/** @var UserModel $user */ | ||
$user = UserModel::factory()->notVerified()->createOne(); | ||
|
||
$this->subject->resend($user->toValueObject()); | ||
|
||
Notification::assertSentTo($user, VerifyEmail::class); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
tests/Integration/Services/Users/LaravelUserRegistratorTest.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,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Integration\Services\Users; | ||
|
||
use App\Domain\Users\Commands\RegisterUser; | ||
use App\Services\Users\LaravelUserRegistrator; | ||
use Illuminate\Auth\Events\Registered; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use Illuminate\Support\Facades\Event; | ||
use Tests\TestCase; | ||
|
||
final class LaravelUserRegistratorTest extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->subject = new LaravelUserRegistrator(); | ||
} | ||
|
||
public function testRegisterCreatesANewUser(): void | ||
{ | ||
$registerUser = new RegisterUser( | ||
name: ':test:', | ||
email: 'user@example.com', | ||
password: ':password:', | ||
origin: 'https://origin.com', | ||
); | ||
|
||
$this->subject->register($registerUser); | ||
|
||
$this->assertDatabaseHas('users', [ | ||
'name' => $registerUser->name, | ||
'email' => $registerUser->email, | ||
'origin' => $registerUser->origin, | ||
'email_verified_at' => null, | ||
]); | ||
} | ||
|
||
public function testRegisterEmitEvent(): void | ||
{ | ||
Event::fake(); | ||
|
||
$registerUser = new RegisterUser( | ||
name: ':test:', | ||
email: 'user@example.com', | ||
password: ':password:', | ||
origin: 'https://origin.com', | ||
); | ||
|
||
$this->subject->register($registerUser); | ||
|
||
Event::assertDispatched(Registered::class); | ||
} | ||
} |