Skip to content

Commit a2aa029

Browse files
committed
Add more type tests
1 parent 9a87aef commit a2aa029

16 files changed

+325
-24
lines changed

src/Type/Coercer/ArrayKeyTypeCoercer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @template-implements TypeCoercerInterface<array-key>
1212
*/
13-
final class ArrayKeyTypeCoercer implements TypeCoercerInterface
13+
class ArrayKeyTypeCoercer implements TypeCoercerInterface
1414
{
1515
public function coerce(mixed $value, Context $context): int|string
1616
{

src/Type/Coercer/BoolTypeCoercer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* @template-implements TypeCoercerInterface<bool>
1111
*/
12-
final class BoolTypeCoercer implements TypeCoercerInterface
12+
class BoolTypeCoercer implements TypeCoercerInterface
1313
{
1414
public function coerce(mixed $value, Context $context): bool
1515
{

src/Type/Coercer/FloatTypeCoercer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @template-implements TypeCoercerInterface<float>
1212
*/
13-
final class FloatTypeCoercer implements TypeCoercerInterface
13+
class FloatTypeCoercer implements TypeCoercerInterface
1414
{
1515
public function coerce(mixed $value, Context $context): float
1616
{

src/Type/Coercer/IntTypeCoercer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @template-implements TypeCoercerInterface<int>
1212
*/
13-
final class IntTypeCoercer implements TypeCoercerInterface
13+
class IntTypeCoercer implements TypeCoercerInterface
1414
{
1515
public function coerce(mixed $value, Context $context): int
1616
{

src/Type/FloatLiteralType.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,12 @@ public function __construct(
3131
*/
3232
public function match(mixed $value, Context $context): bool
3333
{
34-
if (\is_int($value)) {
35-
return (float) $value === $this->value;
36-
}
37-
3834
return $value === $this->value;
3935
}
4036

4137
public function cast(mixed $value, Context $context): float
4238
{
43-
if ($this->match($value, $context)) {
39+
if ($value === $this->value) {
4440
return (float) $value;
4541
}
4642

tests/Type/ArrayKeyTypeTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\Attributes\Group;
99
use TypeLang\Mapper\Type\ArrayKeyType;
10+
use TypeLang\Mapper\Type\Coercer\TypeCoercerInterface;
1011
use TypeLang\Mapper\Type\TypeInterface;
1112

12-
#[Group('types')]
13+
#[Group('type')]
1314
#[CoversClass(ArrayKeyType::class)]
14-
final class ArrayKeyTypeTest extends TypeTestCase
15+
final class ArrayKeyTypeTest extends CoercibleTypeTestCase
1516
{
16-
protected static function createType(): TypeInterface
17+
protected static function createType(?TypeCoercerInterface $coercer = null): TypeInterface
1718
{
19+
if ($coercer !== null) {
20+
return new ArrayKeyType(coercer: $coercer);
21+
}
22+
1823
return new ArrayKeyType();
1924
}
2025

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Type\BoolLiteralType;
10+
use TypeLang\Mapper\Type\Coercer\TypeCoercerInterface;
11+
use TypeLang\Mapper\Type\TypeInterface;
12+
13+
#[Group('type')]
14+
#[CoversClass(BoolLiteralType::class)]
15+
final class BoolFalseLiteralTypeTest extends CoercibleTypeTestCase
16+
{
17+
protected static function createType(?TypeCoercerInterface $coercer = null): TypeInterface
18+
{
19+
if ($coercer !== null) {
20+
return new BoolLiteralType(false, coercer: $coercer);
21+
}
22+
23+
return new BoolLiteralType(false);
24+
}
25+
26+
protected static function matchValues(bool $normalize): iterable
27+
{
28+
foreach (self::defaultMatchDataProviderSamples() as $value => $default) {
29+
yield $value => match (true) {
30+
// Only false is matching
31+
$value === false => true,
32+
default => $default,
33+
};
34+
}
35+
}
36+
37+
protected static function castValues(bool $normalize): iterable
38+
{
39+
foreach (self::defaultCastDataProviderSamples() as $value => $default) {
40+
yield $value => match (true) {
41+
// Only false may cast to false
42+
$value === false => false,
43+
default => $default,
44+
};
45+
}
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Type\BoolLiteralType;
10+
use TypeLang\Mapper\Type\Coercer\TypeCoercerInterface;
11+
use TypeLang\Mapper\Type\TypeInterface;
12+
13+
#[Group('type')]
14+
#[CoversClass(BoolLiteralType::class)]
15+
final class BoolTrueLiteralTypeTest extends CoercibleTypeTestCase
16+
{
17+
protected static function createType(?TypeCoercerInterface $coercer = null): TypeInterface
18+
{
19+
if ($coercer !== null) {
20+
return new BoolLiteralType(true, coercer: $coercer);
21+
}
22+
23+
return new BoolLiteralType(true);
24+
}
25+
26+
protected static function matchValues(bool $normalize): iterable
27+
{
28+
foreach (self::defaultMatchDataProviderSamples() as $value => $default) {
29+
yield $value => match (true) {
30+
// Only true is matches
31+
$value === true => true,
32+
default => $default,
33+
};
34+
}
35+
}
36+
37+
protected static function castValues(bool $normalize): iterable
38+
{
39+
foreach (self::defaultCastDataProviderSamples() as $value => $default) {
40+
yield $value => match (true) {
41+
// Only true may casts
42+
$value === true => true,
43+
default => $default,
44+
};
45+
}
46+
}
47+
}

tests/Type/BoolTypeTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\Attributes\Group;
99
use TypeLang\Mapper\Type\BoolType;
10+
use TypeLang\Mapper\Type\Coercer\TypeCoercerInterface;
1011
use TypeLang\Mapper\Type\TypeInterface;
1112

12-
#[Group('types')]
13+
#[Group('type')]
1314
#[CoversClass(BoolType::class)]
14-
final class BoolTypeTest extends TypeTestCase
15+
final class BoolTypeTest extends CoercibleTypeTestCase
1516
{
16-
protected static function createType(): TypeInterface
17+
protected static function createType(?TypeCoercerInterface $coercer = null): TypeInterface
1718
{
19+
if ($coercer !== null) {
20+
return new BoolType(coercer: $coercer);
21+
}
22+
1823
return new BoolType();
1924
}
2025

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Mapper\Tests\Type;
6+
7+
use TypeLang\Mapper\Type\Coercer\TypeCoercerInterface;
8+
use TypeLang\Mapper\Type\TypeInterface;
9+
10+
abstract class CoercibleTypeTestCase extends TypeTestCase
11+
{
12+
abstract protected static function createType(?TypeCoercerInterface $coercer = null): TypeInterface;
13+
14+
/**
15+
* @api
16+
*/
17+
public function testUsesTypeCoercion(): void
18+
{
19+
$this->expectException(\BadMethodCallException::class);
20+
$this->expectExceptionMessage('on-coerce');
21+
22+
$coercer = $this->createMock(TypeCoercerInterface::class);
23+
$coercer->method('coerce')
24+
->willThrowException(new \BadMethodCallException('on-coerce'));
25+
26+
$value = \fopen('php://memory', 'rb');
27+
28+
$type = static::createType($coercer);
29+
$type->cast($value, $this->createNormalizationContext($value, false));
30+
}
31+
}

0 commit comments

Comments
 (0)