Skip to content

Commit ef41357

Browse files
committed
refactor: replace InvalidArgumentException with BizException for validation errors
1 parent bf17b47 commit ef41357

File tree

8 files changed

+23
-11
lines changed

8 files changed

+23
-11
lines changed

contexts/ArticlePublishing/Tests/Unit/Domain/Models/ArticleIdTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
use App\Exceptions\BizException;
45
use Contexts\ArticlePublishing\Domain\Models\ArticleId;
56

67
it('can be created', function (int $validId) {
@@ -10,7 +11,7 @@
1011
})->with([1, 100]);
1112

1213
it('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]);

contexts/Authorization/Domain/UserIdentity/Models/Email.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
namespace Contexts\Authorization\Domain\UserIdentity\Models;
66

7+
use App\Exceptions\BizException;
8+
79
class 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

contexts/Authorization/Domain/UserIdentity/Models/Password.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
namespace Contexts\Authorization\Domain\UserIdentity\Models;
66

7+
use App\Exceptions\BizException;
8+
79
class 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));

contexts/Authorization/Tests/Unit/Domain/UserIdentity/Models/EmailTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use App\Exceptions\BizException;
56
use Contexts\Authorization\Domain\UserIdentity\Models\Email;
67

78
it('can be created with valid email address', function (string $validEmail) {
@@ -19,7 +20,7 @@
1920
it('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',

contexts/Authorization/Tests/Unit/Domain/UserIdentity/Models/PasswordTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use App\Exceptions\BizException;
56
use Contexts\Authorization\Domain\UserIdentity\Models\Password;
67

78
it('can create a password from plain text', function () {
@@ -21,7 +22,7 @@
2122
it('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

2728
it('verifies a correct password', function () {

contexts/Authorization/Tests/Unit/Domain/UserIdentity/Models/UserIdentityTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,13 @@
352352
});
353353

354354
it('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

359359
it('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

364364
it('can get user summary for logging', function () {

contexts/Shared/ValueObjects/IntId.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
namespace Contexts\Shared\ValueObjects;
66

7+
use App\Exceptions\BizException;
8+
79
abstract 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

tests/Unit/Shared/ValueObjects/IntIdTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use App\Exceptions\BizException;
56
use 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

1617
it('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

2122
it('can create a null ID', function () {

0 commit comments

Comments
 (0)