File tree Expand file tree Collapse file tree 8 files changed +23
-11
lines changed
ArticlePublishing/Tests/Unit/Domain/Models
Domain/UserIdentity/Models
Tests/Unit/Domain/UserIdentity/Models
tests/Unit/Shared/ValueObjects Expand file tree Collapse file tree 8 files changed +23
-11
lines changed Original file line number Diff line number Diff line change 11<?php
22
33declare (strict_types=1 );
4+ use App \Exceptions \BizException ;
45use Contexts \ArticlePublishing \Domain \Models \ArticleId ;
56
67it ('can be created ' , function (int $ validId ) {
1011})->with ([1 , 100 ]);
1112
1213it ('throws an exception when the ID is invalid ' , function (int $ invalidId ) {
13- $ this ->expectException (\InvalidArgumentException ::class);
14+ $ this ->expectException (BizException ::class);
1415
1516 ArticleId::fromInt ($ invalidId );
1617})->with ([-1 , -100 ]);
Original file line number Diff line number Diff line change 44
55namespace Contexts \Authorization \Domain \UserIdentity \Models ;
66
7+ use App \Exceptions \BizException ;
8+
79class Email
810{
911 public function __construct (private string $ value )
1012 {
1113 if (! filter_var ($ value , FILTER_VALIDATE_EMAIL )) {
12- throw new \InvalidArgumentException ('Invalid email address ' );
14+ throw BizException::make ('Invalid email address: :email ' )
15+ ->with ('email ' , $ value );
1316 }
1417 }
1518
Original file line number Diff line number Diff line change 44
55namespace Contexts \Authorization \Domain \UserIdentity \Models ;
66
7+ use App \Exceptions \BizException ;
8+
79class Password
810{
911 private function __construct (private string $ hashedValue ) {}
1012
1113 public static function createFromPlainText (string $ plainText ): self
1214 {
1315 if (strlen ($ plainText ) < 8 ) {
14- throw new \ InvalidArgumentException ('Password must be at least 8 characters long ' );
16+ throw BizException:: make ('Password must be at least 8 characters long ' );
1517 }
1618
1719 return new self (password_hash ($ plainText , PASSWORD_ARGON2ID ));
Original file line number Diff line number Diff line change 22
33declare (strict_types=1 );
44
5+ use App \Exceptions \BizException ;
56use Contexts \Authorization \Domain \UserIdentity \Models \Email ;
67
78it ('can be created with valid email address ' , function (string $ validEmail ) {
1920it ('throws an exception when email address is invalid ' , function (string $ invalidEmail ) {
2021 expect (function () use ($ invalidEmail ) {
2122 new Email ($ invalidEmail );
22- })->toThrow (InvalidArgumentException ::class, 'Invalid email address ' );
23+ })->toThrow (BizException ::class, 'Invalid email address ' );
2324})->with ([
2425 'not-an-email ' ,
2526 'missing@domain ' ,
Original file line number Diff line number Diff line change 22
33declare (strict_types=1 );
44
5+ use App \Exceptions \BizException ;
56use Contexts \Authorization \Domain \UserIdentity \Models \Password ;
67
78it ('can create a password from plain text ' , function () {
2122it ('throws an exception when password is too short ' , function (string $ shortPassword ) {
2223 expect (function () use ($ shortPassword ) {
2324 Password::createFromPlainText ($ shortPassword );
24- })->toThrow (InvalidArgumentException ::class, 'Password must be at least 8 characters long ' );
25+ })->toThrow (BizException ::class, 'Password must be at least 8 characters long ' );
2526})->with (['abc ' , 'short ' , '1234567 ' ]);
2627
2728it ('verifies a correct password ' , function () {
Original file line number Diff line number Diff line change 352352});
353353
354354it ('validates email format ' , function () {
355- expect (fn () => new Email ('invalid-email ' ))->toThrow (\InvalidArgumentException ::class);
356- expect (
fn () =>
new Email (
'[email protected] ' ))->
not ->
toThrow (
\InvalidArgumentException ::class);
355+ expect (fn () => new Email ('invalid-email ' ))->toThrow (BizException ::class);
356+ expect (
fn () =>
new Email (
'[email protected] ' ))->
not ->
toThrow (
BizException ::class);
357357});
358358
359359it ('validates password minimum length ' , function () {
360- expect (fn () => Password::createFromPlainText ('short ' ))->toThrow (\InvalidArgumentException ::class);
361- expect (fn () => Password::createFromPlainText ('password12345 ' ))->not ->toThrow (\InvalidArgumentException ::class);
360+ expect (fn () => Password::createFromPlainText ('short ' ))->toThrow (BizException ::class);
361+ expect (fn () => Password::createFromPlainText ('password12345 ' ))->not ->toThrow (BizException ::class);
362362});
363363
364364it ('can get user summary for logging ' , function () {
Original file line number Diff line number Diff line change 44
55namespace Contexts \Shared \ValueObjects ;
66
7+ use App \Exceptions \BizException ;
8+
79abstract class IntId
810{
911 final private function __construct (private readonly int $ value )
1012 {
1113 if ($ value < 0 ) {
12- throw new \InvalidArgumentException ('Invalid ID value ' );
14+ throw BizException::make ('Invalid ID value :value ' )
15+ ->with ('value ' , $ value );
1316 }
1417 }
1518
Original file line number Diff line number Diff line change 22
33declare (strict_types=1 );
44
5+ use App \Exceptions \BizException ;
56use Contexts \Shared \ValueObjects \IntId ;
67
78// Create a concrete implementation of the abstract IntId class for testing
@@ -15,7 +16,7 @@ class ConcreteIntId extends IntId {}
1516
1617it ('throws exception when created with negative value ' , function (int $ invalidId ) {
1718 expect (fn () => ConcreteIntId::fromInt ($ invalidId ))
18- ->toThrow (\InvalidArgumentException ::class, 'Invalid ID value ' );
19+ ->toThrow (BizException ::class, 'Invalid ID value ' );
1920})->with ([-1 , -100 , PHP_INT_MIN ]);
2021
2122it ('can create a null ID ' , function () {
You can’t perform that action at this time.
0 commit comments