Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
36 / 36 |
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 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | pest()->group('unit'); |
| 11 | |
| 12 | use Phexium\Plugin\PasswordHasher\Adapter\PlaintextPasswordHasher; |
| 13 | |
| 14 | beforeEach(function (): void { |
| 15 | $this->hasher = new PlaintextPasswordHasher(); |
| 16 | }); |
| 17 | |
| 18 | describe('Hashing', function (): void { |
| 19 | it('produces a hash prefixed with the plain marker', function (): void { |
| 20 | $plainPassword = 'SecurePassword123'; |
| 21 | |
| 22 | $hash = $this->hasher->hash($plainPassword); |
| 23 | |
| 24 | expect($hash)->toBe('$plain$SecurePassword123'); |
| 25 | }); |
| 26 | |
| 27 | it('produces the same hash for the same password', function (): void { |
| 28 | $plainPassword = 'SecurePassword123'; |
| 29 | |
| 30 | $hash1 = $this->hasher->hash($plainPassword); |
| 31 | $hash2 = $this->hasher->hash($plainPassword); |
| 32 | |
| 33 | expect($hash1)->toBe($hash2); |
| 34 | }); |
| 35 | }); |
| 36 | |
| 37 | describe('Verification', function (): void { |
| 38 | it('returns true for the correct password', function (): void { |
| 39 | $plainPassword = 'SecurePassword123'; |
| 40 | $hash = $this->hasher->hash($plainPassword); |
| 41 | |
| 42 | expect($this->hasher->verify($plainPassword, $hash))->toBeTrue(); |
| 43 | }); |
| 44 | |
| 45 | it('returns false for an incorrect password', function (): void { |
| 46 | $wrongPassword = 'WrongPassword456'; |
| 47 | $plainPassword = 'SecurePassword123'; |
| 48 | $hash = $this->hasher->hash($plainPassword); |
| 49 | |
| 50 | expect($this->hasher->verify($wrongPassword, $hash))->toBeFalse(); |
| 51 | }); |
| 52 | |
| 53 | it('treats passwords as case sensitive', function (): void { |
| 54 | $wrongPassword = 'securepassword123'; |
| 55 | $plainPassword = 'SecurePassword123'; |
| 56 | $hash = $this->hasher->hash($plainPassword); |
| 57 | |
| 58 | expect($this->hasher->verify($wrongPassword, $hash))->toBeFalse(); |
| 59 | }); |
| 60 | }); |