Completed
Push — 3.x ( e95e95...638cd1 )
by Oskar
05:54
created

tests/Admin/BaseFieldDescriptionTest.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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