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