Skip to content

Commit 2186fb1

Browse files
committed
Add unit tests for type aliases and improve static return type usage
- Added comprehensive unit tests for new type aliases, including `Str`, `NonEmptyStr`, `StrType`, `Integer`, `PositiveInt`, `IntType`, `NonNegativeInt`, `TinyInt`, `Double`, `FloatType`, `PositiveFloat`, and `NonNegativeFloat`. - Refactored return types from `self` to `static` in `DateTimeW3C`, `TimestampMilliseconds`, and `Json` for improved type consistency and covariant support.
1 parent e3e9b6b commit 2186fb1

File tree

16 files changed

+226
-3
lines changed

16 files changed

+226
-3
lines changed

src/DateTime/DateTimeW3C.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
public static function fromString(string $value): static
2929
{
3030
return new static(
31-
self::createFromFormat(
31+
static::createFromFormat(
3232
$value,
3333
static::FORMAT,
3434
new DateTimeZone(static::ZONE)

src/DateTime/Timestamp/TimestampMilliseconds.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static function fromString(string $value): static
5252
return new static(
5353
static::createFromFormat(
5454
$secondsWithMicro,
55-
self::FORMAT,
55+
static::FORMAT,
5656
new DateTimeZone(static::ZONE)
5757
)
5858
);

src/String/Json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
public function __construct(string $value)
3131
{
32-
self::assertJsonString($value);
32+
static::assertJsonString($value);
3333

3434
$this->value = $value;
3535
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\Float\Alias\Double;
6+
7+
it('Double::fromFloat returns Double instance (late static binding)', function (): void {
8+
$v = Double::fromFloat(3.14);
9+
10+
expect($v)->toBeInstanceOf(Double::class)
11+
->and($v::class)->toBe(Double::class)
12+
->and($v->value())->toBe(3.14);
13+
});
14+
15+
it('Double::fromString returns Double instance (late static binding)', function (): void {
16+
$v = Double::fromString('2.5');
17+
18+
expect($v)->toBeInstanceOf(Double::class)
19+
->and($v::class)->toBe(Double::class)
20+
->and($v->toString())->toBe('2.5');
21+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\Float\Alias\FloatType;
6+
7+
it('FloatType::fromFloat returns FloatType instance', function (): void {
8+
$v = FloatType::fromFloat(1.25);
9+
10+
expect($v)->toBeInstanceOf(FloatType::class)
11+
->and($v::class)->toBe(FloatType::class)
12+
->and($v->value())->toBe(1.25);
13+
});
14+
15+
it('FloatType::fromString returns FloatType instance', function (): void {
16+
$v = FloatType::fromString('2.75');
17+
18+
expect($v)->toBeInstanceOf(FloatType::class)
19+
->and($v::class)->toBe(FloatType::class)
20+
->and($v->toString())->toBe('2.75');
21+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\Float\Alias\NonNegativeFloat;
6+
7+
it('NonNegativeFloat::fromFloat returns NonNegativeFloat instance', function (): void {
8+
$v = NonNegativeFloat::fromFloat(0.0);
9+
10+
expect($v)->toBeInstanceOf(NonNegativeFloat::class)
11+
->and($v::class)->toBe(NonNegativeFloat::class)
12+
->and($v->value())->toBe(0.0);
13+
});
14+
15+
it('NonNegativeFloat::fromString returns NonNegativeFloat instance', function (): void {
16+
$v = NonNegativeFloat::fromString('2.0');
17+
18+
expect($v)->toBeInstanceOf(NonNegativeFloat::class)
19+
->and($v::class)->toBe(NonNegativeFloat::class)
20+
->and($v->toString())->toBe('2');
21+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\Float\Alias\PositiveFloat;
6+
7+
it('PositiveFloat::fromFloat returns PositiveFloat instance', function (): void {
8+
$v = PositiveFloat::fromFloat(0.1);
9+
10+
expect($v)->toBeInstanceOf(PositiveFloat::class)
11+
->and($v::class)->toBe(PositiveFloat::class)
12+
->and($v->value())->toBe(0.1);
13+
});
14+
15+
it('PositiveFloat::fromString returns PositiveFloat instance', function (): void {
16+
$v = PositiveFloat::fromString('1.5');
17+
18+
expect($v)->toBeInstanceOf(PositiveFloat::class)
19+
->and($v::class)->toBe(PositiveFloat::class)
20+
->and($v->toString())->toBe('1.5');
21+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\Integer\Alias\IntType;
6+
7+
it('IntType alias factories return IntType instance', function (): void {
8+
$a = IntType::fromInt(-5);
9+
$b = IntType::fromString('10');
10+
11+
expect($a)->toBeInstanceOf(IntType::class)
12+
->and($a::class)->toBe(IntType::class)
13+
->and($a->value())->toBe(-5)
14+
->and($b)->toBeInstanceOf(IntType::class)
15+
->and($b->toString())->toBe('10');
16+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\Integer\Alias\Integer;
6+
7+
it('Integer alias fromInt returns Integer alias instance', function (): void {
8+
$v = Integer::fromInt(42);
9+
10+
expect($v)->toBeInstanceOf(Integer::class)
11+
->and($v::class)->toBe(Integer::class)
12+
->and($v->value())->toBe(42);
13+
});
14+
15+
it('Integer alias fromString returns Integer alias instance', function (): void {
16+
$v = Integer::fromString('7');
17+
18+
expect($v)->toBeInstanceOf(Integer::class)
19+
->and($v::class)->toBe(Integer::class)
20+
->and($v->toString())->toBe('7');
21+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpTypedValues\Integer\Alias\NonNegativeInt;
6+
7+
it('NonNegativeInt alias factories return NonNegativeInt instance', function (): void {
8+
$a = NonNegativeInt::fromInt(0);
9+
$b = NonNegativeInt::fromString('12');
10+
11+
expect($a)->toBeInstanceOf(NonNegativeInt::class)
12+
->and($a::class)->toBe(NonNegativeInt::class)
13+
->and($a->value())->toBe(0)
14+
->and($b)->toBeInstanceOf(NonNegativeInt::class)
15+
->and($b->value())->toBe(12);
16+
});

0 commit comments

Comments
 (0)