testTransformCustomInputValidatorInstance()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 18

Duplication

Lines 30
Ratio 100 %

Importance

Changes 0
Metric Value
dl 30
loc 30
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
namespace AbacaphiliacTest\Zend\Transformer\Factory;
4
5
use Abacaphiliac\Zend\Transformer\Factory\AbstractTransformerFactory;
6
use Abacaphiliac\Zend\Transformer\TransformerInterface;
7
use AbacaphiliacTest\FizBuz;
8
use AbacaphiliacTest\FooBar;
9
use Zend\Hydrator\ClassMethods;
10
use Zend\ServiceManager\ServiceManager;
11
use Zend\Validator\ValidatorChain;
12
13
/**
14
 * @covers \Abacaphiliac\Zend\Transformer\Factory\AbstractTransformerFactory
15
 */
16
class AbstractTransformerFactoryTest extends \PHPUnit_Framework_TestCase
17
{
18
    /** @var AbstractTransformerFactory */
19
    private $sut;
20
    
21
    /** @var ServiceManager */
22
    private $container;
23
    
24
    protected function setUp()
25
    {
26
        $this->container = new ServiceManager();
27
        
28
        $this->sut = $sut = new AbstractTransformerFactory();
0 ignored issues
show
Unused Code introduced by
$sut is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
29
    }
30
    
31 View Code Duplication
    public function testTransformFromSimpleSpec()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        $this->container->setService('config', [
34
            'abacaphiliac/zend-transformer' => [
35
                'transformers' => [
36
                    'FooBarToFizBuz' => [
37
                        'inputClass' => FooBar::class,
38
                        'keyMap' => [
39
                            'foo' => 'fiz',
40
                            'bar' => 'buz',
41
                        ],
42
                        'outputClass' => FizBuz::class,
43
                    ],
44
                ],
45
            ],
46
        ]);
47
    
48
        self::assertTrue($this->sut->canCreateServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz'));
49
        
50
        $transformer = $this->sut->createServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz');
51
        self::assertInstanceOf(TransformerInterface::class, $transformer);
52
        
53
        $result = $transformer->transform(new FooBar('Alice', 'Bob'), new FizBuz());
54
        self::assertInstanceOf(FizBuz::class, $result);
55
        
56
        /** @var FizBuz $result */
57
        self::assertEquals('Alice', $result->getFiz());
58
        self::assertEquals('Bob', $result->getBuz());
59
    }
60
    
61
    /**
62
     * @expectedException \Abacaphiliac\Zend\Transformer\Exception\TransformationException
63
     */
64 View Code Duplication
    public function testNotTransformDueToInvalidInputClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        $this->container->setService('config', [
67
            'abacaphiliac/zend-transformer' => [
68
                'transformers' => [
69
                    'FooBarToFizBuz' => [
70
                        'inputClass' => FizBuz::class,
71
                        'keyMap' => [
72
                            'foo' => 'fiz',
73
                            'bar' => 'buz',
74
                        ],
75
                        'outputClass' => FooBar::class,
76
                    ],
77
                ],
78
            ],
79
        ]);
80
        
81
        self::assertTrue($this->sut->canCreateServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz'));
82
        
83
        $transformer = $this->sut->createServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz');
84
        self::assertInstanceOf(TransformerInterface::class, $transformer);
85
        
86
        $transformer->transform(new FooBar('Alice', 'Bob'), new FizBuz());
87
    }
88
    
89
    /**
90
     * @expectedException \Abacaphiliac\Zend\Transformer\Exception\TransformationException
91
     */
92 View Code Duplication
    public function testNotTransformDueToInvalidOutputClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        $this->container->setService('config', [
95
            'abacaphiliac/zend-transformer' => [
96
                'transformers' => [
97
                    'FooBarToFizBuz' => [
98
                        'inputClass' => FooBar::class,
99
                        'keyMap' => [
100
                            'foo' => 'fiz',
101
                            'bar' => 'buz',
102
                        ],
103
                        'outputClass' => FooBar::class,
104
                    ],
105
                ],
106
            ],
107
        ]);
108
        
109
        self::assertTrue($this->sut->canCreateServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz'));
110
        
111
        $transformer = $this->sut->createServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz');
112
        self::assertInstanceOf(TransformerInterface::class, $transformer);
113
        
114
        $transformer->transform(new FooBar('Alice', 'Bob'), new FizBuz());
115
    }
116
    
117 View Code Duplication
    public function testTransformCustomExtractor()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119
        $this->container->setService('config', [
120
            'abacaphiliac/zend-transformer' => [
121
                'transformers' => [
122
                    'FooBarToFizBuz' => [
123
                        'inputClass' => FooBar::class,
124
                        'extractor' => 'ClassMethods',
125
                        'keyMap' => [
126
                            'foo' => 'fiz',
127
                            'bar' => 'buz',
128
                        ],
129
                        'outputClass' => FizBuz::class,
130
                    ],
131
                ],
132
            ],
133
        ]);
134
        
135
        self::assertTrue($this->sut->canCreateServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz'));
136
        
137
        $transformer = $this->sut->createServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz');
138
        self::assertInstanceOf(TransformerInterface::class, $transformer);
139
        
140
        $result = $transformer->transform(new FooBar('Alice', 'Bob'), new FizBuz());
141
        self::assertInstanceOf(FizBuz::class, $result);
142
        
143
        /** @var FizBuz $result */
144
        self::assertEquals('Alice', $result->getFiz());
145
        self::assertEquals('Bob', $result->getBuz());
146
    }
147
    
148 View Code Duplication
    public function testTransformCustomExtractorInstance()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        $this->container->setService('config', [
151
            'abacaphiliac/zend-transformer' => [
152
                'transformers' => [
153
                    'FooBarToFizBuz' => [
154
                        'inputClass' => FooBar::class,
155
                        'extractor' => new ClassMethods(),
156
                        'keyMap' => [
157
                            'foo' => 'fiz',
158
                            'bar' => 'buz',
159
                        ],
160
                        'outputClass' => FizBuz::class,
161
                    ],
162
                ],
163
            ],
164
        ]);
165
        
166
        self::assertTrue($this->sut->canCreateServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz'));
167
        
168
        $transformer = $this->sut->createServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz');
169
        self::assertInstanceOf(TransformerInterface::class, $transformer);
170
        
171
        $result = $transformer->transform(new FooBar('Alice', 'Bob'), new FizBuz());
172
        self::assertInstanceOf(FizBuz::class, $result);
173
        
174
        /** @var FizBuz $result */
175
        self::assertEquals('Alice', $result->getFiz());
176
        self::assertEquals('Bob', $result->getBuz());
177
    }
178
    
179 View Code Duplication
    public function testTransformCustomHydrator()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
180
    {
181
        $this->container->setService('config', [
182
            'abacaphiliac/zend-transformer' => [
183
                'transformers' => [
184
                    'FooBarToFizBuz' => [
185
                        'inputClass' => FooBar::class,
186
                        'keyMap' => [
187
                            'foo' => 'fiz',
188
                            'bar' => 'buz',
189
                        ],
190
                        'hydrator' => 'ClassMethods',
191
                        'outputClass' => FizBuz::class,
192
                    ],
193
                ],
194
            ],
195
        ]);
196
        
197
        self::assertTrue($this->sut->canCreateServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz'));
198
        
199
        $transformer = $this->sut->createServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz');
200
        self::assertInstanceOf(TransformerInterface::class, $transformer);
201
        
202
        $result = $transformer->transform(new FooBar('Alice', 'Bob'), new FizBuz());
203
        self::assertInstanceOf(FizBuz::class, $result);
204
        
205
        /** @var FizBuz $result */
206
        self::assertEquals('Alice', $result->getFiz());
207
        self::assertEquals('Bob', $result->getBuz());
208
    }
209
    
210 View Code Duplication
    public function testTransformCustomHydratorInstance()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211
    {
212
        $this->container->setService('config', [
213
            'abacaphiliac/zend-transformer' => [
214
                'transformers' => [
215
                    'FooBarToFizBuz' => [
216
                        'inputClass' => FooBar::class,
217
                        'keyMap' => [
218
                            'foo' => 'fiz',
219
                            'bar' => 'buz',
220
                        ],
221
                        'hydrator' => new ClassMethods(),
222
                        'outputClass' => FizBuz::class,
223
                    ],
224
                ],
225
            ],
226
        ]);
227
        
228
        self::assertTrue($this->sut->canCreateServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz'));
229
        
230
        $transformer = $this->sut->createServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz');
231
        self::assertInstanceOf(TransformerInterface::class, $transformer);
232
        
233
        $result = $transformer->transform(new FooBar('Alice', 'Bob'), new FizBuz());
234
        self::assertInstanceOf(FizBuz::class, $result);
235
        
236
        /** @var FizBuz $result */
237
        self::assertEquals('Alice', $result->getFiz());
238
        self::assertEquals('Bob', $result->getBuz());
239
    }
240
    
241 View Code Duplication
    public function testTransformCustomKeyMapCallable()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
242
    {
243
        $this->container->setService('config', [
244
            'abacaphiliac/zend-transformer' => [
245
                'transformers' => [
246
                    'FooBarToFizBuz' => [
247
                        'inputClass' => FooBar::class,
248
                        'transformer' => function (array $data) {
249
                            return [
250
                                'fiz' => $data['foo'],
251
                                'buz' => $data['bar'],
252
                            ];
253
                        },
254
                        'outputClass' => FizBuz::class,
255
                    ],
256
                ],
257
            ],
258
        ]);
259
        
260
        self::assertTrue($this->sut->canCreateServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz'));
261
        
262
        $transformer = $this->sut->createServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz');
263
        self::assertInstanceOf(TransformerInterface::class, $transformer);
264
        
265
        $result = $transformer->transform(new FooBar('Alice', 'Bob'), new FizBuz());
266
        self::assertInstanceOf(FizBuz::class, $result);
267
        
268
        /** @var FizBuz $result */
269
        self::assertEquals('Alice', $result->getFiz());
270
        self::assertEquals('Bob', $result->getBuz());
271
    }
272
    
273 View Code Duplication
    public function testTransformCustomKeyMapService()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
274
    {
275
        $this->container->setService('CustomKeyMapService', function (array $data) {
276
            return [
277
                'fiz' => $data['foo'],
278
                'buz' => $data['bar'],
279
            ];
280
        });
281
        
282
        $this->container->setService('config', [
283
            'abacaphiliac/zend-transformer' => [
284
                'transformers' => [
285
                    'FooBarToFizBuz' => [
286
                        'inputClass' => FooBar::class,
287
                        'transformer' => 'CustomKeyMapService',
288
                        'outputClass' => FizBuz::class,
289
                    ],
290
                ],
291
            ],
292
        ]);
293
        
294
        self::assertTrue($this->sut->canCreateServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz'));
295
        
296
        $transformer = $this->sut->createServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz');
297
        self::assertInstanceOf(TransformerInterface::class, $transformer);
298
        
299
        $result = $transformer->transform(new FooBar('Alice', 'Bob'), new FizBuz());
300
        self::assertInstanceOf(FizBuz::class, $result);
301
        
302
        /** @var FizBuz $result */
303
        self::assertEquals('Alice', $result->getFiz());
304
        self::assertEquals('Bob', $result->getBuz());
305
    }
306
    
307 View Code Duplication
    public function testTransformCustomInputValidatorInstance()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
308
    {
309
        $this->container->setService('config', [
310
            'abacaphiliac/zend-transformer' => [
311
                'transformers' => [
312
                    'FooBarToFizBuz' => [
313
                        'inputClass' => FooBar::class,
314
                        'inputValidator' => new ValidatorChain(),
315
                        'keyMap' => [
316
                            'foo' => 'fiz',
317
                            'bar' => 'buz',
318
                        ],
319
                        'outputClass' => FizBuz::class,
320
                    ],
321
                ],
322
            ],
323
        ]);
324
        
325
        self::assertTrue($this->sut->canCreateServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz'));
326
        
327
        $transformer = $this->sut->createServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz');
328
        self::assertInstanceOf(TransformerInterface::class, $transformer);
329
        
330
        $result = $transformer->transform(new FooBar('Alice', 'Bob'), new FizBuz());
331
        self::assertInstanceOf(FizBuz::class, $result);
332
        
333
        /** @var FizBuz $result */
334
        self::assertEquals('Alice', $result->getFiz());
335
        self::assertEquals('Bob', $result->getBuz());
336
    }
337
    
338 View Code Duplication
    public function testTransformCustomOutputValidatorInstance()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
339
    {
340
        $this->container->setService('config', [
341
            'abacaphiliac/zend-transformer' => [
342
                'transformers' => [
343
                    'FooBarToFizBuz' => [
344
                        'inputClass' => FooBar::class,
345
                        'keyMap' => [
346
                            'foo' => 'fiz',
347
                            'bar' => 'buz',
348
                        ],
349
                        'outputClass' => FizBuz::class,
350
                        'outputValidator' => new ValidatorChain(),
351
                    ],
352
                ],
353
            ],
354
        ]);
355
        
356
        self::assertTrue($this->sut->canCreateServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz'));
357
        
358
        $transformer = $this->sut->createServiceWithName($this->container, 'FooBarToFizBuz', 'FooBarToFizBuz');
359
        self::assertInstanceOf(TransformerInterface::class, $transformer);
360
        
361
        $result = $transformer->transform(new FooBar('Alice', 'Bob'), new FizBuz());
362
        self::assertInstanceOf(FizBuz::class, $result);
363
        
364
        /** @var FizBuz $result */
365
        self::assertEquals('Alice', $result->getFiz());
366
        self::assertEquals('Bob', $result->getBuz());
367
    }
368
}
369