testMultipartHydratorChoiceEmptyOccurrence()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * apparat-resource
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Resource
8
 * @subpackage  Apparat\Resource\Tests
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation Fixture (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Resource\Tests;
38
39
use Apparat\Dev\Tests\AbstractTest;
40
use Apparat\Kernel\Ports\Kernel;
41
use Apparat\Resource\Domain\Factory\HydratorFactory;
42
use Apparat\Resource\Domain\Model\Hydrator\HydratorInterface;
43
use Apparat\Resource\Domain\Model\Hydrator\InvalidArgumentException;
44
use Apparat\Resource\Domain\Model\Hydrator\RuntimeException;
45
use Apparat\Resource\Domain\Model\Part\OutOfBoundsException;
46
use Apparat\Resource\Infrastructure\Model\Hydrator\TextHydrator;
47
use Apparat\Resource\Infrastructure\Model\Part\TextPart;
48
49
/**
50
 * Hydrator tests
51
 *
52
 * @package     Apparat\Resource
53
 * @subpackage  Apparat\Resource\Tests
54
 */
55
class MultipartHydratorTest extends AbstractTest
56
{
57
58
    /**
59
     * Tears down the fixture
60
     */
61
    public function tearDown()
62
    {
63
        putenv('MOCK_VALIDATE_PARAMETERS');
64
        putenv('MOCK_OCCURRENCE_DEHYDRATION');
65
        putenv('MOCK_AGGREGATE_CLASS');
66
        putenv('MOCK_EMPTY_OCCURRENCE');
67
        putenv('MOCK_SUBHYDRATOR_NAME');
68
        putenv('MOCK_PART_INSTANCE');
69
        putenv('MOCK_OCCURRENCE_NUMBER');
70
        putenv('MOCK_ASSIGNMENT_PART_IDENTIFIER');
71
        putenv('MOCK_EMPTY_OCCURRENCE');
72
        putenv('MOCK_SUBHYDRATOR_NAME');
73
        putenv('MOCK_PART_INSTANCE');
74
        parent::tearDown();
75
    }
76
77
    /**
78
     * Test a missing multipart hydrator
79
     *
80
     * @expectedException InvalidArgumentException
81
     * @expectedExceptionCode 1447107537
82
     */
83
    public function testMissingMultipartHydrator()
84
    {
85
        HydratorFactory::build([[1, 2]]);
86
    }
87
88
    /**
89
     * Test an empty multipart hydrator class
90
     *
91
     * @expectedException InvalidArgumentException
92
     * @expectedExceptionCode 1447107792
93
     */
94
    public function testEmptyMultipartHydratorClass()
95
    {
96
        HydratorFactory::build([[1, 2], '']);
97
    }
98
99
    /**
100
     * Test an invalid multipart hydrator class
101
     *
102
     * @expectedException InvalidArgumentException
103
     * @expectedExceptionCode 1447107792
104
     */
105
    public function testInvalidMultipartHydratorClass()
106
    {
107
        HydratorFactory::build([[1, 2], \stdClass::class]);
108
    }
109
110
    /**
111
     * Test invalid multipart hydrator parameters
112
     *
113
     * @expectedException InvalidArgumentException
114
     * @expectedExceptionCode 1447109790
115
     */
116
    public function testInvalidMultipartHydratorParameters()
117
    {
118
        putenv('MOCK_VALIDATE_PARAMETERS=1');
119
        HydratorFactory::build([[1, 2], AbstractSequenceHydrator::class, true]);
120
    }
121
122
    /**
123
     * Test too few multipart hydrator parameters
124
     *
125
     * @expectedException InvalidArgumentException
126
     * @expectedExceptionCode 1447866302
127
     */
128
    public function testTooFewMultipartHydratorParameters()
129
    {
130
        HydratorFactory::build([[1, 2], AbstractSequenceHydrator::class, 0]);
131
    }
132
133
    /**
134
     * Test invalid multipart hydrator occurrences minimum
135
     *
136
     * @expectedException \Apparat\Resource\Domain\Model\Part\InvalidArgumentException
137
     * @expectedExceptionCode 1447021191
138
     */
139
    public function testInvalidMultipartHydratorMinimumOccurrences()
140
    {
141
        HydratorFactory::build([[1, 2], AbstractSequenceHydrator::class, 0, 1]);
142
    }
143
144
    /**
145
     * Test invalid multipart hydrator occurrences maximum
146
     *
147
     * @expectedException \Apparat\Resource\Domain\Model\Part\InvalidArgumentException
148
     * @expectedExceptionCode 1447021211
149
     */
150
    public function testInvalidMultipartHydratorMaximumOccurrences()
151
    {
152
        HydratorFactory::build([[1, 2], AbstractSequenceHydrator::class, 2, 1]);
153
    }
154
155
    /**
156
     * Test an invalid multipart hydrator class
157
     *
158
     * @expectedException InvalidArgumentException
159
     * @expectedExceptionCode 1447868909
160
     */
161
    public function testInvalidMultipartSubhydratorClass()
162
    {
163
        HydratorFactory::build([[\stdClass::class, \stdClass::class], AbstractSequenceHydrator::class, 1, 1]);
164
    }
165
166
    /**
167
     * Test multipart hydrator
168
     */
169
    public function testMultipartHydrator()
170
    {
171
        /** @var AbstractSequenceHydrator $sequenceHydrator */
172
        $sequenceHydrator = HydratorFactory::build(
173
            [
174
                [TextHydrator::class, TextHydrator::class],
175
                AbstractSequenceHydrator::class,
176
                1,
177
                1
178
            ]
179
        );
180
        $this->assertInstanceOf(AbstractSequenceHydrator::class, $sequenceHydrator);
181
    }
182
183
    /**
184
     * Test multipart hydrator name
185
     */
186
    public function testMultipartHydratorName()
187
    {
188
        /** @var AbstractSequenceHydrator $sequenceHydrator */
189
        $sequenceHydrator = HydratorFactory::build(
190
            [
191
                [TextHydrator::class, TextHydrator::class],
192
                AbstractSequenceHydrator::class,
193
                1,
194
                1
195
            ]
196
        );
197
        $this->assertEquals(HydratorInterface::STANDARD, $sequenceHydrator->getName());
198
    }
199
200
    /**
201
     * Test multipart hydrator dehydration with an invalid part
202
     *
203
     * @expectedException InvalidArgumentException
204
     * @expectedExceptionCode 1448107001
205
     */
206
    public function testMultipartHydratorDehydrationOfInvalidPart()
207
    {
208
        /** @var TextPart $textPart */
209
        $textPart = Kernel::create(TextPart::class, [Kernel::create(TextHydrator::class, ['name']), '']);
210
211
        /** @var AbstractSequenceHydrator $sequenceHydrator */
212
        $sequenceHydrator = HydratorFactory::build(
213
            [
214
                [TextHydrator::class, TextHydrator::class],
215
                AbstractSequenceHydrator::class,
216
                1,
217
                1
218
            ]
219
        );
220
221
        $sequenceHydrator->dehydrate($textPart);
222
    }
223
224
    /**
225
     * Test multipart hydrator dehydration with an invalid part
226
     *
227
     * @expectedException RuntimeException
228
     * @expectedExceptionCode 1448112964
229
     */
230
    public function testMultipartHydratorDehydrationWithInvalidOccurrenceDehydration()
231
    {
232
        /** @var AbstractSequenceHydrator $sequenceHydrator */
233
        $sequenceHydrator = HydratorFactory::build(
234
            [
235
                [TextHydrator::class, TextHydrator::class],
236
                AbstractSequenceHydrator::class,
237
                1,
238
                1
239
            ]
240
        );
241
        $sequence = $sequenceHydrator->hydrate('one|two');
242
243
        putenv('MOCK_OCCURRENCE_DEHYDRATION=1');
244
        $sequenceHydrator->dehydrate($sequence);
245
    }
246
247
    /**
248
     * Test sequence hydration with invalid aggregate class
249
     *
250
     * @expectedException RuntimeException
251
     * @expectedExceptionCode 1447887703
252
     */
253
    public function testMultipartHydratorInvalidAggregateClass()
254
    {
255
        /** @var AbstractSequenceHydrator $sequenceHydrator */
256
        $sequenceHydrator = HydratorFactory::build(
257
            [
258
                [TextHydrator::class, TextHydrator::class],
259
                AbstractSequenceHydrator::class,
260
                1,
261
                1
262
            ]
263
        );
264
265
        putenv('MOCK_AGGREGATE_CLASS=1');
266
        $sequenceHydrator->hydrate('one|two');
267
    }
268
269
    /**
270
     * Test sequence dehydration with empty occurrence
271
     *
272
     * @expectedException \Apparat\Resource\Domain\Model\Hydrator\SkippedOccurrenceDehydrationException
273
     * @expectedExceptionCode 1448108316
274
     */
275
    public function testMultipartHydratorSequenceEmptyOccurrence()
276
    {
277
        /** @var AbstractSequenceHydrator $sequenceHydrator */
278
        $sequenceHydrator = HydratorFactory::build(
279
            [
280
                [TextHydrator::class, TextHydrator::class],
281
                AbstractSequenceHydrator::class,
282
                1,
283
                1
284
            ]
285
        );
286
        $sequence = $sequenceHydrator->hydrate('one|two');
287
        putenv('MOCK_EMPTY_OCCURRENCE=1');
288
        $sequenceHydrator->dehydrate($sequence);
289
    }
290
291
    /**
292
     * Test sequence dehydration with unknown subhydrator name
293
     *
294
     * @expectedException \Apparat\Resource\Domain\Model\Hydrator\SkippedOccurrenceDehydrationException
295
     * @expectedExceptionCode 1448108444
296
     */
297
    public function testMultipartHydratorSequenceInvalidSubhydratorName()
298
    {
299
        /** @var AbstractSequenceHydrator $sequenceHydrator */
300
        $sequenceHydrator = HydratorFactory::build(
301
            [
302
                [TextHydrator::class, TextHydrator::class],
303
                AbstractSequenceHydrator::class,
304
                1,
305
                1
306
            ]
307
        );
308
        $sequence = $sequenceHydrator->hydrate('one|two');
309
        putenv('MOCK_SUBHYDRATOR_NAME=1');
310
        $sequenceHydrator->dehydrate($sequence);
311
    }
312
313
    /**
314
     * Test sequence dehydration with invalid part instance
315
     *
316
     * @expectedException \Apparat\Resource\Domain\Model\Hydrator\SkippedOccurrenceDehydrationException
317
     * @expectedExceptionCode 1448108849
318
     */
319
    public function testMultipartHydratorSequenceInvalidPartInstance()
320
    {
321
        /** @var AbstractSequenceHydrator $sequenceHydrator */
322
        $sequenceHydrator = HydratorFactory::build(
323
            [
324
                [TextHydrator::class, TextHydrator::class],
325
                AbstractSequenceHydrator::class,
326
                1,
327
                1
328
            ]
329
        );
330
        $sequence = $sequenceHydrator->hydrate('one|two');
331
        putenv('MOCK_PART_INSTANCE=1');
332
        $sequenceHydrator->dehydrate($sequence);
333
    }
334
335
    /**
336
     * Test sequence part count
337
     */
338
    public function testMultipartHydratorSequenceCount()
339
    {
340
        /** @var AbstractSequenceHydrator $sequenceHydrator */
341
        $sequenceHydrator = HydratorFactory::build(
342
            [
343
                [TextHydrator::class, TextHydrator::class],
344
                AbstractSequenceHydrator::class,
345
                1,
346
                1
347
            ]
348
        );
349
        $this->assertEquals(1, count($sequenceHydrator->hydrate('one|two')));
350
    }
351
352
    /**
353
     * Test invalid occurrences number
354
     *
355
     * @expectedException OutOfBoundsException
356
     * @expectedExceptionCode 1447976806
357
     */
358
    public function testMultipartHydratorSequenceInvalidOccurrenceNumber()
359
    {
360
        /** @var AbstractSequenceHydrator $sequenceHydrator */
361
        $sequenceHydrator = HydratorFactory::build(
362
            [
363
                [TextHydrator::class, TextHydrator::class],
364
                AbstractSequenceHydrator::class,
365
                1,
366
                1
367
            ]
368
        );
369
        putenv('MOCK_OCCURRENCE_NUMBER=1');
370
        $sequenceHydrator->hydrate('one|two');
371
    }
372
373
    /**
374
     * Test sequence serialization
375
     */
376
    public function testMultipartHydratorSequenceSerialization()
377
    {
378
        /** @var AbstractSequenceHydrator $sequenceHydrator */
379
        $sequenceHydrator = HydratorFactory::build(
380
            [
381
                [TextHydrator::class, TextHydrator::class],
382
                AbstractSequenceHydrator::class,
383
                1,
384
                1
385
            ]
386
        );
387
        $sequence = $sequenceHydrator->hydrate('one|two');
388
        $this->assertEquals('onetwo', strval($sequence));
389
    }
390
391
    /**
392
     * Test invalid assignment part identifier
393
     *
394
     * @expectedException InvalidArgumentException
395
     * @expectedExceptionCode 1447364401
396
     */
397
    public function testMultipartHydratorSequenceInvalidAssignmentPartIdentifier()
398
    {
399
        /** @var AbstractSequenceHydrator $sequenceHydrator */
400
        $sequenceHydrator = HydratorFactory::build(
401
            [
402
                [TextHydrator::class, TextHydrator::class],
403
                AbstractSequenceHydrator::class,
404
                1,
405
                1
406
            ]
407
        );
408
        putenv('MOCK_ASSIGNMENT_PART_IDENTIFIER=1');
409
        $sequenceHydrator->hydrate('one|two');
410
    }
411
412
    /**
413
     * Test sequence unknown delegate method
414
     *
415
     * @expectedException \Apparat\Resource\Domain\Model\Part\InvalidArgumentException
416
     * @expectedExceptionCode 1448225222
417
     */
418
    public function testMultipartHydratorSequenceUnknownMethod()
419
    {
420
        /** @var AbstractSequenceHydrator $sequenceHydrator */
421
        $sequenceHydrator = HydratorFactory::build(
422
            [
423
                [TextHydrator::class, TextHydrator::class],
424
                AbstractSequenceHydrator::class,
425
                1,
426
                1
427
            ]
428
        );
429
        $sequence = $sequenceHydrator->hydrate('one|two');
430
        $sequence->delegate('unknownMethod', [], []);
431
    }
432
433
    /**
434
     * Test choice dehydration with empty occurrence
435
     *
436
     * @expectedException \Apparat\Resource\Domain\Model\Hydrator\SkippedOccurrenceDehydrationException
437
     * @expectedExceptionCode 1448108316
438
     */
439
    public function testMultipartHydratorChoiceEmptyOccurrence()
440
    {
441
        /** @var AbstractChoiceHydrator $choiceHydrator */
442
        $choiceHydrator = HydratorFactory::build(
443
            [
444
                [TextHydrator::class, TextHydrator::class],
445
                AbstractChoiceHydrator::class,
446
                1,
447
                1
448
            ]
449
        );
450
        $choice = $choiceHydrator->hydrate('one');
451
        putenv('MOCK_EMPTY_OCCURRENCE=1');
452
        $choiceHydrator->dehydrate($choice);
453
    }
454
455
    /**
456
     * Test choice dehydration with unknown subhydrator name
457
     *
458
     * @expectedException \Apparat\Resource\Domain\Model\Hydrator\SkippedOccurrenceDehydrationException
459
     * @expectedExceptionCode 1448108444
460
     */
461
    public function testMultipartHydratorChoiceInvalidSubhydratorName()
462
    {
463
        /** @var AbstractChoiceHydrator $choiceHydrator */
464
        $choiceHydrator = HydratorFactory::build(
465
            [
466
                [TextHydrator::class, TextHydrator::class],
467
                AbstractChoiceHydrator::class,
468
                1,
469
                1
470
            ]
471
        );
472
        $choice = $choiceHydrator->hydrate('one');
473
        putenv('MOCK_SUBHYDRATOR_NAME=1');
474
        $choiceHydrator->dehydrate($choice);
475
    }
476
477
    /**
478
     * Test choice dehydration with invalid part instance
479
     *
480
     * @expectedException \Apparat\Resource\Domain\Model\Hydrator\SkippedOccurrenceDehydrationException
481
     * @expectedExceptionCode 1448108849
482
     */
483
    public function testMultipartHydratorChoiceInvalidPartInstance()
484
    {
485
        /** @var AbstractChoiceHydrator $choiceHydrator */
486
        $choiceHydrator = HydratorFactory::build(
487
            [
488
                [TextHydrator::class, TextHydrator::class],
489
                AbstractChoiceHydrator::class,
490
                1,
491
                1
492
            ]
493
        );
494
        $choice = $choiceHydrator->hydrate('one');
495
        putenv('MOCK_PART_INSTANCE=1');
496
        $choiceHydrator->dehydrate($choice);
497
    }
498
}
499