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.
feat: handle variadic parameters in constructors
Using variadic parameters is now handled properly by the library, meaning the following example will run: ```php final class SomeClass { /** @var string[] */ private array $values; public function __construct(string ...$values) { $this->values = $values; } } (new \CuyZ\Valinor\MapperBuilder()) ->mapper() ->map(SomeClass::class, ['foo', 'bar', 'baz']); ```
- Loading branch information
Showing
11 changed files
with
197 additions
and
13 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
127 changes: 127 additions & 0 deletions
127
tests/Integration/Mapping/VariadicParameterMappingTest.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,127 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Tests\Integration\Mapping; | ||
|
||
use CuyZ\Valinor\Mapper\MappingError; | ||
use CuyZ\Valinor\Tests\Integration\IntegrationTest; | ||
|
||
final class VariadicParameterMappingTest extends IntegrationTest | ||
{ | ||
public function test_only_variadic_parameters_are_mapped_properly(): void | ||
{ | ||
try { | ||
$object = $this->mapperBuilder->mapper()->map(SomeClassWithOnlyVariadicParameters::class, [ | ||
'values' => ['foo', 'bar', 'baz'] | ||
]); | ||
} catch (MappingError $error) { | ||
$this->mappingFail($error); | ||
} | ||
|
||
self::assertSame(['foo', 'bar', 'baz'], $object->values); | ||
} | ||
|
||
public function test_named_constructor_with_only_variadic_parameters_are_mapped_properly(): void | ||
{ | ||
try { | ||
$object = $this->mapperBuilder->mapper()->map(SomeClassWithNamedConstructorWithOnlyVariadicParameters::class, [ | ||
'values' => ['foo', 'bar', 'baz'] | ||
]); | ||
} catch (MappingError $error) { | ||
$this->mappingFail($error); | ||
} | ||
|
||
self::assertSame(['foo', 'bar', 'baz'], $object->values); | ||
} | ||
|
||
public function test_non_variadic_and_variadic_parameters_are_mapped_properly(): void | ||
{ | ||
try { | ||
$object = $this->mapperBuilder->mapper()->map(SomeClassWithNonVariadicAndVariadicParameters::class, [ | ||
'int' => 42, | ||
'values' => ['foo', 'bar', 'baz'] | ||
]); | ||
} catch (MappingError $error) { | ||
$this->mappingFail($error); | ||
} | ||
|
||
self::assertSame(42, $object->int); | ||
self::assertSame(['foo', 'bar', 'baz'], $object->values); | ||
} | ||
|
||
public function test_named_constructor_with_non_variadic_and_variadic_parameters_are_mapped_properly(): void | ||
{ | ||
try { | ||
$object = $this->mapperBuilder->mapper()->map(SomeClassWithNamedConstructorWithNonVariadicAndVariadicParameters::class, [ | ||
'int' => 42, | ||
'values' => ['foo', 'bar', 'baz'] | ||
]); | ||
} catch (MappingError $error) { | ||
$this->mappingFail($error); | ||
} | ||
|
||
self::assertSame(42, $object->int); | ||
self::assertSame(['foo', 'bar', 'baz'], $object->values); | ||
} | ||
} | ||
|
||
final class SomeClassWithOnlyVariadicParameters | ||
{ | ||
/** @var string[] */ | ||
public array $values; | ||
|
||
public function __construct(string ...$values) | ||
{ | ||
$this->values = $values; | ||
} | ||
} | ||
|
||
final class SomeClassWithNamedConstructorWithOnlyVariadicParameters | ||
{ | ||
/** @var string[] */ | ||
public array $values; | ||
|
||
private function __construct(string ...$values) | ||
{ | ||
$this->values = $values; | ||
} | ||
|
||
public static function new(string ...$values): self | ||
{ | ||
return new self(...$values); | ||
} | ||
} | ||
|
||
final class SomeClassWithNonVariadicAndVariadicParameters | ||
{ | ||
public int $int; | ||
|
||
/** @var string[] */ | ||
public array $values; | ||
|
||
public function __construct(int $int, string ...$values) | ||
{ | ||
$this->int = $int; | ||
$this->values = $values; | ||
} | ||
} | ||
|
||
final class SomeClassWithNamedConstructorWithNonVariadicAndVariadicParameters | ||
{ | ||
public int $int; | ||
|
||
/** @var string[] */ | ||
public array $values; | ||
|
||
private function __construct(int $int, string ...$values) | ||
{ | ||
$this->int = $int; | ||
$this->values = $values; | ||
} | ||
|
||
public static function new(int $int, string ...$values): self | ||
{ | ||
return new self($int, ...$values); | ||
} | ||
} |
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