Completed
Push — master ( afca2b...7dac57 )
by Eric
9s
created

BooleanExtensionTest::testValidInitialData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\ResourceBundle\Tests\Form\Extension;
13
14
use Lug\Bundle\ResourceBundle\Form\DataTransformer\BooleanTransformer;
15
use Lug\Bundle\ResourceBundle\Form\Extension\BooleanExtension;
16
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface;
17
use Symfony\Component\Form\Extension\Core\DataTransformer\BooleanToStringTransformer;
18
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
19
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
20
use Symfony\Component\Form\FormFactoryInterface;
21
use Symfony\Component\Form\Forms;
22
use Symfony\Component\Validator\Validation;
23
24
/**
25
 * @author GeLo <[email protected]>
26
 */
27
class BooleanExtensionTest extends \PHPUnit_Framework_TestCase
28
{
29
    /**
30
     * @var FormFactoryInterface
31
     */
32
    private $formFactory;
33
34
    /**
35
     * @var \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface
36
     */
37
    private $parameterResolver;
38
39
    /**
40
     * @var BooleanTransformer
41
     */
42
    private $booleanTransformer;
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function setUp()
48
    {
49
        $this->parameterResolver = $this->createParameterResolverMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createParameterResolverMock() can also be of type object<Lug\Bundle\Resour...meterResolverInterface>. However, the property $parameterResolver is declared as type object<PHPUnit_Framework_MockObject_MockObject>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
50
        $this->booleanTransformer = new BooleanTransformer();
51
52
        $this->formFactory = Forms::createFormFactoryBuilder()
53
            ->addExtension(new ValidatorExtension(Validation::createValidator()))
54
            ->addTypeExtension(new BooleanExtension($this->parameterResolver, $this->booleanTransformer))
55
            ->getFormFactory();
56
    }
57
58 View Code Duplication
    public function testCheckboxType()
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...
59
    {
60
        $viewTransformers = $this->formFactory
61
            ->create(CheckboxType::class, null, ['api' => false])
62
            ->getConfig()
63
            ->getViewTransformers();
64
65
        $this->assertCount(1, $viewTransformers);
66
        $this->assertInstanceOf(BooleanToStringTransformer::class, $viewTransformers[0]);
67
    }
68
69 View Code Duplication
    public function testApiType()
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...
70
    {
71
        $viewTransformers = $this->formFactory
72
            ->create(CheckboxType::class, null, ['api' => true])
73
            ->getConfig()
74
            ->getViewTransformers();
75
76
        $this->assertCount(1, $viewTransformers);
77
        $this->assertInstanceOf(BooleanTransformer::class, $viewTransformers[0]);
78
    }
79
80
    /**
81
     * @dataProvider validInitialProvider
82
     */
83
    public function testValidInitialData($expected, $data)
84
    {
85
        $form = $this->formFactory->create(CheckboxType::class, $data, ['api' => true]);
86
87
        $this->assertSame($expected, $form->getData());
88
    }
89
90
    /**
91
     * @dataProvider invalidProvider
92
     *
93
     * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
94
     * @expectedExceptionMessage The boolean type expects a boolean or null value
95
     */
96
    public function testInvalidInitialData($data)
97
    {
98
        $this->formFactory->create(CheckboxType::class, $data, ['api' => true]);
99
    }
100
101
    /**
102
     * @dataProvider validSubmitProvider
103
     */
104 View Code Duplication
    public function testValidSubmittedData($expected, $data)
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...
105
    {
106
        $form = $this->formFactory
107
            ->create(CheckboxType::class, null, ['api' => true])
108
            ->submit($data);
109
110
        $this->assertTrue($form->isValid());
111
        $this->assertSame($expected, $form->getData());
112
    }
113
114
    /**
115
     * @dataProvider invalidProvider
116
     */
117 View Code Duplication
    public function testInvalidSubmittedData($data)
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
        $form = $this->formFactory
120
            ->create(CheckboxType::class, null, ['api' => true])
121
            ->submit($data);
122
123
        $this->assertFalse($form->isValid());
124
        $this->assertNull($form->getData());
125
    }
126
127
    /**
128
     * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
129
     * @expectedExceptionMessage The option "api" with value "foo" is expected to be of type "bool", but is of type "string".
130
     */
131
    public function testInvalidApiOption()
132
    {
133
        $this->formFactory->create(CheckboxType::class, null, ['api' => 'foo']);
134
    }
135
136
    /**
137
     * @return mixed[]
138
     */
139
    public function validInitialProvider()
140
    {
141
        return [
142
            [true, true],
143
            [false, false],
144
            [false, null],
145
        ];
146
    }
147
148
    /**
149
     * @return mixed[]
150
     */
151
    public function validSubmitProvider()
152
    {
153
        return [
154
            [true, true],
155
            [true, 1],
156
            [true, '1'],
157
            [true, 'true'],
158
            [true, 'yes'],
159
            [true, 'on'],
160
            [false, false],
161
            [false, 0],
162
            [false, '0'],
163
            [false, 'false'],
164
            [false, 'no'],
165
            [false, 'off'],
166
            [false, ''],
167
            [false, null],
168
        ];
169
    }
170
171
    /**
172
     * @return mixed[]
173
     */
174
    public function invalidProvider()
175
    {
176
        return [
177
            ['foo'],
178
            [1.2],
179
            [new \stdClass()],
180
            [['foo' => 'bar']],
181
        ];
182
    }
183
184
    /**
185
     * @return \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface
186
     */
187
    private function createParameterResolverMock()
188
    {
189
        return $this->getMock(ParameterResolverInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
190
    }
191
}
192