1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ use ComplexHeart \Domain \Model \Exceptions \InvariantViolation ;
6+ use ComplexHeart \Domain \Model \ValueObjects \StringValue ;
7+
8+
9+ final class ProductName extends StringValue
10+ {
11+ protected int $ _maxLength = 30 ;
12+ protected int $ _minLength = 3 ;
13+
14+ protected function invariantMustStartWithStringProduct (): bool
15+ {
16+ if (strpos ($ this ->value (), 'PR ' ) !== 0 ) {
17+ throw new InvariantViolation ('Product Name must start with PR chars ' );
18+ }
19+
20+ return true ;
21+ }
22+ }
23+
24+ test ('Should create a valid Value Object. ' , function () {
25+ $ vo = new ProductName ('PR sample ' );
26+ expect ($ vo )->toEqual ('PR sample ' );
27+ expect ((string )$ vo )->toEqual ('PR sample ' );
28+ })->group ('Unit ' );
29+
30+ test ('Should return true on equal ValueObjects. ' , function () {
31+ $ vo = new ProductName ('PR sample ' );
32+ expect ($ vo ->equals (new ProductName ('PR sample ' )))->toBeTrue ();
33+ })->group ('Unit ' );
34+
35+ test ('Should return false on not equal ValueObjects. ' , function () {
36+ $ vo = new ProductName ('PR sample ' );
37+ expect ($ vo ->equals (new ProductName ('PR diff ' )))->toBeFalse ();
38+ })->group ('Unit ' );
39+
40+ test ('Should throw exception on min length invariant violation. ' , function () {
41+ new class ('a ' ) extends StringValue {
42+ protected int $ _minLength = 5 ;
43+ };
44+
45+ })
46+ ->throws (InvariantViolation::class)
47+ ->group ('Unit ' );
48+
49+ test ('Should throw exception on max length invariant violation. ' , function () {
50+ new class ('this is long ' ) extends StringValue {
51+ protected int $ _maxLength = 5 ;
52+ };
53+ })
54+ ->throws (InvariantViolation::class)
55+ ->group ('Unit ' );
56+
57+ test ('Should throw exception on regex invariant violation. ' , function () {
58+ new class ('INVALID ' ) extends StringValue {
59+ protected string $ _pattern = '[a-z] ' ;
60+ };
61+ })
62+ ->throws (InvariantViolation::class)
63+ ->group ('Unit ' );
0 commit comments