Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
30 / 30 |
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 AppDemo\User\Domain\HashedPassword; |
| 13 | use Tests\Phexium\Fake\Domain\StringValueObject as FakeValueObject; |
| 14 | |
| 15 | describe('Creation', function (): void { |
| 16 | it('creates a hashed password from a hash string', function (): void { |
| 17 | $hash = '$plain$abcdefghijklmnopqrstuuAbCdEfGhIjKlMnOpQrStUvWxYz01234'; |
| 18 | $hashedPassword = HashedPassword::fromHash($hash); |
| 19 | |
| 20 | expect($hashedPassword->getValue())->toBe($hash) |
| 21 | ->and($hashedPassword->jsonSerialize())->toBe($hash) |
| 22 | ->and((string) $hashedPassword)->toBe($hash) |
| 23 | ; |
| 24 | }); |
| 25 | }); |
| 26 | |
| 27 | describe('Equality', function (): void { |
| 28 | it('considers two hashed passwords with same value as equal', function (): void { |
| 29 | $hash = '$plain$abcdefghijklmnopqrstuuAbCdEfGhIjKlMnOpQrStUvWxYz01234'; |
| 30 | $hashedPassword1 = HashedPassword::fromHash($hash); |
| 31 | $hashedPassword2 = HashedPassword::fromHash($hash); |
| 32 | |
| 33 | expect($hashedPassword1->equals($hashedPassword2))->toBeTrue(); |
| 34 | }); |
| 35 | |
| 36 | it('considers two hashed passwords with different values as not equal', function (): void { |
| 37 | $hashedPassword1 = HashedPassword::fromHash('$plain$hash1abcdefghijklmnopqrstuuAbCdEfGhIjKlMnOpQrStUvWx'); |
| 38 | $hashedPassword2 = HashedPassword::fromHash('$plain$hash2abcdefghijklmnopqrstuuAbCdEfGhIjKlMnOpQrStUvWx'); |
| 39 | |
| 40 | expect($hashedPassword1->equals($hashedPassword2))->toBeFalse(); |
| 41 | }); |
| 42 | |
| 43 | it('considers a hashed password and another value object as not equal', function (): void { |
| 44 | $hash = '$plain$abcdefghijklmnopqrstuuAbCdEfGhIjKlMnOpQrStUvWxYz01234'; |
| 45 | $hashedPassword = HashedPassword::fromHash($hash); |
| 46 | $object = FakeValueObject::fromString($hash); |
| 47 | |
| 48 | expect($hashedPassword->equals($object))->toBeFalse(); |
| 49 | }); |
| 50 | }); |