Completed
Push — 3.x ( b75183...b1c847 )
by Oskar
04:45
created

tests/Admin/FieldDescriptionCollectionTest.php (2 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\FieldDescriptionCollection;
18
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
19
20
class FieldDescriptionCollectionTest extends TestCase
21
{
22
    public function testMethods(): void
23
    {
24
        $collection = new FieldDescriptionCollection();
25
26
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
27
        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('title'));
28
        $collection->add($fieldDescription);
29
30
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
31
        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position'));
32
        $collection->add($fieldDescription);
33
34
        $this->assertFalse($collection->has('foo'));
35
        $this->assertFalse(isset($collection['foo']));
36
        $this->assertTrue($collection->has('title'));
37
        $this->assertTrue(isset($collection['title']));
38
39
        $this->assertCount(2, $collection->getElements());
40
        $this->assertCount(2, $collection);
41
42
        $this->isInstanceOf(FieldDescriptionInterface::class, $collection['title']);
0 ignored issues
show
The call to FieldDescriptionCollectionTest::isInstanceOf() has too many arguments starting with $collection['title'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
43
        $this->isInstanceOf(FieldDescriptionInterface::class, $collection->get('title'));
0 ignored issues
show
The call to FieldDescriptionCollectionTest::isInstanceOf() has too many arguments starting with $collection->get('title').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
44
45
        $collection->remove('title');
46
        $this->assertFalse($collection->has('title'));
47
48
        unset($collection['position']);
49
50
        $this->assertCount(0, $collection->getElements());
51
        $this->assertCount(0, $collection);
52
    }
53
54
    public function testNonExistentField(): void
55
    {
56
        $this->expectException(\InvalidArgumentException::class);
57
        $this->expectExceptionMessage('Element "foo" does not exist.');
58
59
        $collection = new FieldDescriptionCollection();
60
        $collection->get('foo');
61
    }
62
63
    public function testArrayAccessSetField(): void
64
    {
65
        $this->expectException(\RuntimeException::class);
66
        $this->expectExceptionMessage('Cannot set value, use add');
67
68
        $collection = new FieldDescriptionCollection();
69
70
        $collection['foo'] = null;
71
    }
72
73
    public function testReorderListWithoutBatchField(): void
74
    {
75
        $collection = new FieldDescriptionCollection();
76
77
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
78
        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('title'));
79
        $collection->add($fieldDescription);
80
81
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
82
        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position'));
83
        $collection->add($fieldDescription);
84
85
        $newOrder = ['position', 'title'];
86
        $collection->reorder($newOrder);
87
88
        $actualElements = array_keys($collection->getElements());
89
        $this->assertSame($newOrder, $actualElements, 'the order is wrong');
90
    }
91
92
    public function testReorderListWithBatchField(): void
93
    {
94
        $collection = new FieldDescriptionCollection();
95
96
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
97
        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('title'));
98
        $collection->add($fieldDescription);
99
100
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
101
        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position'));
102
        $collection->add($fieldDescription);
103
104
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
105
        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('batch'));
106
        $collection->add($fieldDescription);
107
108
        $newOrder = ['position', 'title'];
109
        $collection->reorder($newOrder);
110
        array_unshift($newOrder, 'batch');
111
112
        $actualElements = array_keys($collection->getElements());
113
        $this->assertSame($newOrder, $actualElements, 'the order is wrong');
114
    }
115
}
116