File tree Expand file tree Collapse file tree 3 files changed +57
-2
lines changed
packages/mapper/src/Serializers Expand file tree Collapse file tree 3 files changed +57
-2
lines changed Original file line number Diff line number Diff line change 22
33namespace Tempest \Mapper \Serializers ;
44
5- use Tempest \Mapper \Exceptions \CannotCastValue ;
5+ use Tempest \Mapper \Exceptions \CannotSerializeValue ;
66use Tempest \Mapper \Serializer ;
77
88use function Tempest \map ;
@@ -12,7 +12,7 @@ final class DtoSerializer implements Serializer
1212 public function serialize (mixed $ input ): array |string
1313 {
1414 if (! is_object ($ input )) {
15- throw new CannotCastValue ('object ' );
15+ throw new CannotSerializeValue ('object ' );
1616 }
1717
1818 $ data = map ($ input )->toArray ();
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tests \Tempest \Integration \Mapper \Casters ;
4+
5+ use Tempest \Mapper \Casters \DtoCaster ;
6+ use Tempest \Mapper \Exceptions \CannotCastValue ;
7+ use Tests \Tempest \Integration \FrameworkIntegrationTestCase ;
8+ use Tests \Tempest \Integration \Mapper \Fixtures \MyObject ;
9+
10+ final class DtoCasterTest extends FrameworkIntegrationTestCase
11+ {
12+ public function test_cast (): void
13+ {
14+ $ json = json_encode (['type ' => MyObject::class, 'data ' => ['name ' => 'test ' ]]);
15+
16+ $ dto = new DtoCaster ()->cast ($ json );
17+
18+ $ this ->assertInstanceOf (MyObject::class, $ dto );
19+ $ this ->assertSame ('test ' , $ dto ->name );
20+ }
21+ public function test_cannot_cast_with_invalid_json (): void
22+ {
23+ $ json = '' ;
24+
25+ $ this ->expectException (CannotCastValue::class);
26+
27+ new DtoCaster ()->cast ($ json );
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tests \Tempest \Integration \Mapper \Serializers ;
4+
5+ use Tempest \Mapper \Exceptions \CannotSerializeValue ;
6+ use Tempest \Mapper \Serializers \DtoSerializer ;
7+ use Tests \Tempest \Integration \FrameworkIntegrationTestCase ;
8+ use Tests \Tempest \Integration \Mapper \Fixtures \MyObject ;
9+
10+ final class DtoSerializerTest extends FrameworkIntegrationTestCase
11+ {
12+ public function test_serialize (): void
13+ {
14+ $ this ->assertSame (
15+ json_encode (['type ' => MyObject::class, 'data ' => ['name ' => 'test ' ]]),
16+ new DtoSerializer ()->serialize (new MyObject (name: 'test ' )),
17+ );
18+ }
19+
20+ public function test_cannot_serialize_non_object (): void
21+ {
22+ $ this ->expectException (CannotSerializeValue::class);
23+
24+ new DtoSerializer ()->serialize ([]);
25+ }
26+ }
You can’t perform that action at this time.
0 commit comments