Completed
Push — master ( f169c2...fc1f10 )
by Grégoire
12s
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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests\Admin;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Exception\NoValueException;
19
use Sonata\AdminBundle\Tests\Fixtures\Admin\FieldDescription;
20
use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
21
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooBoolean;
22
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooCall;
23
24
class BaseFieldDescriptionTest extends TestCase
25
{
26
    public function testSetName(): void
27
    {
28
        $description = new FieldDescription();
29
        $description->setName('foo');
30
31
        $this->assertSame('foo', $description->getFieldName());
32
        $this->assertSame('foo', $description->getName());
33
    }
34
35
    public function testOptions(): void
36
    {
37
        $description = new FieldDescription();
38
        $description->setOption('foo', 'bar');
39
40
        $this->assertNull($description->getOption('bar'));
41
        $this->assertSame('bar', $description->getOption('foo'));
42
43
        $description->mergeOptions(['settings' => ['value_1', 'value_2']]);
44
        $description->mergeOptions(['settings' => ['value_1', 'value_3']]);
45
46
        $this->assertSame(['value_1', 'value_2', 'value_1', 'value_3'], $description->getOption('settings'));
47
48
        $description->mergeOption('settings', ['value_4']);
49
        $this->assertSame(['value_1', 'value_2', 'value_1', 'value_3', 'value_4'], $description->getOption('settings'));
50
51
        $description->mergeOption('bar', ['hello']);
52
53
        $this->assertCount(1, $description->getOption('bar'));
54
55
        $description->setOption('label', 'trucmuche');
56
        $this->assertSame('trucmuche', $description->getLabel());
57
        $this->assertNull($description->getTemplate());
58
        $description->setOptions(['type' => 'integer', 'template' => 'foo.twig.html', 'help' => 'fooHelp']);
59
60
        $this->assertSame('integer', $description->getType());
61
        $this->assertSame('foo.twig.html', $description->getTemplate());
62
        $this->assertSame('fooHelp', $description->getHelp());
63
64
        $this->assertCount(2, $description->getOptions());
65
66
        $description->setHelp('Please enter an integer');
67
        $this->assertSame('Please enter an integer', $description->getHelp());
68
69
        $description->setMappingType('int');
70
        $this->assertSame('int', $description->getMappingType());
71
72
        $this->assertSame('short_object_description_placeholder', $description->getOption('placeholder'));
73
        $description->setOptions(['placeholder' => false]);
74
        $this->assertFalse($description->getOption('placeholder'));
75
76
        $description->setOption('sortable', false);
77
        $this->assertFalse($description->isSortable());
78
79
        $description->setOption('sortable', 'field_name');
80
        $this->assertTrue($description->isSortable());
81
    }
82
83
    public function testAdmin(): void
84
    {
85
        $description = new FieldDescription();
86
87
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
88
        $description->setAdmin($admin);
89
        $this->isInstanceOf(AdminInterface::class, $description->getAdmin());
90
91
        $associationAdmin = $this->getMockForAbstractClass(AdminInterface::class);
92
        $associationAdmin->expects($this->once())->method('setParentFieldDescription');
93
94
        $this->assertFalse($description->hasAssociationAdmin());
95
        $description->setAssociationAdmin($associationAdmin);
96
        $this->assertTrue($description->hasAssociationAdmin());
97
        $this->isInstanceOf(AdminInterface::class, $description->getAssociationAdmin());
98
99
        $parent = $this->getMockForAbstractClass(AdminInterface::class);
100
        $description->setParent($parent);
101
        $this->isInstanceOf(AdminInterface::class, $description->getParent());
102
    }
103
104
    public function testGetValue(): void
105
    {
106
        $description = new FieldDescription();
107
        $description->setOption('code', 'getFoo');
108
109
        $mock = $this->getMockBuilder('stdClass')
110
            ->setMethods(['getFoo'])
111
            ->getMock();
112
        $mock->expects($this->once())->method('getFoo')->will($this->returnValue(42));
113
114
        $this->assertSame(42, $description->getFieldValue($mock, 'fake'));
115
116
        /*
117
         * Test with One parameter int
118
         */
119
        $arg1 = 38;
120
        $oneParameter = [$arg1];
121
        $description1 = new FieldDescription();
122
        $description1->setOption('code', 'getWithOneParameter');
123
        $description1->setOption('parameters', $oneParameter);
124
125
        $mock1 = $this->getMockBuilder('stdClass')
126
            ->setMethods(['getWithOneParameter'])
127
            ->getMock();
128
        $returnValue1 = $arg1 + 2;
129
        $mock1->expects($this->once())->method('getWithOneParameter')->with($this->equalTo($arg1))->will($this->returnValue($returnValue1));
130
131
        $this->assertSame(40, $description1->getFieldValue($mock1, 'fake'));
132
133
        /*
134
         * Test with Two parameters int
135
         */
136
        $arg2 = 4;
137
        $twoParameters = [$arg1, $arg2];
138
        $description2 = new FieldDescription();
139
        $description2->setOption('code', 'getWithTwoParameters');
140
        $description2->setOption('parameters', $twoParameters);
141
142
        $mock2 = $this->getMockBuilder('stdClass')
143
            ->setMethods(['getWithTwoParameters'])
144
            ->getMock();
145
        $returnValue2 = $arg1 + $arg2;
146
        $mock2->expects($this->any())->method('getWithTwoParameters')->with($this->equalTo($arg1), $this->equalTo($arg2))->will($this->returnValue($returnValue2));
147
        $this->assertSame(42, $description2->getFieldValue($mock2, 'fake'));
148
149
        /*
150
         * Test with underscored attribute name
151
         */
152
        foreach (['getFake', 'isFake', 'hasFake'] as $method) {
153
            $description3 = new FieldDescription();
154
            $mock3 = $this->getMockBuilder('stdClass')
155
                ->setMethods([$method])
156
                ->getMock();
157
158
            $mock3->expects($this->once())->method($method)->will($this->returnValue(42));
159
            $this->assertSame(42, $description3->getFieldValue($mock3, '_fake'));
160
        }
161
162
        $mock4 = $this->getMockBuilder('MockedTestObject')
163
            ->setMethods(['myMethod'])
164
            ->getMock();
165
        $mock4->expects($this->once())
166
            ->method('myMethod')
167
            ->will($this->returnValue('myMethodValue'));
168
169
        $description4 = new FieldDescription();
170
        $description4->setOption('code', 'myMethod');
171
172
        $this->assertEquals($description4->getFieldValue($mock4, null), 'myMethodValue');
173
    }
174
175
    public function testGetValueNoValueException(): void
176
    {
177
        $this->expectException(\Sonata\AdminBundle\Exception\NoValueException::class);
178
179
        $description = new FieldDescription();
180
        $mock = $this->getMockBuilder('stdClass')
181
            ->setMethods(['getFoo'])
182
            ->getMock();
183
184
        $description->getFieldValue($mock, 'fake');
185
    }
186
187
    public function testGetVirtualValue(): void
188
    {
189
        $description = new FieldDescription();
190
        $mock = $this->getMockBuilder('stdClass')
191
            ->setMethods(['getFoo'])
192
            ->getMock();
193
194
        $description->setOption('virtual_field', true);
195
        $description->getFieldValue($mock, 'fake');
196
    }
197
198
    public function testExceptionOnNonArrayOption(): void
199
    {
200
        $this->expectException(\RuntimeException::class);
201
202
        $description = new FieldDescription();
203
        $description->setOption('bar', 'hello');
204
        $description->mergeOption('bar', ['exception']);
205
    }
206
207
    public function testGetTranslationDomain(): void
208
    {
209
        $description = new FieldDescription();
210
211
        $admin = $this->createMock(AdminInterface::class);
212
        $description->setAdmin($admin);
213
214
        $admin->expects($this->once())
215
            ->method('getTranslationDomain')
216
            ->will($this->returnValue('AdminDomain'));
217
218
        $this->assertSame('AdminDomain', $description->getTranslationDomain());
219
220
        $admin->expects($this->never())
221
            ->method('getTranslationDomain');
222
        $description->setOption('translation_domain', 'ExtensionDomain');
223
        $this->assertSame('ExtensionDomain', $description->getTranslationDomain());
224
    }
225
226
    public function testGetInaccessibleValue(): void
227
    {
228
        $quux = 'quuX';
229
        $foo = new Foo();
230
        $foo->setQuux($quux);
231
        $ro = new \ReflectionObject($foo);
232
        $rm = $ro->getMethod('getQuux');
233
        $rm->setAccessible(true);
234
        $this->assertSame($quux, $rm->invokeArgs($foo, []));
235
236
        $description = new FieldDescription();
237
238
        $this->expectException(NoValueException::class);
239
        $description->getFieldValue($foo, 'quux');
240
    }
241
242
    public function testGetFieldValue(): void
243
    {
244
        $foo = new Foo();
245
        $foo->setBar('Bar');
246
247
        $description = new FieldDescription();
248
        $this->assertSame('Bar', $description->getFieldValue($foo, 'bar'));
249
        $foo->setBar('baR');
250
        $this->assertSame('baR', $description->getFieldValue($foo, 'bar'));
251
252
        $foo->qux = 'Qux';
253
        $this->assertSame('Qux', $description->getFieldValue($foo, 'qux'));
254
        $foo->qux = 'quX';
255
        $this->assertSame('quX', $description->getFieldValue($foo, 'qux'));
256
257
        $foo = new FooBoolean();
258
        $foo->setBar(true);
259
        $foo->setBaz(false);
260
261
        $description = new FieldDescription();
262
        $this->assertTrue($description->getFieldValue($foo, 'bar'));
263
        $this->assertFalse($description->getFieldValue($foo, 'baz'));
264
265
        $this->expectException(NoValueException::class);
266
        $description->getFieldValue($foo, 'inexistantMethod');
267
    }
268
269
    public function testGetFieldValueWithCodeOption(): void
270
    {
271
        $foo = new Foo();
272
        $foo->setBaz('Baz');
273
274
        $description = new FieldDescription();
275
276
        $description->setOption('code', 'getBaz');
277
        $this->assertSame('Baz', $description->getFieldValue($foo, 'inexistantMethod'));
278
279
        $description->setOption('code', 'inexistantMethod');
280
        $this->expectException(NoValueException::class);
281
        $description->getFieldValue($foo, 'inexistantMethod');
282
    }
283
284
    public function testGetFieldValueMagicCall(): void
285
    {
286
        $parameters = ['foo', 'bar'];
287
        $foo = new FooCall();
288
289
        $description = new FieldDescription();
290
        $description->setOption('parameters', $parameters);
291
        $this->assertSame(['inexistantMethod', $parameters], $description->getFieldValue($foo, 'inexistantMethod'));
292
293
        // repeating to cover retrieving cached getter
294
        $this->assertSame(['inexistantMethod', $parameters], $description->getFieldValue($foo, 'inexistantMethod'));
295
    }
296
297
    public function testGetFieldValueWithNullObject(): void
298
    {
299
        $foo = null;
300
        $description = new FieldDescription();
301
        $this->assertNull($description->getFieldValue($foo, 'bar'));
302
    }
303
}
304