ObjectSelectTest::testSetValueSingleValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
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();
0 ignored issues
show
Bug introduced by
The method getInputSpecification() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
        $this->assertEquals($options, $element->getValueOptions());
0 ignored issues
show
Bug introduced by
The method getValueOptions() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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