GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 48aa34...e9e817 )
by Mario
29:19 queued 01:50
created

testBuildingWithSenderRecipientAndSubjectFromContent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 96
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 96
rs 8.3859
cc 1
eloc 65
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Netgen\Bundle\InformationCollectionBundle\Tests\Factory;
4
5
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
6
use eZ\Publish\API\Repository\Values\Content\Field;
7
use eZ\Publish\Core\FieldType\EmailAddress\Value as EmailValue;
8
use eZ\Publish\Core\FieldType\TextLine\Value as TextLineValue;
9
use eZ\Publish\Core\Helper\FieldHelper;
10
use eZ\Publish\Core\Helper\TranslationHelper;
11
use eZ\Publish\Core\Repository\ContentService;
12
use eZ\Publish\Core\Repository\Values\Content\Content;
13
use eZ\Publish\Core\Repository\Values\Content\Location;
14
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
15
use eZ\Publish\Core\Repository\Values\ContentType\ContentType;
16
use Netgen\Bundle\EzFormsBundle\Form\DataWrapper;
17
use Netgen\Bundle\EzFormsBundle\Form\Payload\InformationCollectionStruct;
18
use Netgen\Bundle\InformationCollectionBundle\Event\InformationCollected;
19
use Netgen\Bundle\InformationCollectionBundle\Factory\EmailDataFactory;
20
use Netgen\Bundle\InformationCollectionBundle\Value\EmailData;
21
use PHPUnit\Framework\TestCase;
22
use Twig_Environment;
23
use Twig_Loader_Array;
24
use Twig_TemplateWrapper;
25
26
class EmailDataFactoryTest extends TestCase
27
{
28
    /**
29
     * @var \Netgen\Bundle\InformationCollectionBundle\Factory\EmailDataFactory
30
     */
31
    protected $factory;
32
33
    /**
34
     * @var array
35
     */
36
    protected $config;
37
38
    /**
39
     * @var \PHPUnit_Framework_MockObject_MockObject
40
     */
41
    protected $translationHelper;
42
43
    /**
44
     * @var \PHPUnit_Framework_MockObject_MockObject
45
     */
46
    protected $fieldHelper;
47
48
    /**
49
     * @var \PHPUnit_Framework_MockObject_MockObject
50
     */
51
    protected $contentService;
52
53
    /**
54
     * @var \eZ\Publish\Core\Repository\Values\ContentType\ContentType
55
     */
56
    protected $contentType;
57
58
    /**
59
     * @var \PHPUnit_Framework_MockObject_MockObject
60
     */
61
    protected $twig;
62
63
    /**
64
     * @var \PHPUnit_Framework_MockObject_MockObject
65
     */
66
    protected $templateWrapper;
67
68
    /**
69
     * @var \eZ\Publish\Core\Repository\Values\ContentType\ContentType
70
     */
71
    protected $contentType2;
72
73
    /**
74
     * @var \eZ\Publish\Core\Repository\Values\Content\VersionInfo
75
     */
76
    protected $versionInfo;
77
78
    public function setUp()
79
    {
80
        $this->config = array(
81
            'templates' => array(
82
                'default' => 'AcmeBundle::email.html.twig',
83
                'content_types' => array(
84
                    'test_content_type' => 'AcmeBundle::test_content_type.html.twig',
85
                ),
86
            ),
87
            'default_variables' => array(
88
                'sender' => '[email protected]',
89
                'recipient' => '[email protected]',
90
                'subject' => 'subject',
91
            ),
92
        );
93
94
        $this->translationHelper = $this->getMockBuilder(TranslationHelper::class)
95
            ->disableOriginalConstructor()
96
            ->setMethods(array('getTranslatedField'))
97
            ->getMock();
98
99
        $this->fieldHelper = $this->getMockBuilder(FieldHelper::class)
100
            ->disableOriginalConstructor()
101
            ->setMethods(array('isFieldEmpty'))
102
            ->getMock();
103
104
        $this->contentService = $this->getMockBuilder(ContentService::class)
105
            ->disableOriginalConstructor()
106
            ->setMethods(array('loadContent'))
107
            ->getMock();
108
109
        $this->twig = $this->getMockBuilder(\Twig_Environment::class)
110
            ->disableOriginalConstructor()
111
            ->setMethods(array('load'))
112
            ->getMock();
113
114
        $this->contentType = new ContentType(array(
115
            'identifier' => 'test_content_type',
116
            'fieldDefinitions' => array(),
117
        ));
118
119
        $this->contentType2 = new ContentType(array(
120
            'identifier' => 'test_content_type2',
121
            'fieldDefinitions' => array(),
122
        ));
123
124
        $this->versionInfo = new VersionInfo(array(
125
            'contentInfo' => new ContentInfo(array(
126
                'contentTypeId' => 123,
127
            )),
128
        ));
129
130
        $this->factory = new EmailDataFactory(
131
            $this->config,
132
            $this->translationHelper,
133
            $this->fieldHelper,
134
            $this->contentService,
135
            $this->twig
136
        );
137
        parent::setUp();
138
    }
139
140
    public function testBuildingWithSenderRecipientAndSubjectFromContent()
141
    {
142
        $twig = new Twig_Environment(
143
            new Twig_Loader_Array(
144
                array(
145
                    'index' => '{% block email %}{{ "email body" }}{% endblock %}',
146
                )
147
            )
148
        );
149
150
        $templateWrapper = new Twig_TemplateWrapper($twig, $twig->loadTemplate('index'));
151
152
        $this->factory = new EmailDataFactory(
153
            $this->config,
154
            $this->translationHelper,
155
            $this->fieldHelper,
156
            $this->contentService,
157
            $this->twig
158
        );
159
160
        $recipientField = new Field(array(
161
            'value' => new EmailValue('[email protected]'),
162
            'languageCode' => 'eng_GB',
163
            'fieldDefIdentifier' => 'recipient',
164
        ));
165
166
        $senderField = new Field(array(
167
            'value' => new EmailValue('[email protected]'),
168
            'languageCode' => 'eng_GB',
169
            'fieldDefIdentifier' => 'sender',
170
        ));
171
172
        $subjectField = new Field(array(
173
            'value' => new TextLineValue('subject test'),
174
            'languageCode' => 'eng_GB',
175
            'fieldDefIdentifier' => 'subject',
176
        ));
177
178
        $content = new Content(array(
179
            'internalFields' => array(
180
                $recipientField, $senderField, $subjectField,
181
            ),
182
            'versionInfo' => $this->versionInfo,
183
        ));
184
185
        $this->fieldHelper->expects($this->exactly(3))
186
            ->method('isFieldEmpty')
187
            ->withAnyParameters()
188
            ->willReturn(false);
189
190
        $this->translationHelper->expects($this->at(0))
191
            ->method('getTranslatedField')
192
            ->with($content, 'recipient')
193
            ->willReturn($recipientField);
194
195
        $this->translationHelper->expects($this->at(1))
196
            ->method('getTranslatedField')
197
            ->with($content, 'sender')
198
            ->willReturn($senderField);
199
200
        $this->translationHelper->expects($this->at(2))
201
            ->method('getTranslatedField')
202
            ->with($content, 'subject')
203
            ->willReturn($subjectField);
204
205
        $this->contentService->expects($this->once())
206
            ->method('loadContent')
207
            ->with(123)
208
            ->willReturn($content);
209
210
        $location = new Location(
211
            array(
212
                'id' => 12345,
213
                'contentInfo' => new ContentInfo(array('id' => 123)),
214
            )
215
        );
216
217
        $contentType = new ContentType(array(
218
            'identifier' => 'test',
219
            'fieldDefinitions' => array(),
220
        ));
221
222
        $event = new InformationCollected(new DataWrapper(new InformationCollectionStruct(), $contentType, $location));
223
224
        $this->twig->expects($this->once())
225
            ->method('load')
226
            ->willReturn($templateWrapper);
227
228
        $value = $this->factory->build($event);
229
230
        $this->assertInstanceOf(EmailData::class, $value);
231
        $this->assertEquals('[email protected]', $value->getRecipient());
232
        $this->assertEquals('[email protected]', $value->getSender());
233
        $this->assertEquals('subject test', $value->getSubject());
234
        $this->assertEquals('email body', $value->getBody());
235
    }
236
237
    public function testBuildingWithSenderRecipientAndSubjectFromTemplate()
238
    {
239
        $template = <<<TEMPLATE
240
            {% block email %}{{ 'My email body' }}{% endblock %}
241
            {% block subject %}{{ 'My custom subject' }}{% endblock %}
242
            {% block recipient %}{{ '[email protected]' }}{% endblock %}
243
            {% block sender %}{{ '[email protected]' }}{% endblock %}
244
TEMPLATE;
245
246
        $twig = new Twig_Environment(
247
            new Twig_Loader_Array(
248
                array(
249
                    'index' => $template,
250
                )
251
            )
252
        );
253
254
        $templateWrapper = new Twig_TemplateWrapper($twig, $twig->loadTemplate('index'));
255
256
        $this->factory = new EmailDataFactory(
257
            $this->config,
258
            $this->translationHelper,
259
            $this->fieldHelper,
260
            $this->contentService,
261
            $this->twig
262
        );
263
264
        $recipientField = new Field(array(
265
            'value' => new EmailValue('[email protected]'),
266
            'languageCode' => 'eng_GB',
267
            'fieldDefIdentifier' => 'recipient',
268
        ));
269
270
        $senderField = new Field(array(
271
            'value' => new EmailValue('[email protected]'),
272
            'languageCode' => 'eng_GB',
273
            'fieldDefIdentifier' => 'sender',
274
        ));
275
276
        $subjectField = new Field(array(
277
            'value' => new TextLineValue('subject test'),
278
            'languageCode' => 'eng_GB',
279
            'fieldDefIdentifier' => 'subject',
280
        ));
281
282
        $content = new Content(array(
283
            'internalFields' => array(
284
                $recipientField, $senderField, $subjectField,
285
            ),
286
            'versionInfo' => $this->versionInfo,
287
        ));
288
289
        $this->fieldHelper->expects($this->never())
290
            ->method('isFieldEmpty');
291
292
        $this->translationHelper->expects($this->never())
293
            ->method('getTranslatedField');
294
295
        $this->contentService->expects($this->once())
296
            ->method('loadContent')
297
            ->with(123)
298
            ->willReturn($content);
299
300
        $location = new Location(
301
            array(
302
                'id' => 12345,
303
                'contentInfo' => new ContentInfo(array('id' => 123)),
304
            )
305
        );
306
307
        $contentType = new ContentType(array(
308
            'identifier' => 'test_content_type',
309
            'fieldDefinitions' => array(),
310
        ));
311
312
        $event = new InformationCollected(new DataWrapper(new InformationCollectionStruct(), $contentType, $location));
313
314
        $this->twig->expects($this->once())
315
            ->method('load')
316
            ->willReturn($templateWrapper);
317
318
        $value = $this->factory->build($event);
319
320
        $this->assertInstanceOf(EmailData::class, $value);
321
        $this->assertEquals('[email protected]', $value->getRecipient());
322
        $this->assertEquals('[email protected]', $value->getSender());
323
        $this->assertEquals('My custom subject', $value->getSubject());
324
        $this->assertEquals('My email body', $value->getBody());
325
    }
326
327
    /**
328
     * @expectedException \Netgen\Bundle\InformationCollectionBundle\Exception\MissingEmailBlockException
329
     * @expectedExceptionMessage Missing email block in index template, currently there is foo available.
330
     */
331
    public function testBuildingWithNoEmailBlockInTemplate()
332
    {
333
        $twig = new Twig_Environment(
334
            new Twig_Loader_Array(
335
                array(
336
                    'index' => '{% block foo %}{% endblock %}',
337
                )
338
            )
339
        );
340
341
        $templateWrapper = new Twig_TemplateWrapper($twig, $twig->loadTemplate('index'));
342
343
        $recipientField = new Field(array(
344
            'value' => new EmailValue('[email protected]'),
345
            'languageCode' => 'eng_GB',
346
            'fieldDefIdentifier' => 'recipient',
347
        ));
348
349
        $senderField = new Field(array(
350
            'value' => new EmailValue('[email protected]'),
351
            'languageCode' => 'eng_GB',
352
            'fieldDefIdentifier' => 'sender',
353
        ));
354
355
        $subjectField = new Field(array(
356
            'value' => new TextLineValue('subject test'),
357
            'languageCode' => 'eng_GB',
358
            'fieldDefIdentifier' => 'subject',
359
        ));
360
361
        $content = new Content(array(
362
            'internalFields' => array(
363
                $recipientField, $senderField, $subjectField,
364
            ),
365
            'versionInfo' => $this->versionInfo,
366
        ));
367
368
        $this->fieldHelper->expects($this->never())
369
            ->method('isFieldEmpty');
370
371
        $this->translationHelper->expects($this->never())
372
            ->method('getTranslatedField');
373
374
        $this->contentService->expects($this->once())
375
            ->method('loadContent')
376
            ->with(123)
377
            ->willReturn($content);
378
379
        $location = new Location(
380
            array(
381
                'id' => 12345,
382
                'contentInfo' => new ContentInfo(array('id' => 123)),
383
            )
384
        );
385
386
        $contentType = new ContentType(array(
387
            'identifier' => 'test',
388
            'fieldDefinitions' => array(),
389
        ));
390
391
        $event = new InformationCollected(new DataWrapper(new InformationCollectionStruct(), $contentType, $location));
392
393
        $this->twig->expects($this->once())
394
            ->method('load')
395
            ->willReturn($templateWrapper);
396
397
        $value = $this->factory->build($event);
398
399
        $this->assertInstanceOf(EmailData::class, $value);
400
        $this->assertEquals('[email protected]', $value->getRecipient());
401
        $this->assertEquals('[email protected]', $value->getSender());
402
        $this->assertEquals('subject test', $value->getSubject());
403
        $this->assertEquals('body test', $value->getBody());
404
    }
405
406
    public function testBuildingWithSenderRecipientAndSubjectFromConfiguration()
407
    {
408
        $twig = new Twig_Environment(
409
            new Twig_Loader_Array(
410
                array(
411
                    'index' => '{% block email %}{% endblock %}',
412
                )
413
            )
414
        );
415
416
        $templateWrapper = new Twig_TemplateWrapper($twig, $twig->loadTemplate('index'));
417
418
        $recipientField = new Field(array(
419
            'value' => new EmailValue('[email protected]'),
420
            'languageCode' => 'eng_GB',
421
            'fieldDefIdentifier' => 'recipient',
422
        ));
423
424
        $senderField = new Field(array(
425
            'value' => new EmailValue('[email protected]'),
426
            'languageCode' => 'eng_GB',
427
            'fieldDefIdentifier' => 'sender',
428
        ));
429
430
        $subjectField = new Field(array(
431
            'value' => new TextLineValue('subject test'),
432
            'languageCode' => 'eng_GB',
433
            'fieldDefIdentifier' => 'subject',
434
        ));
435
436
        $content = new Content(array(
437
            'internalFields' => array(
438
                $recipientField, $senderField, $subjectField,
439
            ),
440
            'versionInfo' => $this->versionInfo,
441
        ));
442
443
        $this->fieldHelper->expects($this->exactly(3))
444
            ->method('isFieldEmpty')
445
            ->withAnyParameters()
446
            ->willReturn(true);
447
448
        $this->translationHelper->expects($this->never())
449
            ->method('getTranslatedField')
450
            ->with($content, 'recipient');
451
452
        $this->translationHelper->expects($this->never())
453
            ->method('getTranslatedField')
454
            ->with($content, 'sender');
455
456
        $this->translationHelper->expects($this->never())
457
            ->method('getTranslatedField')
458
            ->with($content, 'subject');
459
460
        $this->contentService->expects($this->once())
461
            ->method('loadContent')
462
            ->with(123)
463
            ->willReturn($content);
464
465
        $location = new Location(
466
            array(
467
                'id' => 12345,
468
                'contentInfo' => new ContentInfo(array('id' => 123)),
469
            )
470
        );
471
472
        $contentType = new ContentType(array(
473
            'identifier' => 'test',
474
            'fieldDefinitions' => array(),
475
        ));
476
477
        $event = new InformationCollected(new DataWrapper(new InformationCollectionStruct(), $contentType, $location));
478
479
        $this->twig->expects($this->once())
480
            ->method('load')
481
            ->willReturn($templateWrapper);
482
483
        $value = $this->factory->build($event);
484
485
        $this->assertInstanceOf(EmailData::class, $value);
486
        $this->assertEquals($this->config['default_variables']['recipient'], $value->getRecipient());
487
        $this->assertEquals($this->config['default_variables']['sender'], $value->getSender());
488
        $this->assertEquals($this->config['default_variables']['subject'], $value->getSubject());
489
    }
490
}
491