Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
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 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | use AppDemo\User\Domain\Exception\InvalidPasswordException; |
| 11 | use AppDemo\User\Domain\Password; |
| 12 | use Tests\Phexium\Fake\Domain\StringValueObject as FakeValueObject; |
| 13 | |
| 14 | test('Can create password using static factory method', function (): void { |
| 15 | $password = Password::fromString('securePassword123'); |
| 16 | |
| 17 | expect($password->getValue())->toBe('securePassword123') |
| 18 | ->and($password->jsonSerialize())->toBe('securePassword123') |
| 19 | ->and((string) $password)->toBe('securePassword123') |
| 20 | ; |
| 21 | }); |
| 22 | |
| 23 | test('Normalize (identity) a password', function (): void { |
| 24 | $password = Password::fromString(' securePassword123 '); |
| 25 | |
| 26 | expect($password->getValue())->toBe(' securePassword123 '); |
| 27 | }); |
| 28 | |
| 29 | test('Two passwords with same value are equal', function (): void { |
| 30 | $password1 = Password::fromString('securePassword123'); |
| 31 | $password2 = Password::fromString('securePassword123'); |
| 32 | |
| 33 | expect($password1->equals($password2))->toBeTrue(); |
| 34 | }); |
| 35 | |
| 36 | test('Two passwords with different values are not equal', function (): void { |
| 37 | $password1 = Password::fromString('securePassword123'); |
| 38 | $password2 = Password::fromString('differentPassword456'); |
| 39 | |
| 40 | expect($password1->equals($password2))->toBeFalse(); |
| 41 | }); |
| 42 | |
| 43 | test('A password and another value object are not equal', function (): void { |
| 44 | $password = Password::fromString('securePassword123'); |
| 45 | $object = FakeValueObject::fromString('securePassword123'); |
| 46 | |
| 47 | expect($password->equals($object))->toBeFalse(); |
| 48 | }); |
| 49 | |
| 50 | it('throws exception for empty password', function (): void { |
| 51 | expect(fn (): Password => Password::fromString('')) |
| 52 | ->toThrow(InvalidPasswordException::class, 'Password cannot be empty') |
| 53 | ; |
| 54 | }); |
| 55 | |
| 56 | it('throws exception for password shorter than 8 characters', function (): void { |
| 57 | $min = Password::fromString(str_repeat('a', 8)); |
| 58 | expect($min)->toBeInstanceOf(Password::class); |
| 59 | |
| 60 | expect(fn (): Password => Password::fromString(str_repeat('a', 7))) |
| 61 | ->toThrow(InvalidPasswordException::class, 'Password must be at least 8 characters long') |
| 62 | ; |
| 63 | }); |
| 64 | |
| 65 | it('throws exception for password exceeding 255 characters', function (): void { |
| 66 | $max = Password::fromString(str_repeat('a', 255)); |
| 67 | expect($max)->toBeInstanceOf(Password::class); |
| 68 | |
| 69 | expect(fn (): Password => Password::fromString(str_repeat('a', 256))) |
| 70 | ->toThrow(InvalidPasswordException::class, 'Password cannot exceed 255 characters') |
| 71 | ; |
| 72 | }); |