Completed
Push — master ( 3fd2dd...aba624 )
by Grégoire
04:19
created

testReorderListWithBatchField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
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\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);
0 ignored issues
show
Documentation introduced by
$fieldDescription is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...ldDescriptionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
29
30
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
31
        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position'));
32
        $collection->add($fieldDescription);
0 ignored issues
show
Documentation introduced by
$fieldDescription is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...ldDescriptionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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());
0 ignored issues
show
Documentation introduced by
$collection->getElements() is of type array<integer,object<Son...dDescriptionInterface>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
        $this->assertCount(2, $collection);
41
42
        $this->isInstanceOf(FieldDescriptionInterface::class, $collection['title']);
0 ignored issues
show
Unused Code introduced by
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
Unused Code introduced by
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());
0 ignored issues
show
Documentation introduced by
$collection->getElements() is of type array<integer,object<Son...dDescriptionInterface>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$fieldDescription is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...ldDescriptionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
81
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
82
        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position'));
83
        $collection->add($fieldDescription);
0 ignored issues
show
Documentation introduced by
$fieldDescription is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...ldDescriptionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$fieldDescription is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...ldDescriptionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
100
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
101
        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position'));
102
        $collection->add($fieldDescription);
0 ignored issues
show
Documentation introduced by
$fieldDescription is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...ldDescriptionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
104
        $fieldDescription = $this->createMock(FieldDescriptionInterface::class);
105
        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('batch'));
106
        $collection->add($fieldDescription);
0 ignored issues
show
Documentation introduced by
$fieldDescription is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Sonata\AdminBundl...ldDescriptionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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