Test Failed
Branch release/v1.0.0 (d4539f)
by Anatoly
11:43 queued 01:17
created

testInvalidValueExceptionForOneToOneProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Sunrise\Hydrator\Tests;
4
5
/**
6
 * Import classes
7
 */
8
use PHPUnit\Framework\TestCase;
9
use Sunrise\Hydrator\Exception;
10
use Sunrise\Hydrator\Hydrator;
11
use Sunrise\Hydrator\HydratorInterface;
12
use DateTimeInterface;
13
14
/**
15
 * HydratorTest
16
 */
17
class HydratorTest extends TestCase
18
{
19
20
    /**
21
     * @return void
22
     */
23
    public function testContracts() : void
24
    {
25
        $hydrator = new Hydrator();
26
27
        $this->assertInstanceOf(HydratorInterface::class, $hydrator);
28
    }
29
30
    /**
31
     * @return void
32
     */
33
    public function testHydrate() : void
34
    {
35
        $data = [
36
            'static' => 'foo',
37
            'nullable' => null,
38
            'bool' => false,
39
            'int' => 0,
40
            'float' => 0.0,
41
            'string' => 'foo',
42
            'array' => [
43
                'foo',
44
                'bar',
45
                100,
46
            ],
47
            'dateTime' => '2005-08-15T15:52:01.000+00:00',
48
            'barDto' => [
49
                'value' => 'foo',
50
            ],
51
            'barDtoCollection' => [
52
                [
53
                    'value' => 'foo',
54
                ],
55
                [
56
                    'value' => 'bar',
57
                ],
58
            ],
59
            'alias' => 'value',
60
        ];
61
62
        $object = new Fixture\FooDto();
63
        $hydrator = new Hydrator();
64
        $hydrator->hydrate($object, $data);
65
66
        $this->assertSame('default value', $object::$static);
67
        $this->assertSame('default value', $object->valuable);
68
        $this->assertSame($data['nullable'], $object->nullable);
69
        $this->assertSame($data['bool'], $object->bool);
70
        $this->assertSame($data['int'], $object->int);
71
        $this->assertSame($data['float'], $object->float);
72
        $this->assertSame($data['string'], $object->string);
73
        $this->assertSame($data['array'], $object->array->getArrayCopy());
74
        $this->assertSame($data['dateTime'], $object->dateTime->format(DateTimeInterface::RFC3339_EXTENDED));
75
        $this->assertSame($data['barDto']['value'], $object->barDto->value);
76
        $this->assertSame($data['barDtoCollection'][0]['value'], $object->barDtoCollection->getIterator()[0]->value);
77
        $this->assertSame($data['barDtoCollection'][1]['value'], $object->barDtoCollection->getIterator()[1]->value);
78
        $this->assertSame($data['alias'], $object->hidden);
79
    }
80
81
    /**
82
     * @return void
83
     */
84
    public function testMissingRequiredValueException() : void
85
    {
86
        $object = new Fixture\BarDto();
87
        $hydrator = new Hydrator();
88
89
        $this->expectException(Exception\MissingRequiredValueException::class);
90
        $this->expectExceptionMessage('The <BarDto.value> property is required.');
91
92
        $hydrator->hydrate($object, [
93
        ]);
94
    }
95
96
    /**
97
     * @return void
98
     */
99
    public function testUntypedObjectPropertyException() : void
100
    {
101
        $object = new Fixture\WithUntypedPropertyDto();
102
        $hydrator = new Hydrator();
103
104
        $this->expectException(Exception\UntypedObjectPropertyException::class);
105
        $this->expectExceptionMessage('The <WithUntypedPropertyDto.value> property is not typed.');
106
107
        $hydrator->hydrate($object, [
108
            'value' => 'foo',
109
        ]);
110
    }
111
112
    /**
113
     * @return void
114
     */
115
    public function testUnsupportedObjectPropertyTypeException() : void
116
    {
117
        $object = new Fixture\WithUnsupportedPropertyTypeDto();
118
        $hydrator = new Hydrator();
119
120
        $this->expectException(Exception\UnsupportedObjectPropertyTypeException::class);
121
        $this->expectExceptionMessage('The <WithUnsupportedPropertyTypeDto.value> property ' .
122
                                      'contains the <Unknown> unhydrable type.');
123
124
        $hydrator->hydrate($object, [
125
            'value' => 'foo',
126
        ]);
127
    }
128
129
    /**
130
     * @return void
131
     */
132
    public function testInvalidValueExceptionForNonNullableProperty() : void
133
    {
134
        $object = new Fixture\BazDto();
135
        $hydrator = new Hydrator();
136
137
        $this->expectException(Exception\InvalidValueException::class);
138
        $this->expectExceptionMessage('The <BazDto.nonNullable> property does not support null.');
139
140
        $hydrator->hydrate($object, [
141
            'nonNullable' => null,
142
        ]);
143
    }
144
145
    /**
146
     * @param mixed $nonScalarValue
147
     *
148
     * @return void
149
     *
150
     * @dataProvider nonScalarDataProvider
151
     */
152
    public function testInvalidValueExceptionForScalarProperty($nonScalarValue) : void
153
    {
154
        $object = new Fixture\BazDto();
155
        $hydrator = new Hydrator();
156
157
        $this->expectException(Exception\InvalidValueException::class);
158
        $this->expectExceptionMessage('The <BazDto.scalar> property only accepts a scalar value.');
159
160
        $hydrator->hydrate($object, [
161
            'scalar' => $nonScalarValue,
162
        ]);
163
    }
164
165
    /**
166
     * @param mixed $nonArrayValue
167
     *
168
     * @return void
169
     *
170
     * @dataProvider nonArrayDataProvider
171
     */
172
    public function testInvalidValueExceptionForArrayProperty($nonArrayValue) : void
173
    {
174
        $object = new Fixture\BazDto();
175
        $hydrator = new Hydrator();
176
177
        $this->expectException(Exception\InvalidValueException::class);
178
        $this->expectExceptionMessage('The <BazDto.array> property only accepts an array.');
179
180
        $hydrator->hydrate($object, [
181
            'array' => $nonArrayValue,
182
        ]);
183
    }
184
185
    /**
186
     * @param mixed $nonDateTimeValue
187
     *
188
     * @return void
189
     *
190
     * @dataProvider nonDateTimeDataProvider
191
     */
192
    public function testInvalidValueExceptionForDateTimeProperty($nonDateTimeValue) : void
193
    {
194
        $object = new Fixture\BazDto();
195
        $hydrator = new Hydrator();
196
197
        $this->expectException(Exception\InvalidValueException::class);
198
        $this->expectExceptionMessage('The <BazDto.dateTime> property only accepts a valid date-time string.');
199
200
        $hydrator->hydrate($object, [
201
            'dateTime' => $nonDateTimeValue,
202
        ]);
203
    }
204
205
    /**
206
     * @param mixed $nonArrayValue
207
     *
208
     * @return void
209
     *
210
     * @dataProvider nonArrayDataProvider
211
     */
212
    public function testInvalidValueExceptionForOneToOneProperty($nonArrayValue) : void
213
    {
214
        $object = new Fixture\BazDto();
215
        $hydrator = new Hydrator();
216
217
        $this->expectException(Exception\InvalidValueException::class);
218
        $this->expectExceptionMessage('The <BazDto.oneToOne> property only accepts an array.');
219
220
        $hydrator->hydrate($object, [
221
            'oneToOne' => $nonArrayValue,
222
        ]);
223
    }
224
225
    /**
226
     * @param mixed $nonArrayValue
227
     *
228
     * @return void
229
     *
230
     * @dataProvider nonArrayDataProvider
231
     */
232
    public function testInvalidValueExceptionForOneToManyProperty($nonArrayValue) : void
233
    {
234
        $object = new Fixture\BazDto();
235
        $hydrator = new Hydrator();
236
237
        $this->expectException(Exception\InvalidValueException::class);
238
        $this->expectExceptionMessage('The <BazDto.oneToMany> property only accepts an array.');
239
240
        $hydrator->hydrate($object, [
241
            'oneToMany' => $nonArrayValue,
242
        ]);
243
    }
244
245
    /**
246
     * @return array<array>
247
     */
248
    public function nonScalarDataProvider() : array
249
    {
250
        return [
251
            [[]],
252
            [new \stdClass],
253
            [function () {
254
            }],
255
            [\STDOUT],
256
        ];
257
    }
258
259
    /**
260
     * @return array<array>
261
     */
262
    public function nonArrayDataProvider() : array
263
    {
264
        return [
265
            [true],
266
            [1],
267
            [1.1],
268
            [''],
269
            [new \stdClass],
270
            [function () {
271
            }],
272
            [\STDOUT],
273
        ];
274
    }
275
276
    /**
277
     * @return array<array>
278
     */
279
    public function nonDateTimeDataProvider() : array
280
    {
281
        return [
282
            [true],
283
            [1],
284
            [1.1],
285
            [''],
286
            ['0'],
287
            ['non-date-time-string'],
288
            [new \stdClass],
289
            [function () {
290
            }],
291
            [\STDOUT],
292
        ];
293
    }
294
}
295