-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mapper): allow mapping value to object via cast method
- Loading branch information
Showing
8 changed files
with
115 additions
and
10 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
44 changes: 44 additions & 0 deletions
44
src/Tempest/Mapper/tests/Mappers/MixedToObjectMapperTest.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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Mapper\Tests\Mappers; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Tempest\Mapper\Casters\CasterFactory; | ||
use Tempest\Mapper\Mappers\MixedToObjectMapper; | ||
use Tempest\Mapper\Tests\Support\StringCastValue; | ||
use Tempest\Mapper\Tests\Support\StringValue; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class MixedToObjectMapperTest extends TestCase | ||
{ | ||
private MixedToObjectMapper $subject; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->subject = new MixedToObjectMapper(new CasterFactory()); | ||
} | ||
|
||
public function test_map_array_to_object(): void | ||
{ | ||
$value = ['value' => 'Tempest']; | ||
|
||
/** @var StringValue $object */ | ||
$object = $this->subject->map($value, StringValue::class); | ||
|
||
$this->assertEquals('Tempest', $object->getValue()); | ||
} | ||
|
||
public function test_map_string_to_object_with_automatic_cast(): void | ||
{ | ||
$value = 'Tempest'; | ||
|
||
/** @var StringCastValue $object */ | ||
$object = $this->subject->map($value, StringCastValue::class); | ||
|
||
$this->assertEquals('Tempest', $object->getValue()); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Mapper\Tests\Support; | ||
|
||
final readonly class StringCastValue | ||
{ | ||
private function __construct(private string $value) | ||
{ | ||
} | ||
|
||
public function getValue(): string | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public static function cast(string $value): self | ||
{ | ||
return new self($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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Mapper\Tests\Support; | ||
|
||
final readonly class StringValue | ||
{ | ||
public function __construct(public string $value) | ||
{ | ||
} | ||
|
||
public function getValue(): string | ||
{ | ||
return $this->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
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