Skip to content

Commit 9c8eb83

Browse files
committed
Add string tests
1 parent 5987373 commit 9c8eb83

File tree

4 files changed

+132
-14
lines changed

4 files changed

+132
-14
lines changed

tests/Type/ArrayKeyTypeTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,24 @@ protected static function castValues(bool $strict): iterable
100100
$value === '' => '',
101101
// Type casts
102102
$strict === false => match (true) {
103-
// Numeric integer-like string values
104103
$value === "42" => 42,
105104
$value === "1" => 1,
106105
$value === "0" => 0,
107106
$value === "-1" => -1,
108107
$value === "-42" => -42,
109-
// Float values
110108
$value === 42.0 => 42,
111109
$value === 1.0 => 1,
112110
$value === 0.0 => 0,
113111
$value === -1.0 => -1,
114112
$value === -42.0 => -42,
115-
// Numeric float-like string values
116113
$value === "42.0" => 42,
117114
$value === "1.0" => 1,
118115
$value === "0.0" => 0,
119116
$value === "-1.0" => -1,
120117
$value === "-42.0" => -42,
121-
// Null
122118
$value === null => 0,
123-
// Boolean
124119
$value === true => 1,
125120
$value === false => 0,
126-
// Enum
127121
$value === IntBackedEnumStub::ExampleCase => IntBackedEnumStub::ExampleCase->value,
128122
$value === StringBackedEnumStub::ExampleCase => StringBackedEnumStub::ExampleCase->value,
129123
$value === UnitEnumStub::ExampleCase => UnitEnumStub::ExampleCase->name,

tests/Type/IntTypeTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,25 @@ protected static function castValues(bool $strict): iterable
5050
$value === \PHP_INT_MIN => \PHP_INT_MIN,
5151
// Type casts
5252
$strict === false => match (true) {
53-
// Numeric integer-like string values
5453
$value === "42" => 42,
5554
$value === "1" => 1,
5655
$value === "0" => 0,
5756
$value === "-1" => -1,
5857
$value === "-42" => -42,
59-
// Float values
6058
$value === 42.0 => 42,
6159
$value === 1.0 => 1,
6260
$value === 0.0 => 0,
6361
$value === -1.0 => -1,
6462
$value === -42.0 => -42,
65-
// Numeric float-like string values
6663
$value === "42.0" => 42,
6764
$value === "1.0" => 1,
6865
$value === "0.0" => 0,
6966
$value === "-1.0" => -1,
7067
$value === "-42.0" => -42,
71-
// Null
7268
$value === null => 0,
73-
// Boolean
7469
$value === true => 1,
7570
$value === false => 0,
76-
// Enum
71+
\is_resource($value) => \get_resource_id($value),
7772
$value === IntBackedEnumStub::ExampleCase => IntBackedEnumStub::ExampleCase->value,
7873
default => $default,
7974
},

tests/Type/StringTypeTest.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Mapper\Tests\Type;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\Group;
9+
use TypeLang\Mapper\Tests\Type\Stub\IntBackedEnumStub;
10+
use TypeLang\Mapper\Tests\Type\Stub\StringBackedEnumStub;
11+
use TypeLang\Mapper\Tests\Type\Stub\UnitEnumStub;
12+
use TypeLang\Mapper\Type\ArrayKeyType;
13+
use TypeLang\Mapper\Type\Coercer\ArrayKeyTypeCoercer;
14+
use TypeLang\Mapper\Type\StringType;
15+
use TypeLang\Mapper\Type\TypeInterface;
16+
17+
#[Group('types')]
18+
#[CoversClass(ArrayKeyType::class)]
19+
#[CoversClass(ArrayKeyTypeCoercer::class)]
20+
final class StringTypeTest extends SymmetricTypeTestCase
21+
{
22+
protected static function createType(): TypeInterface
23+
{
24+
return new StringType();
25+
}
26+
27+
protected static function matchValues(bool $strict): iterable
28+
{
29+
foreach (self::defaultMatchDataProviderSamples() as $value => $default) {
30+
yield $value => match (true) {
31+
$value === '9223372036854775808',
32+
$value === '9223372036854775807',
33+
$value === '42',
34+
$value === '1',
35+
$value === '0',
36+
$value === '-1',
37+
$value === '-42',
38+
$value === '-9223372036854775808',
39+
$value === '-9223372036854775809',
40+
$value === '9223372036854775808.0',
41+
$value === '9223372036854775807.0',
42+
$value === '42.5',
43+
$value === '42.0',
44+
$value === '1.0',
45+
$value === '0.0',
46+
$value === '-1.0',
47+
$value === '-42.0',
48+
$value === '-42.5',
49+
$value === '-9223372036854775808.0',
50+
$value === '-9223372036854775809.0',
51+
$value === 'true',
52+
$value === 'false',
53+
$value === 'non empty',
54+
$value === '' => true,
55+
default => $default,
56+
};
57+
}
58+
}
59+
60+
protected static function castValues(bool $strict): iterable
61+
{
62+
foreach (self::defaultCastDataProviderSamples() as $value => $default) {
63+
yield $value => match (true) {
64+
$value === '9223372036854775808' => '9223372036854775808',
65+
$value === '9223372036854775807' => '9223372036854775807',
66+
$value === '42' => '42',
67+
$value === '1' => '1',
68+
$value === '0' => '0',
69+
$value === '-1' => '-1',
70+
$value === '-42' => '-42',
71+
$value === '-9223372036854775808' => '-9223372036854775808',
72+
$value === '-9223372036854775809' => '-9223372036854775809',
73+
$value === '9223372036854775808.0' => '9223372036854775808.0',
74+
$value === '9223372036854775807.0' => '9223372036854775807.0',
75+
$value === '42.5' => '42.5',
76+
$value === '42.0' => '42.0',
77+
$value === '1.0' => '1.0',
78+
$value === '0.0' => '0.0',
79+
$value === '-1.0' => '-1.0',
80+
$value === '-42.0' => '-42.0',
81+
$value === '-42.5' => '-42.5',
82+
$value === '-9223372036854775808.0' => '-9223372036854775808.0',
83+
$value === '-9223372036854775809.0' => '-9223372036854775809.0',
84+
$value === 'true' => 'true',
85+
$value === 'false' => 'false',
86+
$value === 'non empty' => 'non empty',
87+
$value === '' => '',
88+
// Type casts
89+
$strict === false => match (true) {
90+
$value === \PHP_INT_MAX + 1 => '9223372036854775808.0',
91+
$value === \PHP_INT_MAX => '9223372036854775807',
92+
$value === 42 => '42',
93+
$value === 1 => '1',
94+
$value === 0 => '0',
95+
$value === -1 => '-1',
96+
$value === -42 => '-42',
97+
$value === \PHP_INT_MIN => '-9223372036854775808',
98+
$value === \PHP_INT_MIN - 1 => '-9223372036854775808.0',
99+
$value === 42.0 => '42.0',
100+
$value === 42.5 => '42.5',
101+
$value === 1.0 => '1.0',
102+
$value === 0.0 => '0.0',
103+
$value === -1.0 => '-1.0',
104+
$value === -42.0 => '-42.0',
105+
$value === -42.5 => '-42.5',
106+
$value === null => '',
107+
$value === true => 'true',
108+
$value === false => 'false',
109+
$value === \INF => 'inf',
110+
$value === -\INF => '-inf',
111+
\is_float($value) && \is_nan($value) => 'nan',
112+
\is_resource($value) => match (\get_resource_type($value)) {
113+
'stream' => 'stream',
114+
default => $default,
115+
},
116+
$value === IntBackedEnumStub::ExampleCase => (string) IntBackedEnumStub::ExampleCase->value,
117+
$value === StringBackedEnumStub::ExampleCase => StringBackedEnumStub::ExampleCase->value,
118+
$value === UnitEnumStub::ExampleCase => UnitEnumStub::ExampleCase->name,
119+
default => $default,
120+
},
121+
default => $default,
122+
};
123+
}
124+
}
125+
}

tests/Type/TypeTestCase.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,13 @@ private static function defaultDataProviderSamples(): iterable
128128
yield (object)['key' => 'val'];
129129
yield (object)['val'];
130130

131-
// Enum values
131+
// Resource
132+
yield \fopen('php://memory', 'rb');
133+
\fclose($stream = \fopen('php://memory', 'rb'));
134+
yield $stream; // closed resource
132135

136+
// Enum values
137+
yield UnitEnumStub::ExampleCase;
133138
// This behavior can be confusing to the user, since the "public"
134139
// type (i.e., the one displayed to the user) for an enum is an int,
135140
// but the actual type is an enum's object.
@@ -138,7 +143,6 @@ private static function defaultDataProviderSamples(): iterable
138143
// which is very bad.
139144
yield IntBackedEnumStub::ExampleCase;
140145
yield StringBackedEnumStub::ExampleCase;
141-
yield UnitEnumStub::ExampleCase;
142146
}
143147

144148
/**

0 commit comments

Comments
 (0)