2021-11-28 17:43:02 +01:00
< ? php
declare ( strict_types = 1 );
namespace CuyZ\Valinor\Tests\Unit\Mapper\Tree ;
use CuyZ\Valinor\Mapper\Tree\Exception\CannotGetParentOfRootShell ;
use CuyZ\Valinor\Mapper\Tree\Exception\NewShellTypeDoesNotMatch ;
feat!: make mapper more strict and allow flexible mode
The mapper is now more type-sensitive and will fail in the following
situations:
- When a value does not match exactly the awaited scalar type, for
instance a string `"42"` given to a node that awaits an integer.
- When unnecessary array keys are present, for instance mapping an array
`['foo' => …, 'bar' => …, 'baz' => …]` to an object that needs only
`foo` and `bar`.
- When permissive types like `mixed` or `object` are encountered.
These limitations can be bypassed by enabling the flexible mode:
```php
(new \CuyZ\Valinor\MapperBuilder())
->flexible()
->mapper();
->map('array{foo: int, bar: bool}', [
'foo' => '42', // Will be cast from `string` to `int`
'bar' => 'true', // Will be cast from `string` to `bool`
'baz' => '…', // Will be ignored
]);
```
When using this library for a provider application — for instance an API
endpoint that can be called with a JSON payload — it is recommended to
use the strict mode. This ensures that the consumers of the API provide
the exact awaited data structure, and prevents unknown values to be
passed.
When using this library as a consumer of an external source, it can make
sense to enable the flexible mode. This allows for instance to convert
string numeric values to integers or to ignore data that is present in
the source but not needed in the application.
---
All these changes led to a new check that runs on all registered object
constructors. If a collision is found between several constructors that
have the same signature (the same parameter names), an exception will be
thrown.
```php
final class SomeClass
{
public static function constructorA(string $foo, string $bar): self
{
// …
}
public static function constructorB(string $foo, string $bar): self
{
// …
}
}
(new \CuyZ\Valinor\MapperBuilder())
->registerConstructor(
SomeClass::constructorA(...),
SomeClass::constructorB(...),
)
->mapper();
->map(SomeClass::class, [
'foo' => 'foo',
'bar' => 'bar',
]);
// Exception: A collision was detected […]
```
2022-06-23 10:30:36 +02:00
use CuyZ\Valinor\Mapper\Tree\Exception\ShellHasNoValue ;
2021-11-28 17:43:02 +01:00
use CuyZ\Valinor\Mapper\Tree\Exception\UnresolvableShellType ;
use CuyZ\Valinor\Mapper\Tree\Shell ;
use CuyZ\Valinor\Tests\Fake\Definition\FakeAttributes ;
feat!: make mapper more strict and allow flexible mode
The mapper is now more type-sensitive and will fail in the following
situations:
- When a value does not match exactly the awaited scalar type, for
instance a string `"42"` given to a node that awaits an integer.
- When unnecessary array keys are present, for instance mapping an array
`['foo' => …, 'bar' => …, 'baz' => …]` to an object that needs only
`foo` and `bar`.
- When permissive types like `mixed` or `object` are encountered.
These limitations can be bypassed by enabling the flexible mode:
```php
(new \CuyZ\Valinor\MapperBuilder())
->flexible()
->mapper();
->map('array{foo: int, bar: bool}', [
'foo' => '42', // Will be cast from `string` to `int`
'bar' => 'true', // Will be cast from `string` to `bool`
'baz' => '…', // Will be ignored
]);
```
When using this library for a provider application — for instance an API
endpoint that can be called with a JSON payload — it is recommended to
use the strict mode. This ensures that the consumers of the API provide
the exact awaited data structure, and prevents unknown values to be
passed.
When using this library as a consumer of an external source, it can make
sense to enable the flexible mode. This allows for instance to convert
string numeric values to integers or to ignore data that is present in
the source but not needed in the application.
---
All these changes led to a new check that runs on all registered object
constructors. If a collision is found between several constructors that
have the same signature (the same parameter names), an exception will be
thrown.
```php
final class SomeClass
{
public static function constructorA(string $foo, string $bar): self
{
// …
}
public static function constructorB(string $foo, string $bar): self
{
// …
}
}
(new \CuyZ\Valinor\MapperBuilder())
->registerConstructor(
SomeClass::constructorA(...),
SomeClass::constructorB(...),
)
->mapper();
->map(SomeClass::class, [
'foo' => 'foo',
'bar' => 'bar',
]);
// Exception: A collision was detected […]
```
2022-06-23 10:30:36 +02:00
use CuyZ\Valinor\Tests\Fake\Mapper\FakeShell ;
2021-11-28 17:43:02 +01:00
use CuyZ\Valinor\Tests\Fake\Type\FakeType ;
use CuyZ\Valinor\Type\Types\UnresolvableType ;
use PHPUnit\Framework\TestCase ;
final class ShellTest extends TestCase
{
public function test_type_and_value_can_be_retrieved () : void
{
$type = new FakeType ();
$value = 'foo' ;
$shell = Shell :: root ( $type , $value );
self :: assertSame ( $type , $shell -> type ());
self :: assertSame ( $value , $shell -> value ());
}
2022-07-26 19:27:34 +02:00
public function test_root_path_is_fixed () : void
{
$shell = Shell :: root ( new FakeType (), 'foo' );
self :: assertSame ( '*root*' , $shell -> path ());
}
2021-11-28 17:43:02 +01:00
public function test_change_type_changes_type () : void
{
2022-10-04 21:49:00 +02:00
$typeA = new FakeType ();
$typeB = FakeType :: matching ( $typeA );
2021-11-28 17:43:02 +01:00
$shellA = Shell :: root ( $typeA , []);
$shellB = $shellA -> withType ( $typeB );
self :: assertNotSame ( $shellA , $shellB );
self :: assertSame ( $typeB , $shellB -> type ());
}
feat!: make mapper more strict and allow flexible mode
The mapper is now more type-sensitive and will fail in the following
situations:
- When a value does not match exactly the awaited scalar type, for
instance a string `"42"` given to a node that awaits an integer.
- When unnecessary array keys are present, for instance mapping an array
`['foo' => …, 'bar' => …, 'baz' => …]` to an object that needs only
`foo` and `bar`.
- When permissive types like `mixed` or `object` are encountered.
These limitations can be bypassed by enabling the flexible mode:
```php
(new \CuyZ\Valinor\MapperBuilder())
->flexible()
->mapper();
->map('array{foo: int, bar: bool}', [
'foo' => '42', // Will be cast from `string` to `int`
'bar' => 'true', // Will be cast from `string` to `bool`
'baz' => '…', // Will be ignored
]);
```
When using this library for a provider application — for instance an API
endpoint that can be called with a JSON payload — it is recommended to
use the strict mode. This ensures that the consumers of the API provide
the exact awaited data structure, and prevents unknown values to be
passed.
When using this library as a consumer of an external source, it can make
sense to enable the flexible mode. This allows for instance to convert
string numeric values to integers or to ignore data that is present in
the source but not needed in the application.
---
All these changes led to a new check that runs on all registered object
constructors. If a collision is found between several constructors that
have the same signature (the same parameter names), an exception will be
thrown.
```php
final class SomeClass
{
public static function constructorA(string $foo, string $bar): self
{
// …
}
public static function constructorB(string $foo, string $bar): self
{
// …
}
}
(new \CuyZ\Valinor\MapperBuilder())
->registerConstructor(
SomeClass::constructorA(...),
SomeClass::constructorB(...),
)
->mapper();
->map(SomeClass::class, [
'foo' => 'foo',
'bar' => 'bar',
]);
// Exception: A collision was detected […]
```
2022-06-23 10:30:36 +02:00
public function test_get_value_when_shell_has_no_value_throws_exception () : void
{
$this -> expectException ( ShellHasNoValue :: class );
$this -> expectExceptionCode ( 1655029618 );
$this -> expectExceptionMessage ( 'Shell has no value.' );
FakeShell :: any () -> child ( 'foo' , new FakeType ()) -> value ();
}
2021-11-28 17:43:02 +01:00
public function test_unresolvable_type_throws_exception () : void
{
2022-07-01 21:51:59 +02:00
$type = new UnresolvableType ( 'some-type' , 'some message' );
2021-11-28 17:43:02 +01:00
$this -> expectException ( UnresolvableShellType :: class );
$this -> expectExceptionCode ( 1630943848 );
$this -> expectExceptionMessage ( 'some message' );
Shell :: root ( $type , []);
}
public function test_change_type_with_invalid_type_throws_exception () : void
{
$typeA = new FakeType ();
$typeB = new FakeType ();
$this -> expectException ( NewShellTypeDoesNotMatch :: class );
$this -> expectExceptionCode ( 1628845224 );
2022-07-26 19:27:34 +02:00
$this -> expectExceptionMessage ( " Trying to change the type of the shell at path `*root*`: ` { $typeB -> toString () } ` is not a valid subtype of ` { $typeA -> toString () } `. " );
2021-11-28 17:43:02 +01:00
( Shell :: root ( $typeA , [])) -> withType ( $typeB );
}
public function test_change_value_changes_value () : void
{
$valueA = 'foo' ;
$valueB = 'bar' ;
$shellA = Shell :: root ( new FakeType (), $valueA );
$shellB = $shellA -> withValue ( $valueB );
self :: assertNotSame ( $shellA , $shellB );
self :: assertSame ( $valueB , $shellB -> value ());
}
public function test_root_shell_is_root () : void
{
$shell = Shell :: root ( new FakeType (), []);
self :: assertTrue ( $shell -> isRoot ());
self :: assertSame ( '' , $shell -> name ());
}
public function test_get_root_shell_parent_throws_exception () : void
{
$this -> expectException ( CannotGetParentOfRootShell :: class );
$this -> expectExceptionCode ( 1630674894 );
$this -> expectExceptionMessage ( 'Impossible to get the parent of a root shell.' );
$shell = Shell :: root ( new FakeType (), []);
$shell -> parent ();
}
public function test_shell_child_values_can_be_retrieved () : void
{
$value = 'some value' ;
2021-12-30 19:23:13 +01:00
$type = FakeType :: permissive ();
2021-11-28 17:43:02 +01:00
$attributes = new FakeAttributes ();
$shell = Shell :: root ( new FakeType (), []);
feat!: make mapper more strict and allow flexible mode
The mapper is now more type-sensitive and will fail in the following
situations:
- When a value does not match exactly the awaited scalar type, for
instance a string `"42"` given to a node that awaits an integer.
- When unnecessary array keys are present, for instance mapping an array
`['foo' => …, 'bar' => …, 'baz' => …]` to an object that needs only
`foo` and `bar`.
- When permissive types like `mixed` or `object` are encountered.
These limitations can be bypassed by enabling the flexible mode:
```php
(new \CuyZ\Valinor\MapperBuilder())
->flexible()
->mapper();
->map('array{foo: int, bar: bool}', [
'foo' => '42', // Will be cast from `string` to `int`
'bar' => 'true', // Will be cast from `string` to `bool`
'baz' => '…', // Will be ignored
]);
```
When using this library for a provider application — for instance an API
endpoint that can be called with a JSON payload — it is recommended to
use the strict mode. This ensures that the consumers of the API provide
the exact awaited data structure, and prevents unknown values to be
passed.
When using this library as a consumer of an external source, it can make
sense to enable the flexible mode. This allows for instance to convert
string numeric values to integers or to ignore data that is present in
the source but not needed in the application.
---
All these changes led to a new check that runs on all registered object
constructors. If a collision is found between several constructors that
have the same signature (the same parameter names), an exception will be
thrown.
```php
final class SomeClass
{
public static function constructorA(string $foo, string $bar): self
{
// …
}
public static function constructorB(string $foo, string $bar): self
{
// …
}
}
(new \CuyZ\Valinor\MapperBuilder())
->registerConstructor(
SomeClass::constructorA(...),
SomeClass::constructorB(...),
)
->mapper();
->map(SomeClass::class, [
'foo' => 'foo',
'bar' => 'bar',
]);
// Exception: A collision was detected […]
```
2022-06-23 10:30:36 +02:00
$child = $shell -> child ( 'foo' , $type , $attributes ) -> withValue ( $value );
2021-11-28 17:43:02 +01:00
self :: assertSame ( 'foo' , $child -> name ());
self :: assertSame ( 'foo' , $child -> path ());
self :: assertSame ( $type , $child -> type ());
self :: assertSame ( $value , $child -> value ());
self :: assertSame ( $attributes , $child -> attributes ());
}
}