Skip to content

Commit cd4c347

Browse files
committed
Increased code coverage with tests
1 parent 1c09a08 commit cd4c347

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Tests\Dummies\Serializers;
4+
5+
use Nuxtifyts\PhpDto\Contexts\PropertyContext;
6+
use Nuxtifyts\PhpDto\Exceptions\UnknownTypeException;
7+
use Nuxtifyts\PhpDto\Serializers\Serializer;
8+
use Nuxtifyts\PhpDto\Support\Traits\HasSerializers;
9+
10+
class HasSerializersDummyClass
11+
{
12+
use HasSerializers;
13+
14+
/**
15+
* @throws UnknownTypeException
16+
*
17+
* @return list<Serializer>
18+
*/
19+
public static function testGetSerializersFromPropertyContext(PropertyContext $propertyContext): array
20+
{
21+
return new self()->getSerializersFromPropertyContext($propertyContext);
22+
}
23+
24+
/**
25+
* @return list<Serializer>
26+
*/
27+
protected static function serializersList(): array
28+
{
29+
return [];
30+
}
31+
32+
/**
33+
* @return list<Serializer>
34+
*/
35+
protected function resolveSerializers(): array
36+
{
37+
return [];
38+
}
39+
}

tests/Unit/Concerns/BaseDataTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DateTimeImmutable;
66
use DateTimeInterface;
7+
use Nuxtifyts\PhpDto\Attributes\Property\Computed;
78
use Nuxtifyts\PhpDto\Data;
89
use Nuxtifyts\PhpDto\Exceptions\DeserializeException;
910
use Nuxtifyts\PhpDto\Exceptions\SerializeException;
@@ -112,6 +113,31 @@ public function will_throw_an_exception_if_an_invalid_value_is_passed_to_from_fu
112113
PersonData::from(false);
113114
}
114115

116+
/**
117+
* @throws Throwable
118+
*/
119+
#[Test]
120+
public function will_throw_deserialization_exception_when_invalid_parameter_is_used_with_computed_properties(): void
121+
{
122+
$object = new readonly class ('a', 'b') extends Data {
123+
#[Computed]
124+
public string $c;
125+
126+
public function __construct(
127+
public string $a,
128+
string $b
129+
) {
130+
$this->c = $a . $b;
131+
}
132+
};
133+
134+
self::expectException(DeserializeException::class);
135+
$object::from([
136+
'a' => 'a',
137+
'b' => 'b'
138+
]);
139+
}
140+
115141
/**
116142
* @throws Throwable
117143
*/

tests/Unit/Contexts/PropertyContextTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Nuxtifyts\PhpDto\Tests\Unit\Contexts;
44

5+
use Nuxtifyts\PhpDto\Tests\Dummies\Serializers\HasSerializersDummyClass;
56
use Nuxtifyts\PhpDto\Attributes\Property\Computed;
67
use Nuxtifyts\PhpDto\Attributes\Property\WithRefiner;
78
use Nuxtifyts\PhpDto\Contexts\PropertyContext;
89
use Nuxtifyts\PhpDto\Contexts\TypeContext;
910
use Nuxtifyts\PhpDto\Data;
1011
use Nuxtifyts\PhpDto\DataRefiners\DateTimeRefiner;
1112
use Nuxtifyts\PhpDto\Enums\Property\Type;
13+
use Nuxtifyts\PhpDto\Exceptions\UnknownTypeException;
1214
use Nuxtifyts\PhpDto\Exceptions\UnsupportedTypeException;
1315
use Nuxtifyts\PhpDto\Serializers\ScalarTypeSerializer;
1416
use Nuxtifyts\PhpDto\Tests\Dummies\ComputedPropertiesData;
@@ -32,13 +34,15 @@
3234
#[CoversClass(Computed::class)]
3335
#[CoversClass(WithRefiner::class)]
3436
#[CoversClass(UnsupportedTypeException::class)]
37+
#[CoversClass(UnknownTypeException::class)]
3538
#[UsesClass(ComputedPropertiesData::class)]
3639
#[UsesClass(ScalarTypeSerializer::class)]
3740
#[UsesClass(PersonData::class)]
3841
#[UsesClass(Data::class)]
3942
#[UsesClass(CoordinatesData::class)]
4043
#[UsesClass(UnionMultipleTypeData::class)]
4144
#[UsesClass(PersonData::class)]
45+
#[UsesClass(HasSerializersDummyClass::class)]
4246
final class PropertyContextTest extends UnitCase
4347
{
4448
/**
@@ -99,6 +103,26 @@ public function __construct(
99103
PropertyContext::getInstance($reflectionProperty);
100104
}
101105

106+
/**
107+
* @throws Throwable
108+
*/
109+
#[Test]
110+
public function will_throw_and_exception_if_it_fails_to_resolve_a_serializer(): void
111+
{
112+
$object = new readonly class (1) extends Data {
113+
public function __construct(
114+
public int $value
115+
) {
116+
}
117+
};
118+
119+
$reflectionProperty = new ReflectionProperty($object::class, 'value');
120+
$propertyContext = PropertyContext::getInstance($reflectionProperty);
121+
122+
self::expectException(UnknownTypeException::class);
123+
HasSerializersDummyClass::testGetSerializersFromPropertyContext($propertyContext);
124+
}
125+
102126
/**
103127
* @throws Throwable
104128
*/

0 commit comments

Comments
 (0)