Skip to content

Commit e3e9b6b

Browse files
committed
Break typeUsageTest.php into modular files by type
- Extracted type-specific examples from `typeUsageTest.php` into their respective files (`Boolean.php`, `Float.php`, `String.php`, `Integer.php`, `DateTime.php`, `Undefined.php`). - Added `AllTypes.php` to act as an aggregator for these modular files. - Updated `StructureTest.php` to include new read-only and abstract validations for various abstract classes (`IntType`, `FloatType`, `StrType`, `UndefinedType`). - Refined PHPUnit configuration to exclude the `src/Usage` directory from coverage.
1 parent 025d5a3 commit e3e9b6b

File tree

10 files changed

+303
-216
lines changed

10 files changed

+303
-216
lines changed

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<directory>src</directory>
2121
</include>
2222
<exclude>
23-
<file>src/typeUsageTest.php</file>
23+
<directory>src/Usage</directory>
2424
</exclude>
2525
</source>
2626
</phpunit>

src/Usage/AllTypes.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/**
4+
* Artificial code usage, test psalm types.
5+
*
6+
* For example, PSALM will fail if the INT parameter is used in the POSITIVE-INT function parameter.
7+
*/
8+
9+
require_once 'vendor/autoload.php';
10+
11+
require_once __DIR__ . '/integer.php';
12+
require_once __DIR__ . '/String.php';
13+
require_once __DIR__ . '/Float.php';
14+
require_once __DIR__ . '/Boolean.php';
15+
require_once __DIR__ . '/DateTime.php';
16+
require_once __DIR__ . '/Undefined.php';

src/Usage/Boolean.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use PhpTypedValues\Abstract\Bool\BoolTypeInterface;
4+
use PhpTypedValues\Bool\Alias\Boolean;
5+
use PhpTypedValues\Bool\BoolStandard;
6+
use PhpTypedValues\Exception\UndefinedTypeException;
7+
8+
/**
9+
* Boolean.
10+
*/
11+
$undefinedType1 = BoolStandard::tryFromInt(2);
12+
$undefinedType2 = BoolStandard::tryFromString('test');
13+
try {
14+
echo $undefinedType1->value();
15+
echo $undefinedType2->value();
16+
} catch (UndefinedTypeException $e) {
17+
// suppress
18+
}
19+
echo BoolStandard::fromString('true')->toString() . \PHP_EOL;
20+
echo BoolStandard::fromInt(1)->toString() . \PHP_EOL;
21+
echo BoolStandard::fromBool(true)->toString() . \PHP_EOL;
22+
echo Boolean::fromBool(Boolean::fromBool(true)->value())->toString() . \PHP_EOL;
23+
// Ensure interface method usage is visible to Psalm
24+
echo (testBool(BoolStandard::fromBool(true)) ? 'true' : 'false') . \PHP_EOL;
25+
26+
/**
27+
* Exercise BoolTypeInterface::value() for Psalm.
28+
*/
29+
function testBool(BoolTypeInterface $b): bool
30+
{
31+
return $b->value();
32+
}

src/Usage/DateTime.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
require_once 'vendor/autoload.php';
4+
5+
use PhpTypedValues\DateTime\DateTimeAtom;
6+
use PhpTypedValues\DateTime\DateTimeRFC3339;
7+
use PhpTypedValues\DateTime\Timestamp\TimestampMilliseconds;
8+
use PhpTypedValues\DateTime\Timestamp\TimestampSeconds;
9+
10+
/**
11+
* DateTime.
12+
*/
13+
echo DateTimeAtom::getFormat() . \PHP_EOL;
14+
15+
$dt = DateTimeAtom::fromString('2025-01-02T03:04:05+00:00')->value();
16+
echo DateTimeAtom::fromDateTime($dt)->toString() . \PHP_EOL;
17+
18+
$dt = DateTimeRFC3339::fromString('2025-01-02T03:04:05+00:00')->value();
19+
echo DateTimeRFC3339::fromDateTime($dt)->toString() . \PHP_EOL;
20+
21+
// Timestamp
22+
$tsVo = TimestampSeconds::fromString('1735787045');
23+
echo TimestampSeconds::fromDateTime($tsVo->value())->toString() . \PHP_EOL;
24+
25+
$tsVo = TimestampMilliseconds::fromString('1735787045123');
26+
echo TimestampMilliseconds::fromDateTime($tsVo->value())->toString() . \PHP_EOL;

src/Usage/Float.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
require_once 'vendor/autoload.php';
4+
5+
use PhpTypedValues\Float\Alias\Double;
6+
use PhpTypedValues\Float\Alias\FloatType;
7+
use PhpTypedValues\Float\Alias\NonNegativeFloat;
8+
use PhpTypedValues\Float\Alias\PositiveFloat;
9+
use PhpTypedValues\Float\FloatNonNegative;
10+
use PhpTypedValues\Float\FloatStandard;
11+
12+
/**
13+
* Float.
14+
*/
15+
testFloat(FloatStandard::fromFloat(3.14)->value());
16+
17+
echo FloatStandard::fromString('2.71828')->toString() . \PHP_EOL;
18+
echo NonNegativeFloat::fromString('2.71828')->toString() . \PHP_EOL;
19+
echo FloatType::fromString('2.71828')->toString() . \PHP_EOL;
20+
echo Double::fromString('2.71828')->toString() . \PHP_EOL;
21+
echo PositiveFloat::fromString('2.8')->toString() . \PHP_EOL;
22+
23+
// PositiveFloat usage
24+
testPositiveFloat(FloatNonNegative::fromFloat(0.5)->value());
25+
echo FloatNonNegative::fromString('3.14159')->toString() . \PHP_EOL;
26+
27+
/**
28+
* Artificial functions.
29+
*/
30+
function testFloat(float $f): float
31+
{
32+
return $f;
33+
}
34+
35+
function testPositiveFloat(float $f): float
36+
{
37+
return $f;
38+
}

src/Usage/String.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use PhpTypedValues\String\Alias\JsonStr;
4+
use PhpTypedValues\String\Alias\NonEmptyStr;
5+
use PhpTypedValues\String\Alias\Str;
6+
use PhpTypedValues\String\Alias\StrType;
7+
use PhpTypedValues\String\Json;
8+
use PhpTypedValues\String\StringNonEmpty;
9+
use PhpTypedValues\String\StringStandard;
10+
11+
/**
12+
* String.
13+
*/
14+
testString(StringStandard::fromString('hi')->value());
15+
testNonEmptyString(StringNonEmpty::fromString('hi')->value());
16+
17+
echo StringStandard::fromString('hi')->toString() . \PHP_EOL;
18+
echo NonEmptyStr::fromString('hi')->toString() . \PHP_EOL;
19+
echo StrType::fromString('hi')->toString() . \PHP_EOL;
20+
echo Str::fromString('hi')->toString() . \PHP_EOL;
21+
22+
// JSON
23+
echo json_encode(JsonStr::fromString('{"a": 1, "b": "hi"}')->toArray(), \JSON_THROW_ON_ERROR);
24+
echo json_encode(Json::fromString('{"a": 1, "b": "hi"}')->toObject(), \JSON_THROW_ON_ERROR);
25+
26+
/**
27+
* Artificial functions.
28+
*/
29+
function testString(string $i): string
30+
{
31+
return $i;
32+
}
33+
34+
/**
35+
* @param non-empty-string $i
36+
*/
37+
function testNonEmptyString(string $i): string
38+
{
39+
return $i;
40+
}

src/Usage/Undefined.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
require_once 'vendor/autoload.php';
4+
5+
use PhpTypedValues\Exception\UndefinedTypeException;
6+
use PhpTypedValues\Undefined\Alias\NotExist;
7+
use PhpTypedValues\Undefined\Alias\NotFound;
8+
use PhpTypedValues\Undefined\Alias\NotSet;
9+
use PhpTypedValues\Undefined\Alias\Undefined;
10+
use PhpTypedValues\Undefined\Alias\Unknown;
11+
use PhpTypedValues\Undefined\UndefinedStandard;
12+
13+
// Undefined
14+
try {
15+
UndefinedStandard::create()->toString();
16+
} catch (UndefinedTypeException $e) {
17+
// suppress
18+
}
19+
try {
20+
NotExist::create()->value();
21+
} catch (UndefinedTypeException $e) {
22+
// suppress
23+
}
24+
NotFound::create();
25+
NotSet::create();
26+
Undefined::create();
27+
Unknown::create();

src/Usage/integer.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
use PhpTypedValues\Integer\Alias\Id;
4+
use PhpTypedValues\Integer\Alias\Integer;
5+
use PhpTypedValues\Integer\Alias\IntType;
6+
use PhpTypedValues\Integer\Alias\NonNegativeInt;
7+
use PhpTypedValues\Integer\Alias\PositiveInt;
8+
use PhpTypedValues\Integer\Alias\TinyInt;
9+
use PhpTypedValues\Integer\IntegerNonNegative;
10+
use PhpTypedValues\Integer\IntegerPositive;
11+
use PhpTypedValues\Integer\IntegerStandard;
12+
use PhpTypedValues\Integer\IntegerWeekDay;
13+
use PhpTypedValues\Integer\MariaDb\IntTiny;
14+
15+
/**
16+
* Integer.
17+
*/
18+
testInteger(IntegerStandard::fromInt(10)->value());
19+
testPositiveInt(IntegerPositive::fromInt(10)->value());
20+
testNonNegativeInt(IntegerNonNegative::fromInt(10)->value());
21+
testWeekDayInt(IntegerWeekDay::fromInt(7)->value());
22+
23+
// DB tinyint usage
24+
echo TinyInt::fromInt(-5)->toString() . \PHP_EOL;
25+
echo IntTiny::fromInt(-5)->toString() . \PHP_EOL;
26+
echo IntTiny::fromString('127')->toString() . \PHP_EOL;
27+
28+
echo NonNegativeInt::fromString('10')->toString() . \PHP_EOL;
29+
echo PositiveInt::fromString('10')->toString() . \PHP_EOL;
30+
echo IntegerStandard::fromString('10')->toString() . \PHP_EOL;
31+
echo Id::fromString('10')->toString() . \PHP_EOL;
32+
echo IntType::fromString('10')->toString() . \PHP_EOL;
33+
echo Integer::fromString('10')->toString() . \PHP_EOL;
34+
35+
/**
36+
* Artificial functions.
37+
*/
38+
function testInteger(int $i): int
39+
{
40+
return $i;
41+
}
42+
43+
/**
44+
* @param positive-int $i
45+
*/
46+
function testPositiveInt(int $i): int
47+
{
48+
return $i;
49+
}
50+
51+
/**
52+
* @param non-negative-int $i
53+
*/
54+
function testNonNegativeInt(int $i): int
55+
{
56+
return $i;
57+
}
58+
59+
/**
60+
* @param int<1, 7> $i
61+
*/
62+
function testWeekDayInt(int $i): int
63+
{
64+
return $i;
65+
}

0 commit comments

Comments
 (0)