Skip to content

Commit 70b3025

Browse files
committed
add ValidatedMapperCompilerTest
1 parent af9e18f commit 70b3025

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare (strict_types=1);
2+
3+
namespace ShipMonkTests\InputMapper\Compiler\Mapper\Wrapper\Data;
4+
5+
use ShipMonk\InputMapper\Compiler\Mapper\Wrapper\ValidatedMapperCompiler;
6+
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException;
7+
use ShipMonk\InputMapper\Runtime\Mapper;
8+
use ShipMonk\InputMapper\Runtime\MapperProvider;
9+
use function is_int;
10+
11+
/**
12+
* Generated mapper by {@see ValidatedMapperCompiler}. Do not edit directly.
13+
*
14+
* @implements Mapper<int>
15+
*/
16+
class NotValidatedIntMapperMapper implements Mapper
17+
{
18+
public function __construct(private readonly MapperProvider $provider)
19+
{
20+
}
21+
22+
/**
23+
* @param list<string|int> $path
24+
* @throws MappingFailedException
25+
*/
26+
public function map(mixed $data, array $path = []): int
27+
{
28+
if (!is_int($data)) {
29+
throw MappingFailedException::incorrectType($data, $path, 'int');
30+
}
31+
32+
return $data;
33+
}
34+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare (strict_types=1);
2+
3+
namespace ShipMonkTests\InputMapper\Compiler\Mapper\Wrapper\Data;
4+
5+
use ShipMonk\InputMapper\Compiler\Mapper\Wrapper\ValidatedMapperCompiler;
6+
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException;
7+
use ShipMonk\InputMapper\Runtime\Mapper;
8+
use ShipMonk\InputMapper\Runtime\MapperProvider;
9+
use function is_int;
10+
11+
/**
12+
* Generated mapper by {@see ValidatedMapperCompiler}. Do not edit directly.
13+
*
14+
* @implements Mapper<positive-int>
15+
*/
16+
class PositiveIntMapperMapper implements Mapper
17+
{
18+
public function __construct(private readonly MapperProvider $provider)
19+
{
20+
}
21+
22+
/**
23+
* @param list<string|int> $path
24+
* @return positive-int
25+
* @throws MappingFailedException
26+
*/
27+
public function map(mixed $data, array $path = []): int
28+
{
29+
if (!is_int($data)) {
30+
throw MappingFailedException::incorrectType($data, $path, 'int');
31+
}
32+
33+
if ($data <= 0) {
34+
throw MappingFailedException::incorrectValue($data, $path, 'value greater than 0');
35+
}
36+
37+
return $data;
38+
}
39+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ShipMonkTests\InputMapper\Compiler\Mapper\Wrapper;
4+
5+
use ShipMonk\InputMapper\Compiler\Exception\CannotCompileMapperException;
6+
use ShipMonk\InputMapper\Compiler\Mapper\Scalar\MapInt;
7+
use ShipMonk\InputMapper\Compiler\Mapper\Wrapper\ValidatedMapperCompiler;
8+
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertPositiveInt;
9+
use ShipMonk\InputMapper\Compiler\Validator\String\AssertUrl;
10+
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException;
11+
use ShipMonkTests\InputMapper\Compiler\Mapper\MapperCompilerTestCase;
12+
13+
class ValidatedMapperCompilerTest extends MapperCompilerTestCase
14+
{
15+
16+
public function testCompileWithEmptyValidatorList(): void
17+
{
18+
$mapperCompiler = new ValidatedMapperCompiler(new MapInt(), []);
19+
$mapper = $this->compileMapper('NotValidatedIntMapper', $mapperCompiler);
20+
self::assertSame(1, $mapper->map(1));
21+
}
22+
23+
public function testCompile(): void
24+
{
25+
$mapperCompiler = new ValidatedMapperCompiler(new MapInt(), [new AssertPositiveInt()]);
26+
$mapper = $this->compileMapper('PositiveIntMapper', $mapperCompiler);
27+
28+
self::assertSame(1, $mapper->map(1));
29+
30+
self::assertException(
31+
MappingFailedException::class,
32+
'Failed to map data at path /: Expected value greater than 0, got 0',
33+
static fn() => $mapper->map(0),
34+
);
35+
}
36+
37+
public function testCompileWithIncompatibleValidator(): void
38+
{
39+
$mapperCompiler = new ValidatedMapperCompiler(new MapInt(), [new AssertUrl()]);
40+
41+
self::assertException(
42+
CannotCompileMapperException::class,
43+
'Cannot compile mapper with validator ShipMonk\InputMapper\Compiler\Validator\String\AssertUrl, because mapper output type \'int\' is not compatible with validator input type \'string\'',
44+
fn() => $this->compileMapper('IntMapperWithIncompatibleValidator', $mapperCompiler),
45+
);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)