TemplateInputFilterTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 89
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 1
B propertyValidationData() 0 31 1
A testValidationOfEachProperty() 0 17 2
1
<?php
2
/**
3
 * Copyright (c) 2014 Roave, LLC.
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 *   * Redistributions of source code must retain the above copyright
11
 *     notice, this list of conditions and the following disclaimer.
12
 *
13
 *   * Redistributions in binary form must reproduce the above copyright
14
 *     notice, this list of conditions and the following disclaimer in
15
 *     the documentation and/or other materials provided with the
16
 *     distribution.
17
 *
18
 *   * Neither the names of the copyright holders nor the names of the
19
 *     contributors may be used to endorse or promote products derived
20
 *     from this software without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
 * POSSIBILITY OF SUCH DAMAGE.
34
 *
35
 * @author    Antoine Hedgecock
36
 * @author    Jonas Rosenlind
37
 *
38
 * @copyright 2014 Roave, LLC
39
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
40
 */
41
42
namespace EmailTemplatesTest\InputFilter;
43
44
use PHPUnit_Framework_TestCase;
45
use Roave\EmailTemplates\InputFilter\TemplateInputFilter;
46
use Roave\EmailTemplates\Service\Template\Engine\EngineInterface;
47
use Roave\EmailTemplates\Validator\CanRenderValidator;
48
use Zend\Validator\NotEmpty;
49
50
/**
51
 * Class TemplateInputFilterTest
52
 *
53
 * @coversDefaultClass \Roave\EmailTemplates\InputFilter\TemplateInputFilter
54
 * @covers ::<!public>
55
 *
56
 * @group inputFilter
57
 */
58
class TemplateInputFilterTest extends PHPUnit_Framework_TestCase
59
{
60
    /**
61
     * @var TemplateInputFilter
62
     */
63
    protected $inputFilter;
64
65
    /**
66
     * @covers ::init
67
     */
68
    protected function setUp()
69
    {
70
        $canRenderValidator = new CanRenderValidator($this->createMock(EngineInterface::class));
71
        $this->inputFilter = new TemplateInputFilter();
72
73
        $this->inputFilter
74
            ->getFactory()
75
            ->getDefaultValidatorChain()
76
            ->getPluginManager()
77
            ->setService(CanRenderValidator::class, $canRenderValidator);
78
79
        $this->inputFilter->init();
80
    }
81
82
    /**
83
     * Data provider for {@see testValidationOfEachProperty}
84
     *
85
     * @return array
86
     */
87
    public function propertyValidationData()
88
    {
89
        return [
90
            ['updateParameters', true, null, true],                              // Valid
91
            ['updateParameters', false, null, false],                            // Valid
92
            ['updateParameters', 'true', null, true],                            // Valid
93
94
            ['parameters', '', [NotEmpty::IS_EMPTY]],                            // Empty
95
            ['parameters', 'Param', null, 'Param'],                              // Valid
96
97
            ['subject', '', [NotEmpty::IS_EMPTY]],                               // Empty
98
            ['subject', ' ', [NotEmpty::IS_EMPTY]],                              // Empty, asserting StringTrim
99
            ['subject', 'Test Subject', null, 'Test Subject'],                   // Valid
100
            ['subject', ' Test Subject   ', null, 'Test Subject'],               // Valid, asserting StringTrim
101
102
            ['textBody', '', [NotEmpty::IS_EMPTY]],                              // Empty
103
            ['textBody', ' ', [NotEmpty::IS_EMPTY]],                             // Empty, asserting StringTrim
104
            ['textBody', 'Test Body', null, 'Test Body'],                        // Valid
105
            ['textBody', ' Test Body   ', null, 'Test Body'],                    // Valid, asserting StringTrim
106
107
            ['htmlBody', '', [NotEmpty::IS_EMPTY]],                              // Empty
108
            ['htmlBody', ' ', [NotEmpty::IS_EMPTY]],                             // Empty, asserting StringTrim
109
            ['htmlBody', 'Test Html', null, 'Test Html'],                        // Valid
110
            ['htmlBody', ' Test Html   ', null, 'Test Html'],                    // Valid, asserting StringTrim
111
112
            ['description', '', null, ''],                                       // Empty, still valid
113
            ['description', ' ', null, ''],                                      // Empty, asserting StringTrim
114
            ['description', 'Test description', null, 'Test description'],       // Valid
115
            ['description', ' Test description ', null, 'Test description'],     // Valid, asserting StringTrim
116
        ];
117
    }
118
119
    /**
120
     * @dataProvider propertyValidationData
121
     *
122
     * @param string      $property
123
     * @param string      $value
124
     * @param array|null  $errors
125
     * @param string|null $expected
126
     *
127
     * @return void
128
     */
129
    public function testValidationOfEachProperty($property, $value, array $errors = null, $expected = null)
130
    {
131
        // We only validate against the given property
132
        $this->inputFilter->setValidationGroup([$property]);
133
        $this->inputFilter->setData([$property => $value]);
134
135
        // we expect errors
136
        if (! empty($errors)) {
137
138
            $this->assertFalse($this->inputFilter->isValid());
139
            $this->assertEquals($errors, array_keys($this->inputFilter->getMessages()[$property]));
140
        } else {
141
142
            $this->assertTrue($this->inputFilter->isValid());
143
            $this->assertSame($expected, $this->inputFilter->getValue($property));
144
        }
145
    }
146
}
147