From 7baac927a3c324650ec561b62a76ab7c35e98f9a Mon Sep 17 00:00:00 2001 From: Arman <407448+armanist@users.noreply.github.com> Date: Mon, 23 Mar 2026 14:40:18 +0400 Subject: [PATCH 1/2] Reduce bcrypt cost in Auth and Hasher tests for faster test execution --- tests/Unit/Auth/Adapters/JwtAuthAdapterTest.php | 2 +- tests/Unit/Auth/Adapters/SessionAuthAdapterTest.php | 2 +- tests/Unit/Auth/AuthTest.php | 8 ++++---- tests/Unit/Hasher/HasherTest.php | 10 +++++++--- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/tests/Unit/Auth/Adapters/JwtAuthAdapterTest.php b/tests/Unit/Auth/Adapters/JwtAuthAdapterTest.php index 16d7fd70f..f75cd6619 100644 --- a/tests/Unit/Auth/Adapters/JwtAuthAdapterTest.php +++ b/tests/Unit/Auth/Adapters/JwtAuthAdapterTest.php @@ -30,7 +30,7 @@ public function setUp(): void 'exp' => time() + 60, ]); - $this->jwtAuth = new JwtAuthAdapter($this->authService, $this->mailer, new Hasher(), $jwt); + $this->jwtAuth = new JwtAuthAdapter($this->authService, $this->mailer, (new Hasher())->setCost(4), $jwt); $admin = $this->jwtAuth->signup($this->adminUser); diff --git a/tests/Unit/Auth/Adapters/SessionAuthAdapterTest.php b/tests/Unit/Auth/Adapters/SessionAuthAdapterTest.php index 4eb866667..c9257c053 100644 --- a/tests/Unit/Auth/Adapters/SessionAuthAdapterTest.php +++ b/tests/Unit/Auth/Adapters/SessionAuthAdapterTest.php @@ -19,7 +19,7 @@ public function setUp(): void config()->set('auth.two_fa', false); - $this->sessionAuth = new SessionAuthAdapter($this->authService, $this->mailer, new Hasher()); + $this->sessionAuth = new SessionAuthAdapter($this->authService, $this->mailer, (new Hasher())->setCost(4)); $admin = $this->sessionAuth->signup($this->adminUser); diff --git a/tests/Unit/Auth/AuthTest.php b/tests/Unit/Auth/AuthTest.php index b8e62ee0d..4c3a9a656 100644 --- a/tests/Unit/Auth/AuthTest.php +++ b/tests/Unit/Auth/AuthTest.php @@ -32,18 +32,18 @@ public function setUp(): void public function testAuthGetAdapter(): void { - $auth = new Auth(new SessionAuthAdapter($this->authService, $this->mailer, new Hasher())); + $auth = new Auth(new SessionAuthAdapter($this->authService, $this->mailer, (new Hasher())->setCost(4))); $this->assertInstanceOf(AuthenticatableInterface::class, $auth->getAdapter()); - $auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, new Hasher(), $this->jwt)); + $auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, (new Hasher())->setCost(4), $this->jwt)); $this->assertInstanceOf(AuthenticatableInterface::class, $auth->getAdapter()); } public function testAuthCallingValidMethod(): void { - $auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, new Hasher(), $this->jwt)); + $auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, (new Hasher())->setCost(4), $this->jwt)); $user = $auth->getAdapter()->signup($this->adminUser); @@ -58,7 +58,7 @@ public function testAuthCallingValidMethod(): void public function testAuthCallingInvalidMethod(): void { - $auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, new Hasher(), $this->jwt)); + $auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, (new Hasher())->setCost(4), $this->jwt)); $this->expectException(AuthException::class); diff --git a/tests/Unit/Hasher/HasherTest.php b/tests/Unit/Hasher/HasherTest.php index b2e149bde..518f63ceb 100644 --- a/tests/Unit/Hasher/HasherTest.php +++ b/tests/Unit/Hasher/HasherTest.php @@ -38,6 +38,8 @@ public function testSetAndGetCost(): void public function testHashAndCheck(): void { + $this->hasher->setCost(4); + $hashed = $this->hasher->hash($this->password); $this->assertTrue($this->hasher->check($this->password, $hashed)); @@ -47,23 +49,25 @@ public function testHashAndCheck(): void public function testNeedsRehash(): void { - $this->hasher->setCost(10); + $this->hasher->setCost(4); $hashed = $this->hasher->hash($this->password); $this->assertFalse($this->hasher->needsRehash($hashed)); - $this->hasher->setCost(12); + $this->hasher->setCost(5); $this->assertTrue($this->hasher->needsRehash($hashed)); - $this->hasher->setAlgorithm(PASSWORD_DEFAULT)->setCost(11); + $this->hasher->setAlgorithm(PASSWORD_DEFAULT)->setCost(6); $this->assertTrue($this->hasher->needsRehash($hashed)); } public function testInfo(): void { + $this->hasher->setCost(4); + $hashed = $this->hasher->hash($this->password); $this->assertIsArray($this->hasher->info($hashed)); From 24dc0653e9421433dcb87c8bc205aa774e2d1497 Mon Sep 17 00:00:00 2001 From: Arman <407448+armanist@users.noreply.github.com> Date: Mon, 23 Mar 2026 14:56:20 +0400 Subject: [PATCH 2/2] Replace repeated inline Hasher creation in tests with a centralized helper to produce a low-cost test hasher --- tests/Unit/Auth/AuthTest.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/Unit/Auth/AuthTest.php b/tests/Unit/Auth/AuthTest.php index 4c3a9a656..6bfa7a886 100644 --- a/tests/Unit/Auth/AuthTest.php +++ b/tests/Unit/Auth/AuthTest.php @@ -13,11 +13,14 @@ class AuthTest extends AuthTestCase { private JwtToken $jwt; + private Hasher $hasher; public function setUp(): void { parent::setUp(); + $this->hasher = (new Hasher())->setCost(4); + $this->jwt = (new JwtToken()) ->setLeeway(1) ->setClaims([ @@ -32,18 +35,18 @@ public function setUp(): void public function testAuthGetAdapter(): void { - $auth = new Auth(new SessionAuthAdapter($this->authService, $this->mailer, (new Hasher())->setCost(4))); + $auth = new Auth(new SessionAuthAdapter($this->authService, $this->mailer, $this->hasher)); $this->assertInstanceOf(AuthenticatableInterface::class, $auth->getAdapter()); - $auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, (new Hasher())->setCost(4), $this->jwt)); + $auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, $this->hasher, $this->jwt)); $this->assertInstanceOf(AuthenticatableInterface::class, $auth->getAdapter()); } public function testAuthCallingValidMethod(): void { - $auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, (new Hasher())->setCost(4), $this->jwt)); + $auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, $this->hasher, $this->jwt)); $user = $auth->getAdapter()->signup($this->adminUser); @@ -58,7 +61,7 @@ public function testAuthCallingValidMethod(): void public function testAuthCallingInvalidMethod(): void { - $auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, (new Hasher())->setCost(4), $this->jwt)); + $auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, $this->hasher, $this->jwt)); $this->expectException(AuthException::class);