diff --git a/tests/SPT/Domain/User/Services/UserPassTest.php b/tests/SPT/Domain/User/Services/UserPassTest.php new file mode 100644 index 00000000..042523d9 --- /dev/null +++ b/tests/SPT/Domain/User/Services/UserPassTest.php @@ -0,0 +1,91 @@ +. + */ + +namespace SPT\Domain\User\Services; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\Exception; +use SP\Core\Crypt\Hash; +use SP\Domain\Core\Exceptions\ConstraintException; +use SP\Domain\Core\Exceptions\QueryException; +use SP\Domain\User\Models\User as UserModel; +use SP\Domain\User\Ports\UserRepository; +use SP\Domain\User\Services\UserPass; +use SP\Infrastructure\Common\Repositories\NoSuchItemException; +use SPT\UnitaryTestCase; + +/**+ + * Class UserPassTest + */ +#[Group('unitary')] +class UserPassTest extends UnitaryTestCase +{ + + /** + * @throws Exception + * @throws ConstraintException + * @throws QueryException + * @throws NoSuchItemException + */ + public function testMigrateUserPassById() + { + $userRepository = $this->createMock(UserRepository::class); + $userRepository->expects($this->once()) + ->method('updatePassById') + ->with( + self::callback(static function (UserModel $user) { + return $user->getId() === 100 + && $user->isChangePass() === false + && $user->isChangedPass() === true + && $user->isMigrate() == false + && Hash::checkHashKey('a_password', $user->getPass()); + }) + ) + ->willReturn(1); + + $usePass = new UserPass($this->application, $userRepository); + $usePass->migrateUserPassById(100, 'a_password'); + } + + /** + * @throws Exception + * @throws ConstraintException + * @throws QueryException + * @throws NoSuchItemException + */ + public function testMigrateUserPassByIdWithException() + { + $userRepository = $this->createMock(UserRepository::class); + $userRepository->expects($this->once()) + ->method('updatePassById') + ->willReturn(0); + + $usePass = new UserPass($this->application, $userRepository); + + $this->expectException(NoSuchItemException::class); + $this->expectExceptionMessage('User does not exist'); + + $usePass->migrateUserPassById(100, 'a_password'); + } +}