Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
45 / 45
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10pest()->group('integration');
11
12use Phexium\Plugin\PasswordHasher\Adapter\BcryptPasswordHasher;
13
14beforeEach(function (): void {
15    $this->hasher = new BcryptPasswordHasher(4);
16});
17
18describe('Hashing', function (): void {
19    it('produces a hash that differs from the plain password', function (): void {
20        $plainPassword = 'SecurePassword123';
21
22        $hash = $this->hasher->hash($plainPassword);
23
24        expect($hash)->toBeString();
25        expect($hash)->not->toBe($plainPassword);
26        expect($hash)->toStartWith('$2y$04$');
27    });
28
29    it('produces different hashes for the same password', function (): void {
30        $plainPassword = 'SecurePassword123';
31
32        $hash1 = $this->hasher->hash($plainPassword);
33        $hash2 = $this->hasher->hash($plainPassword);
34
35        expect($hash1)->not->toBe($hash2);
36        expect($this->hasher->verify($plainPassword, $hash1))->toBeTrue();
37        expect($this->hasher->verify($plainPassword, $hash2))->toBeTrue();
38    });
39
40    it('uses the default cost when none is specified', function (): void {
41        $hasher = new BcryptPasswordHasher();
42        $hash = $hasher->hash('test');
43
44        expect($hash)->toStartWith('$2y$12$');
45    });
46});
47
48describe('Verification', function (): void {
49    it('returns true for the correct password', function (): void {
50        $plainPassword = 'SecurePassword123';
51        $hash = $this->hasher->hash($plainPassword);
52
53        expect($this->hasher->verify($plainPassword, $hash))->toBeTrue();
54    });
55
56    it('returns false for an incorrect password', function (): void {
57        $wrongPassword = 'WrongPassword456';
58        $plainPassword = 'SecurePassword123';
59        $hash = $this->hasher->hash($plainPassword);
60
61        expect($this->hasher->verify($wrongPassword, $hash))->toBeFalse();
62    });
63
64    it('treats passwords as case sensitive', function (): void {
65        $wrongPassword = 'securepassword123';
66        $plainPassword = 'SecurePassword123';
67        $hash = $this->hasher->hash($plainPassword);
68
69        expect($this->hasher->verify($wrongPassword, $hash))->toBeFalse();
70    });
71});