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

Form/Element/ObjectMultiCheckboxTest.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
namespace DoctrineModuleTest\Form\Element;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use DoctrineModule\Form\Element\ObjectMultiCheckbox;
9
use function get_class;
10
11
/**
12
 * Tests for the ObjectMultiCheckbox element
13
 *
14
 * @link    http://www.doctrine-project.org/
15
 *
16
 * @covers  \DoctrineModule\Form\Element\ObjectMultiCheckbox
17
 */
18
class ObjectMultiCheckboxTest extends ProxyAwareElementTestCase
19
{
20
    /** @var ArrayCollection */
21
    protected $values;
22
23
    /** @var ObjectMultiCheckbox */
24
    protected $element;
25
26
    /**
27
     * {@inheritDoc}.
28
     */
29
    protected function setUp() : void
30
    {
31
        parent::setUp();
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class DoctrineModuleTest\Form\...oxyAwareElementTestCase as the method setUp() does only exist in the following sub-classes of DoctrineModuleTest\Form\...oxyAwareElementTestCase: DoctrineModuleTest\Form\...ObjectMultiCheckboxTest, DoctrineModuleTest\Form\Element\ObjectRadioTest, DoctrineModuleTest\Form\Element\ObjectSelectTest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
32
        $this->element = new ObjectMultiCheckbox();
33
34
        $this->prepareProxy();
35
    }
36
37
    public function testSetValueWithCollection() : void
38
    {
39
        $this->element->setValue(
40
            $this->values
41
        );
42
43
        $this->assertEquals(
0 ignored issues
show
The method assertEquals() does not seem to exist on object<DoctrineModuleTes...bjectMultiCheckboxTest>.

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...
44
            [1, 2],
45
            $this->element->getValue()
46
        );
47
    }
48
49
    public function testSetValueWithArray() : void
50
    {
51
        $this->element->setValue(
52
            $this->values->toArray()
53
        );
54
55
        $this->assertEquals(
0 ignored issues
show
The method assertEquals() does not seem to exist on object<DoctrineModuleTes...bjectMultiCheckboxTest>.

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...
56
            [1, 2],
57
            $this->element->getValue()
58
        );
59
    }
60
61
    public function testGetValueOptionsDoesntCauseInfiniteLoopIfProxyReturnsEmptyArrayAndValidatorIsInitialized() : void
62
    {
63
        $element = $this->createPartialMock(get_class($this->element), ['setValueOptions']);
64
65
        $options = [];
66
67
        $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy');
68
        $proxy->expects($this->exactly(2))
69
            ->method('getValueOptions')
70
            ->will($this->returnValue($options));
71
72
        $element->expects($this->never())
73
            ->method('setValueOptions');
74
75
        $this->setProxyViaReflection($proxy, $element);
76
        $element->getInputSpecification();
77
        $this->assertEquals($options, $element->getValueOptions());
78
    }
79
80
    public function testGetValueOptionsDoesntInvokeProxyIfOptionsNotEmpty() : void
81
    {
82
        $options = ['foo' => 'bar'];
83
84
        $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy');
85
        $proxy->expects($this->once())
86
            ->method('getValueOptions')
87
            ->will($this->returnValue($options));
88
89
        $this->setProxyViaReflection($proxy);
90
91
        $this->assertEquals($options, $this->element->getValueOptions());
92
        $this->assertEquals($options, $this->element->getValueOptions());
93
    }
94
95
    public function testOptionsCanBeSetSingle() : void
96
    {
97
        $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy');
98
        $proxy->expects($this->once())->method('setOptions')->with(['is_method' => true]);
99
100
        $this->setProxyViaReflection($proxy);
101
102
        $this->element->setOption('is_method', true);
103
    }
104
}
105