Skip to content

Commit 5ebf23c

Browse files
authored
Merge pull request #436 from armanist/435-Reduce-bcrypt-cost-in-Auth-and-Hasher-tests-for-faster-test-execution
Reduce bcrypt cost in Auth and Hasher tests for faster test execution
2 parents efd0249 + 24dc065 commit 5ebf23c

4 files changed

Lines changed: 16 additions & 9 deletions

File tree

‎tests/Unit/Auth/Adapters/JwtAuthAdapterTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function setUp(): void
3030
'exp' => time() + 60,
3131
]);
3232

33-
$this->jwtAuth = new JwtAuthAdapter($this->authService, $this->mailer, new Hasher(), $jwt);
33+
$this->jwtAuth = new JwtAuthAdapter($this->authService, $this->mailer, (new Hasher())->setCost(4), $jwt);
3434

3535
$admin = $this->jwtAuth->signup($this->adminUser);
3636

‎tests/Unit/Auth/Adapters/SessionAuthAdapterTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function setUp(): void
1919

2020
config()->set('auth.two_fa', false);
2121

22-
$this->sessionAuth = new SessionAuthAdapter($this->authService, $this->mailer, new Hasher());
22+
$this->sessionAuth = new SessionAuthAdapter($this->authService, $this->mailer, (new Hasher())->setCost(4));
2323

2424
$admin = $this->sessionAuth->signup($this->adminUser);
2525

‎tests/Unit/Auth/AuthTest.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
class AuthTest extends AuthTestCase
1414
{
1515
private JwtToken $jwt;
16+
private Hasher $hasher;
1617

1718
public function setUp(): void
1819
{
1920
parent::setUp();
2021

22+
$this->hasher = (new Hasher())->setCost(4);
23+
2124
$this->jwt = (new JwtToken())
2225
->setLeeway(1)
2326
->setClaims([
@@ -32,18 +35,18 @@ public function setUp(): void
3235

3336
public function testAuthGetAdapter(): void
3437
{
35-
$auth = new Auth(new SessionAuthAdapter($this->authService, $this->mailer, new Hasher()));
38+
$auth = new Auth(new SessionAuthAdapter($this->authService, $this->mailer, $this->hasher));
3639

3740
$this->assertInstanceOf(AuthenticatableInterface::class, $auth->getAdapter());
3841

39-
$auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, new Hasher(), $this->jwt));
42+
$auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, $this->hasher, $this->jwt));
4043

4144
$this->assertInstanceOf(AuthenticatableInterface::class, $auth->getAdapter());
4245
}
4346

4447
public function testAuthCallingValidMethod(): void
4548
{
46-
$auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, new Hasher(), $this->jwt));
49+
$auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, $this->hasher, $this->jwt));
4750

4851
$user = $auth->getAdapter()->signup($this->adminUser);
4952

@@ -58,7 +61,7 @@ public function testAuthCallingValidMethod(): void
5861

5962
public function testAuthCallingInvalidMethod(): void
6063
{
61-
$auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, new Hasher(), $this->jwt));
64+
$auth = new Auth(new JwtAuthAdapter($this->authService, $this->mailer, $this->hasher, $this->jwt));
6265

6366
$this->expectException(AuthException::class);
6467

‎tests/Unit/Hasher/HasherTest.php‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public function testSetAndGetCost(): void
3838

3939
public function testHashAndCheck(): void
4040
{
41+
$this->hasher->setCost(4);
42+
4143
$hashed = $this->hasher->hash($this->password);
4244

4345
$this->assertTrue($this->hasher->check($this->password, $hashed));
@@ -47,23 +49,25 @@ public function testHashAndCheck(): void
4749

4850
public function testNeedsRehash(): void
4951
{
50-
$this->hasher->setCost(10);
52+
$this->hasher->setCost(4);
5153

5254
$hashed = $this->hasher->hash($this->password);
5355

5456
$this->assertFalse($this->hasher->needsRehash($hashed));
5557

56-
$this->hasher->setCost(12);
58+
$this->hasher->setCost(5);
5759

5860
$this->assertTrue($this->hasher->needsRehash($hashed));
5961

60-
$this->hasher->setAlgorithm(PASSWORD_DEFAULT)->setCost(11);
62+
$this->hasher->setAlgorithm(PASSWORD_DEFAULT)->setCost(6);
6163

6264
$this->assertTrue($this->hasher->needsRehash($hashed));
6365
}
6466

6567
public function testInfo(): void
6668
{
69+
$this->hasher->setCost(4);
70+
6771
$hashed = $this->hasher->hash($this->password);
6872

6973
$this->assertIsArray($this->hasher->info($hashed));

0 commit comments

Comments
 (0)