From 21f392bb17e5efc456fc3a4d94979d72a4e279bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Fri, 19 Apr 2024 18:33:24 +0200 Subject: [PATCH] chore(tests): UT for LoginBase service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- lib/SP/Domain/Auth/Services/LoginBase.php | 2 +- .../Domain/Auth/Services/LoginBaseTest.php | 135 ++++++++++++++++++ 2 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 tests/SPT/Domain/Auth/Services/LoginBaseTest.php diff --git a/lib/SP/Domain/Auth/Services/LoginBase.php b/lib/SP/Domain/Auth/Services/LoginBase.php index 2dcd1f51..1433d98a 100644 --- a/lib/SP/Domain/Auth/Services/LoginBase.php +++ b/lib/SP/Domain/Auth/Services/LoginBase.php @@ -40,7 +40,7 @@ use function SP\__u; */ abstract class LoginBase extends Service { - private TrackRequest $trackRequest; + private readonly TrackRequest $trackRequest; /** * @throws InvalidArgumentException diff --git a/tests/SPT/Domain/Auth/Services/LoginBaseTest.php b/tests/SPT/Domain/Auth/Services/LoginBaseTest.php new file mode 100644 index 00000000..6bdacd79 --- /dev/null +++ b/tests/SPT/Domain/Auth/Services/LoginBaseTest.php @@ -0,0 +1,135 @@ +. + */ + +namespace SPT\Domain\Auth\Services; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\Exception; +use PHPUnit\Framework\MockObject\MockObject; +use RuntimeException; +use SP\Core\Context\ContextException; +use SP\Domain\Auth\Services\AuthException; +use SP\Domain\Auth\Services\LoginBase; +use SP\Domain\Core\Exceptions\InvalidArgumentException; +use SP\Domain\Http\RequestInterface; +use SP\Domain\Security\Dtos\TrackRequest; +use SP\Domain\Security\Ports\TrackService; +use SPT\UnitaryTestCase; + +/** + * Class LoginBaseTest + */ +#[Group('unitary')] +class LoginBaseTest extends UnitaryTestCase +{ + private LoginBase $loginBase; + private RequestInterface|MockObject $request; + private TrackService|MockObject $trackService; + + public function testCheckTracking() + { + $this->trackService + ->expects($this->once()) + ->method('checkTracking') + ->willReturn(true); + + $this->trackService + ->expects($this->once()) + ->method('add'); + + $this->expectException(AuthException::class); + $this->expectExceptionMessage('Attempts exceeded'); + + $this->loginBase->check(); + } + + public function testCheckTrackingWithNoTracking() + { + $this->trackService + ->expects($this->once()) + ->method('checkTracking') + ->willReturn(false); + + $this->loginBase->check(); + } + + public function testAddTracking() + { + $this->trackService + ->expects($this->once()) + ->method('add'); + + $this->loginBase->add(); + } + + public function testAddTrackingWithException() + { + $this->trackService + ->expects($this->once()) + ->method('add') + ->willThrowException(new RuntimeException('test')); + + $this->expectException(AuthException::class); + $this->expectExceptionMessage('Internal error'); + + $this->loginBase->add(); + } + + /** + * @throws Exception + * @throws ContextException + * @throws InvalidArgumentException + */ + protected function setUp(): void + { + parent::setUp(); + + $this->trackService = $this->createMock(TrackService::class); + $this->trackService + ->expects($this->atLeast(1)) + ->method('buildTrackRequest') + ->willReturn( + new TrackRequest( + self::$faker->unixTime(), + self::$faker->colorName(), + self::$faker->ipv4(), + self::$faker->randomNumber(2) + ) + ); + + $this->request = $this->createMock(RequestInterface::class); + + $this->loginBase = new class($this->application, $this->trackService, $this->request) extends LoginBase { + public function check(): void + { + $this->checkTracking(); + } + + public function add(): void + { + $this->addTracking(); + } + }; + } +}