Skip to content

Commit cec2197

Browse files
committed
Add unit tests for IntegerWeekDay, IntegerStandard, IntegerPositive, and IntegerNonNegative
- Introduced comprehensive tests for `tryFromString`, `tryFromInt`, `fromString`, and `fromInt` methods. - Validated strict integer parsing, range, and type-specific constraints. - Ensured exception handling and `Undefined` behavior for invalid inputs.
1 parent 5ac7230 commit cec2197

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\Exception\IntegerTypeException;
6+
use PhpTypedValues\Integer\IntegerNonNegative;
7+
use PhpTypedValues\Undefined\Alias\Undefined;
8+
9+
it('IntegerNonNegative::tryFromString returns value for >= 0', function (): void {
10+
$v0 = IntegerNonNegative::tryFromString('0');
11+
$v5 = IntegerNonNegative::tryFromString('5');
12+
13+
expect($v0)
14+
->toBeInstanceOf(IntegerNonNegative::class)
15+
->and($v0->value())
16+
->toBe(0)
17+
->and($v5)
18+
->toBeInstanceOf(IntegerNonNegative::class)
19+
->and($v5->value())
20+
->toBe(5);
21+
});
22+
23+
it('IntegerNonNegative::tryFromString returns Undefined for negatives and non-integer strings', function (): void {
24+
expect(IntegerNonNegative::tryFromString('-1'))
25+
->toBeInstanceOf(Undefined::class)
26+
->and(IntegerNonNegative::tryFromString('5.0'))
27+
->toBeInstanceOf(Undefined::class);
28+
});
29+
30+
it('IntegerNonNegative::tryFromInt returns value for >= 0 and Undefined for negatives', function (): void {
31+
$ok = IntegerNonNegative::tryFromInt(0);
32+
$bad = IntegerNonNegative::tryFromInt(-10);
33+
34+
expect($ok)
35+
->toBeInstanceOf(IntegerNonNegative::class)
36+
->and($ok->value())
37+
->toBe(0)
38+
->and($bad)
39+
->toBeInstanceOf(Undefined::class);
40+
});
41+
42+
it('IntegerNonNegative throws on negative values in ctor and fromInt', function (): void {
43+
expect(fn() => new IntegerNonNegative(-1))
44+
->toThrow(IntegerTypeException::class, 'Expected non-negative integer, got "-1"')
45+
->and(fn() => IntegerNonNegative::fromInt(-10))
46+
->toThrow(IntegerTypeException::class, 'Expected non-negative integer, got "-10"');
47+
});
48+
49+
it('IntegerNonNegative::fromString enforces strict integer and non-negativity', function (): void {
50+
// Strict integer check
51+
expect(fn() => IntegerNonNegative::fromString('5.0'))
52+
->toThrow(IntegerTypeException::class, 'String "5.0" has no valid strict integer value');
53+
54+
// Non-negativity check after casting
55+
expect(fn() => IntegerNonNegative::fromString('-1'))
56+
->toThrow(IntegerTypeException::class, 'Expected non-negative integer, got "-1"');
57+
58+
// Success path
59+
$v = IntegerNonNegative::fromString('0');
60+
expect($v->value())->toBe(0);
61+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\Exception\IntegerTypeException;
6+
use PhpTypedValues\Integer\IntegerPositive;
7+
use PhpTypedValues\Undefined\Alias\Undefined;
8+
9+
it('IntegerPositive::tryFromString returns value for > 0', function (): void {
10+
$v = IntegerPositive::tryFromString('5');
11+
12+
expect($v)
13+
->toBeInstanceOf(IntegerPositive::class)
14+
->and($v->value())
15+
->toBe(5);
16+
});
17+
18+
it('IntegerPositive::tryFromString returns Undefined for 0 and non-integer strings', function (): void {
19+
expect(IntegerPositive::tryFromString('0'))
20+
->toBeInstanceOf(Undefined::class)
21+
->and(IntegerPositive::tryFromString('5.0'))
22+
->toBeInstanceOf(Undefined::class)
23+
->and(IntegerPositive::tryFromString('-3'))
24+
->toBeInstanceOf(Undefined::class);
25+
});
26+
27+
it('IntegerPositive::tryFromInt returns value for positive int and Undefined otherwise', function (): void {
28+
$ok = IntegerPositive::tryFromInt(10);
29+
$bad = IntegerPositive::tryFromInt(-1);
30+
31+
expect($ok)
32+
->toBeInstanceOf(IntegerPositive::class)
33+
->and($ok->value())
34+
->toBe(10)
35+
->and($bad)
36+
->toBeInstanceOf(Undefined::class);
37+
});
38+
39+
it('IntegerPositive throws on non-positive values in ctor and fromInt', function (): void {
40+
expect(fn() => new IntegerPositive(0))
41+
->toThrow(IntegerTypeException::class, 'Expected positive integer, got "0"')
42+
->and(fn() => IntegerPositive::fromInt(-1))
43+
->toThrow(IntegerTypeException::class, 'Expected positive integer, got "-1"');
44+
});
45+
46+
it('IntegerPositive::fromString enforces strict integer and positivity', function (): void {
47+
// Strict integer check
48+
expect(fn() => IntegerPositive::fromString('5.0'))
49+
->toThrow(IntegerTypeException::class, 'String "5.0" has no valid strict integer value');
50+
51+
// Positivity check after casting
52+
expect(fn() => IntegerPositive::fromString('0'))
53+
->toThrow(IntegerTypeException::class, 'Expected positive integer, got "0"');
54+
55+
// Success path
56+
$v = IntegerPositive::fromString('7');
57+
expect($v->value())->toBe(7);
58+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\Exception\IntegerTypeException;
6+
use PhpTypedValues\Integer\IntegerStandard;
7+
use PhpTypedValues\Undefined\Alias\Undefined;
8+
9+
it('IntegerStandard::tryFromString returns value on valid integer string', function (): void {
10+
$v = IntegerStandard::tryFromString('123');
11+
12+
expect($v)
13+
->toBeInstanceOf(IntegerStandard::class)
14+
->and($v->value())
15+
->toBe(123);
16+
});
17+
18+
it('IntegerStandard::tryFromString returns Undefined on invalid integer string', function (): void {
19+
$v = IntegerStandard::tryFromString('5.0');
20+
21+
expect($v)->toBeInstanceOf(Undefined::class);
22+
});
23+
24+
it('IntegerStandard::tryFromInt always returns value for any int', function (): void {
25+
$v = IntegerStandard::tryFromInt(-999);
26+
27+
expect($v)
28+
->toBeInstanceOf(IntegerStandard::class)
29+
->and($v->value())
30+
->toBe(-999);
31+
});
32+
33+
it('IntegerStandard::fromInt returns instance and preserves value', function (): void {
34+
$v = IntegerStandard::fromInt(42);
35+
36+
expect($v)
37+
->toBeInstanceOf(IntegerStandard::class)
38+
->and($v->value())->toBe(42)
39+
->and($v->toString())->toBe('42');
40+
});
41+
42+
it('IntegerStandard::fromString throws on non-integer strings (strict check)', function (): void {
43+
expect(fn() => IntegerStandard::fromString('12.3'))
44+
->toThrow(IntegerTypeException::class, 'String "12.3" has no valid strict integer value');
45+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\Exception\IntegerTypeException;
6+
use PhpTypedValues\Integer\IntegerWeekDay;
7+
use PhpTypedValues\Undefined\Alias\Undefined;
8+
9+
it('IntegerWeekDay::tryFromString returns value for 1..7', function (): void {
10+
$v1 = IntegerWeekDay::tryFromString('1');
11+
$v7 = IntegerWeekDay::tryFromString('7');
12+
13+
expect($v1)
14+
->toBeInstanceOf(IntegerWeekDay::class)
15+
->and($v1->value())
16+
->toBe(1)
17+
->and($v7)
18+
->toBeInstanceOf(IntegerWeekDay::class)
19+
->and($v7->value())
20+
->toBe(7);
21+
});
22+
23+
it('IntegerWeekDay::tryFromString returns Undefined outside 1..7 and for non-integer strings', function (): void {
24+
expect(IntegerWeekDay::tryFromString('0'))
25+
->toBeInstanceOf(Undefined::class)
26+
->and(IntegerWeekDay::tryFromString('8'))
27+
->toBeInstanceOf(Undefined::class)
28+
->and(IntegerWeekDay::tryFromString('3.0'))
29+
->toBeInstanceOf(Undefined::class);
30+
});
31+
32+
it('IntegerWeekDay::tryFromInt returns value for 1..7 and Undefined otherwise', function (): void {
33+
$ok = IntegerWeekDay::tryFromInt(3);
34+
$bad = IntegerWeekDay::tryFromInt(0);
35+
36+
expect($ok)
37+
->toBeInstanceOf(IntegerWeekDay::class)
38+
->and($ok->value())
39+
->toBe(3)
40+
->and($bad)
41+
->toBeInstanceOf(Undefined::class);
42+
});
43+
44+
it('IntegerWeekDay throws on values outside 1..7 in ctor and fromInt', function (): void {
45+
expect(fn() => new IntegerWeekDay(0))
46+
->toThrow(IntegerTypeException::class, 'Expected value between 1-7, got "0"')
47+
->and(fn() => IntegerWeekDay::fromInt(8))
48+
->toThrow(IntegerTypeException::class, 'Expected value between 1-7, got "8"');
49+
});
50+
51+
it('IntegerWeekDay::fromString enforces strict integer and range 1..7', function (): void {
52+
// Strict integer check
53+
expect(fn() => IntegerWeekDay::fromString('3.0'))
54+
->toThrow(IntegerTypeException::class, 'String "3.0" has no valid strict integer value');
55+
56+
// Range checks after casting
57+
expect(fn() => IntegerWeekDay::fromString('0'))
58+
->toThrow(IntegerTypeException::class, 'Expected value between 1-7, got "0"')
59+
->and(fn() => IntegerWeekDay::fromString('8'))
60+
->toThrow(IntegerTypeException::class, 'Expected value between 1-7, got "8"');
61+
62+
// Success path
63+
$v = IntegerWeekDay::fromString('6');
64+
expect($v->value())->toBe(6);
65+
});

0 commit comments

Comments
 (0)