Completed
Push — master ( e0017c...6b1304 )
by Tom
14s queued 11s
created

Form/Element/ObjectSelectTest.php (1 issue)

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
namespace DoctrineModuleTest\Form\Element;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use DoctrineModule\Form\Element\ObjectSelect;
9
use function get_class;
10
11
/**
12
 * Tests for the ObjectSelect element
13
 *
14
 * @link    http://www.doctrine-project.org/
15
 *
16
 * @covers  \DoctrineModule\Form\Element\ObjectSelect
17
 */
18
class ObjectSelectTest extends ProxyAwareElementTestCase
19
{
20
    /** @var ArrayCollection */
21
    protected $values;
22
23
    /** @var ObjectSelect */
24
    protected $element;
25
26
    /**
27
     * {@inheritDoc}.
28
     */
29
    protected function setUp() : void
30
    {
31
        parent::setUp();
32
        $this->element = new ObjectSelect();
33
34
        $this->prepareProxy();
35
    }
36
37
    public function testSetValueWithCollection() : void
38
    {
39
        $this->element->setAttribute('multiple', true);
40
41
        $this->element->setValue(
42
            $this->values
43
        );
44
45
        $this->assertEquals(
46
            [1, 2],
47
            $this->element->getValue()
48
        );
49
    }
50
51
    public function testSetValueWithArray() : void
52
    {
53
        $this->element->setAttribute('multiple', true);
54
55
        $this->element->setValue(
56
            $this->values->toArray()
57
        );
58
59
        $this->assertEquals(
60
            [1, 2],
61
            $this->element->getValue()
62
        );
63
    }
64
65
    public function testSetValueSingleValue() : void
66
    {
67
        $value = $this->values->toArray();
68
69
        $this->element->setValue(
70
            $value[0]
71
        );
72
73
        $this->assertEquals(
74
            1,
75
            $this->element->getValue()
76
        );
77
    }
78
79
    public function testGetValueOptionsDoesntCauseInfiniteLoopIfProxyReturnsEmptyArrayAndValidatorIsInitialized() : void
80
    {
81
        $element = $this->createPartialMock(get_class($this->element), ['setValueOptions']);
82
83
        $options = [];
84
85
        $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy');
86
        $proxy->expects($this->exactly(2))
87
              ->method('getValueOptions')
88
              ->will($this->returnValue($options));
89
90
        $element->expects($this->never())
91
                ->method('setValueOptions');
92
93
        $this->setProxyViaReflection($proxy, $element);
94
        $element->getInputSpecification();
95
        $this->assertEquals($options, $element->getValueOptions());
96
    }
97
98
    public function testGetValueOptionsDoesntInvokeProxyIfOptionsNotEmpty() : void
99
    {
100
        $options = ['foo' => 'bar'];
101
102
        $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy');
103
        $proxy->expects($this->once())
104
              ->method('getValueOptions')
105
              ->will($this->returnValue($options));
106
107
        $this->setProxyViaReflection($proxy);
108
109
        $this->assertEquals($options, $this->element->getValueOptions());
110
        $this->assertEquals($options, $this->element->getValueOptions());
111
    }
112
113
    public function testOptionsCanBeSetSingle() : void
114
    {
115
        $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy');
116
        $proxy->expects($this->once())->method('setOptions')->with(['is_method' => true]);
117
118
        $this->setProxyViaReflection($proxy);
0 ignored issues
show
$proxy is of type null, but the function expects a object<PHPUnit\Framework\MockObject\MockObject>.

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...
119
120
        $this->element->setOption('is_method', true);
121
    }
122
}
123