forked from CuyZ/Valinor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat/normalizer
- Loading branch information
Showing
13 changed files
with
224 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/Integration/Mapping/Fixture/SimpleNamespacedObject.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleNamespace; | ||
|
||
final class SimpleNamespacedObject | ||
{ | ||
public function __construct( | ||
/** @var list<string> */ | ||
public array $value, | ||
) {} | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/Integration/Mapping/Object/SimpleNamespacedObjectMappingTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; | ||
|
||
use CuyZ\Valinor\Mapper\MappingError; | ||
use CuyZ\Valinor\MapperBuilder; | ||
use CuyZ\Valinor\Tests\Integration\IntegrationTest; | ||
use SimpleNamespace\SimpleNamespacedObject; | ||
|
||
final class SimpleNamespacedObjectMappingTest extends IntegrationTest | ||
{ | ||
public function test_simple_namespaced_object_can_be_mapped(): void | ||
{ | ||
require_once(__DIR__ . '/../Fixture/SimpleNamespacedObject.php'); | ||
|
||
try { | ||
$object = (new MapperBuilder())->mapper()->map(SimpleNamespacedObject::class, ['foo']); | ||
} catch (MappingError $error) { | ||
$this->mappingFail($error); | ||
} | ||
|
||
self::assertSame(['foo'], $object->value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Tests\Integration\Mapping; | ||
|
||
use CuyZ\Valinor\Mapper\MappingError; | ||
use CuyZ\Valinor\MapperBuilder; | ||
use CuyZ\Valinor\Tests\Integration\IntegrationTest; | ||
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\City; | ||
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject; | ||
|
||
final class UnionMappingTest extends IntegrationTest | ||
{ | ||
public function test_union_with_int_or_object(): void | ||
{ | ||
try { | ||
$array = (new MapperBuilder())->mapper()->map("list<int|" . SimpleObject::class . ">", [123, "foo"]); | ||
} catch (MappingError $error) { | ||
$this->mappingFail($error); | ||
} | ||
|
||
self::assertSame(123, $array[0]); | ||
self::assertInstanceOf(SimpleObject::class, $array[1]); | ||
} | ||
|
||
public function test_union_with_string_or_object_prioritizes_string(): void | ||
{ | ||
try { | ||
$array = (new MapperBuilder()) | ||
->mapper() | ||
->map("list<string|" . SimpleObject::class . ">", ["foo", "bar", "baz"]); | ||
} catch (MappingError $error) { | ||
$this->mappingFail($error); | ||
} | ||
|
||
self::assertSame(["foo", "bar", "baz"], $array); | ||
} | ||
|
||
public function test_union_with_string_literal_or_object_prioritizes_string_literal(): void | ||
{ | ||
try { | ||
$array = (new MapperBuilder()) | ||
->mapper() | ||
->map("list<'foo'|" . SimpleObject::class . "|'bar'>", ["foo", "bar", "baz"]); | ||
} catch (MappingError $error) { | ||
$this->mappingFail($error); | ||
} | ||
|
||
self::assertSame("foo", $array[0]); | ||
self::assertSame("bar", $array[1]); | ||
self::assertInstanceOf(SimpleObject::class, $array[2]); | ||
} | ||
|
||
public function test_union_of_objects(): void | ||
{ | ||
try { | ||
$array = (new MapperBuilder()) | ||
->mapper() | ||
->map( | ||
"list<" . SimpleObject::class . "|" . City::class . ">", | ||
["foo", ["name" => "foobar", "timeZone" => "UTC"], "baz"], | ||
); | ||
} catch (MappingError $error) { | ||
$this->mappingFail($error); | ||
} | ||
|
||
self::assertInstanceOf(SimpleObject::class, $array[0]); | ||
self::assertInstanceOf(City::class, $array[1]); | ||
self::assertInstanceOf(SimpleObject::class, $array[2]); | ||
} | ||
} |