Completed
Push — 3.x ( 2c19f5...4783e0 )
by Grégoire
06:30
created

testGetFieldValueWithNullObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

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 4
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Tests\Admin;
13
14
use PHPUnit\Framework\TestCase;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\AdminBundle\Admin\BaseFieldDescription;
17
use Sonata\AdminBundle\Exception\NoValueException;
18
use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
19
use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
20
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooBoolean;
21
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooCall;
22
23
class BaseFieldDescriptionTest extends TestCase
24
{
25
    public function testSetName()
26
    {
27
        $description = new FieldDescription();
28
        $description->setName('foo');
29
30
        $this->assertSame('foo', $description->getFieldName());
31
        $this->assertSame('foo', $description->getName());
32
    }
33
34
    public function testOptions()
35
    {
36
        $description = new FieldDescription();
37
        $description->setOption('foo', 'bar');
38
39
        $this->assertNull($description->getOption('bar'));
40
        $this->assertSame('bar', $description->getOption('foo'));
41
42
        $description->mergeOptions(['settings' => ['value_1', 'value_2']]);
43
        $description->mergeOptions(['settings' => ['value_1', 'value_3']]);
44
45
        $this->assertSame(['value_1', 'value_2', 'value_1', 'value_3'], $description->getOption('settings'));
46
47
        $description->mergeOption('settings', ['value_4']);
48
        $this->assertSame(['value_1', 'value_2', 'value_1', 'value_3', 'value_4'], $description->getOption('settings'));
49
50
        $description->mergeOption('bar', ['hello']);
51
52
        $this->assertCount(1, $description->getOption('bar'));
53
54
        $description->setOption('label', 'trucmuche');
55
        $this->assertSame('trucmuche', $description->getLabel());
56
        $this->assertNull($description->getTemplate());
57
        $description->setOptions(['type' => 'integer', 'template' => 'foo.twig.html', 'help' => 'fooHelp']);
58
59
        $this->assertSame('integer', $description->getType());
60
        $this->assertSame('foo.twig.html', $description->getTemplate());
61
        $this->assertSame('fooHelp', $description->getHelp());
62
63
        $this->assertCount(2, $description->getOptions());
64
65
        $description->setHelp('Please enter an integer');
66
        $this->assertSame('Please enter an integer', $description->getHelp());
67
68
        $description->setMappingType('int');
69
        $this->assertSame('int', $description->getMappingType());
70
71
        $this->assertSame('short_object_description_placeholder', $description->getOption('placeholder'));
72
        $description->setOptions(['placeholder' => false]);
73
        $this->assertFalse($description->getOption('placeholder'));
74
75
        $description->setOption('sortable', false);
76
        $this->assertFalse($description->isSortable());
77
78
        $description->setOption('sortable', 'field_name');
79
        $this->assertTrue($description->isSortable());
80
    }
81
82
    public function testAdmin()
83
    {
84
        $description = new FieldDescription();
85
86
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
87
        $description->setAdmin($admin);
88
        $this->isInstanceOf(AdminInterface::class, $description->getAdmin());
89
90
        $associationAdmin = $this->getMockForAbstractClass(AdminInterface::class);
91
        $associationAdmin->expects($this->once())->method('setParentFieldDescription');
92
93
        $this->assertFalse($description->hasAssociationAdmin());
94
        $description->setAssociationAdmin($associationAdmin);
95
        $this->assertTrue($description->hasAssociationAdmin());
96
        $this->isInstanceOf(AdminInterface::class, $description->getAssociationAdmin());
97
98
        $parent = $this->getMockForAbstractClass(AdminInterface::class);
99
        $description->setParent($parent);
100
        $this->isInstanceOf(AdminInterface::class, $description->getParent());
101
    }
102
103
    public function testGetValue()
104
    {
105
        $description = new FieldDescription();
106
        $description->setOption('code', 'getFoo');
107
108
        $mock = $this->getMockBuilder('stdClass')
109
            ->setMethods(['getFoo'])
110
            ->getMock();
111
        $mock->expects($this->once())->method('getFoo')->will($this->returnValue(42));
112
113
        $this->assertSame(42, $description->getFieldValue($mock, 'fake'));
114
115
        /*
116
         * Test with One parameter int
117
         */
118
        $arg1 = 38;
119
        $oneParameter = [$arg1];
120
        $description1 = new FieldDescription();
121
        $description1->setOption('code', 'getWithOneParameter');
122
        $description1->setOption('parameters', $oneParameter);
123
124
        $mock1 = $this->getMockBuilder('stdClass')
125
            ->setMethods(['getWithOneParameter'])
126
            ->getMock();
127
        $returnValue1 = $arg1 + 2;
128
        $mock1->expects($this->once())->method('getWithOneParameter')->with($this->equalTo($arg1))->will($this->returnValue($returnValue1));
129
130
        $this->assertSame(40, $description1->getFieldValue($mock1, 'fake'));
131
132
        /*
133
         * Test with Two parameters int
134
         */
135
        $arg2 = 4;
136
        $twoParameters = [$arg1, $arg2];
137
        $description2 = new FieldDescription();
138
        $description2->setOption('code', 'getWithTwoParameters');
139
        $description2->setOption('parameters', $twoParameters);
140
141
        $mock2 = $this->getMockBuilder('stdClass')
142
            ->setMethods(['getWithTwoParameters'])
143
            ->getMock();
144
        $returnValue2 = $arg1 + $arg2;
145
        $mock2->expects($this->any())->method('getWithTwoParameters')->with($this->equalTo($arg1), $this->equalTo($arg2))->will($this->returnValue($returnValue2));
146
        $this->assertSame(42, $description2->getFieldValue($mock2, 'fake'));
147
148
        /*
149
         * Test with underscored attribute name
150
         */
151
        foreach (['getFake', 'isFake', 'hasFake'] as $method) {
152
            $description3 = new FieldDescription();
153
            $mock3 = $this->getMockBuilder('stdClass')
154
                ->setMethods([$method])
155
                ->getMock();
156
157
            $mock3->expects($this->once())->method($method)->will($this->returnValue(42));
158
            $this->assertSame(42, $description3->getFieldValue($mock3, '_fake'));
159
        }
160
161
        $mock4 = $this->getMockBuilder('MockedTestObject')
162
            ->setMethods(['myMethod'])
163
            ->getMock();
164
        $mock4->expects($this->once())
165
            ->method('myMethod')
166
            ->will($this->returnValue('myMethodValue'));
167
168
        $description4 = new FieldDescription();
169
        $description4->setOption('code', 'myMethod');
170
171
        $this->assertEquals($description4->getFieldValue($mock4, null), 'myMethodValue');
172
    }
173
174
    public function testGetValueNoValueException()
175
    {
176
        $this->expectException(\Sonata\AdminBundle\Exception\NoValueException::class);
177
178
        $description = new FieldDescription();
179
        $mock = $this->getMockBuilder('stdClass')
180
            ->setMethods(['getFoo'])
181
            ->getMock();
182
183
        $description->getFieldValue($mock, 'fake');
184
    }
185
186
    public function testGetVirtualValue()
187
    {
188
        $description = new FieldDescription();
189
        $mock = $this->getMockBuilder('stdClass')
190
            ->setMethods(['getFoo'])
191
            ->getMock();
192
193
        $description->setOption('virtual_field', true);
194
        $description->getFieldValue($mock, 'fake');
195
    }
196
197
    public function testExceptionOnNonArrayOption()
198
    {
199
        $this->expectException(\RuntimeException::class);
200
201
        $description = new FieldDescription();
202
        $description->setOption('bar', 'hello');
203
        $description->mergeOption('bar', ['exception']);
204
    }
205
206
    public function testGetTranslationDomain()
207
    {
208
        $description = new FieldDescription();
209
210
        $admin = $this->createMock(AdminInterface::class);
211
        $description->setAdmin($admin);
212
213
        $admin->expects($this->once())
214
            ->method('getTranslationDomain')
215
            ->will($this->returnValue('AdminDomain'));
216
217
        $this->assertSame('AdminDomain', $description->getTranslationDomain());
218
219
        $admin->expects($this->never())
220
            ->method('getTranslationDomain');
221
        $description->setOption('translation_domain', 'ExtensionDomain');
222
        $this->assertSame('ExtensionDomain', $description->getTranslationDomain());
223
    }
224
225
    /**
226
     * @group legacy
227
     */
228
    public function testCamelize()
229
    {
230
        $this->assertSame('FooBar', BaseFieldDescription::camelize('foo_bar'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...Description::camelize() has been deprecated with message: Deprecated since version 3.1. Use \Doctrine\Common\Inflector\Inflector::classify() instead

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...
231
        $this->assertSame('FooBar', BaseFieldDescription::camelize('foo bar'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...Description::camelize() has been deprecated with message: Deprecated since version 3.1. Use \Doctrine\Common\Inflector\Inflector::classify() instead

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...
232
        $this->assertSame('FOoBar', BaseFieldDescription::camelize('fOo bar'));
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...Description::camelize() has been deprecated with message: Deprecated since version 3.1. Use \Doctrine\Common\Inflector\Inflector::classify() instead

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...
233
    }
234
235
    public function testGetInaccessibleValue()
236
    {
237
        $quux = 'quuX';
238
        $foo = new Foo();
239
        $foo->setQuux($quux);
240
        $ro = new \ReflectionObject($foo);
241
        $rm = $ro->getMethod('getQuux');
242
        $rm->setAccessible(true);
243
        $this->assertSame($quux, $rm->invokeArgs($foo, []));
244
245
        $description = new FieldDescription();
246
247
        $this->expectException(NoValueException::class);
248
        $description->getFieldValue($foo, 'quux');
249
    }
250
251
    public function testGetFieldValue()
252
    {
253
        $foo = new Foo();
254
        $foo->setBar('Bar');
255
256
        $description = new FieldDescription();
257
        $this->assertSame('Bar', $description->getFieldValue($foo, 'bar'));
258
        $foo->setBar('baR');
259
        $this->assertSame('baR', $description->getFieldValue($foo, 'bar'));
260
261
        $foo->qux = 'Qux';
262
        $this->assertSame('Qux', $description->getFieldValue($foo, 'qux'));
263
        $foo->qux = 'quX';
264
        $this->assertSame('quX', $description->getFieldValue($foo, 'qux'));
265
266
        $foo = new FooBoolean();
267
        $foo->setBar(true);
268
        $foo->setBaz(false);
269
270
        $description = new FieldDescription();
271
        $this->assertTrue($description->getFieldValue($foo, 'bar'));
272
        $this->assertFalse($description->getFieldValue($foo, 'baz'));
273
274
        $this->expectException(NoValueException::class);
275
        $description->getFieldValue($foo, 'inexistantMethod');
276
    }
277
278
    public function testGetFieldValueWithCodeOption()
279
    {
280
        $foo = new Foo();
281
        $foo->setBaz('Baz');
282
283
        $description = new FieldDescription();
284
285
        $description->setOption('code', 'getBaz');
286
        $this->assertSame('Baz', $description->getFieldValue($foo, 'inexistantMethod'));
287
288
        $description->setOption('code', 'inexistantMethod');
289
        $this->expectException(NoValueException::class);
290
        $description->getFieldValue($foo, 'inexistantMethod');
291
    }
292
293
    public function testGetFieldValueMagicCall()
294
    {
295
        $parameters = ['foo', 'bar'];
296
        $foo = new FooCall();
297
298
        $description = new FieldDescription();
299
        $description->setOption('parameters', $parameters);
300
        $this->assertSame(['inexistantMethod', $parameters], $description->getFieldValue($foo, 'inexistantMethod'));
301
302
        // repeating to cover retrieving cached getter
303
        $this->assertSame(['inexistantMethod', $parameters], $description->getFieldValue($foo, 'inexistantMethod'));
304
    }
305
306
    public function testGetFieldValueWithNullObject()
307
    {
308
        $foo = null;
309
        $description = new FieldDescription();
310
        $this->assertNull($description->getFieldValue($foo, 'bar'));
311
    }
312
}
313