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

testSetValueWithCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
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\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
Bug introduced by
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
Bug introduced by
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
Bug introduced by
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']);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $element is correct as $this->createPartialMock...ray('setValueOptions')) (which targets PHPUnit\Framework\TestCase::createPartialMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
64
65
        $options = [];
66
67
        $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $proxy is correct as $this->createMock('Doctr...\Form\\Element\\Proxy') (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68
        $proxy->expects($this->exactly(2))
0 ignored issues
show
Bug introduced by
The method exactly() 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...
Bug introduced by
The method expects cannot be called on $proxy (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
69
            ->method('getValueOptions')
70
            ->will($this->returnValue($options));
0 ignored issues
show
Bug introduced by
The method returnValue() 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...
71
72
        $element->expects($this->never())
0 ignored issues
show
Bug introduced by
The method never() 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...
Bug introduced by
The method expects cannot be called on $element (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
73
            ->method('setValueOptions');
74
75
        $this->setProxyViaReflection($proxy, $element);
0 ignored issues
show
Documentation introduced by
$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...
76
        $element->getInputSpecification();
0 ignored issues
show
Bug introduced by
The method getInputSpecification cannot be called on $element (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
77
        $this->assertEquals($options, $element->getValueOptions());
0 ignored issues
show
Bug introduced by
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...
Bug introduced by
The method getValueOptions cannot be called on $element (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
78
    }
79
80
    public function testGetValueOptionsDoesntInvokeProxyIfOptionsNotEmpty() : void
81
    {
82
        $options = ['foo' => 'bar'];
83
84
        $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $proxy is correct as $this->createMock('Doctr...\Form\\Element\\Proxy') (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
        $proxy->expects($this->once())
0 ignored issues
show
Bug introduced by
The method once() 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...
Bug introduced by
The method expects cannot be called on $proxy (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
86
            ->method('getValueOptions')
87
            ->will($this->returnValue($options));
0 ignored issues
show
Bug introduced by
The method returnValue() 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...
88
89
        $this->setProxyViaReflection($proxy);
0 ignored issues
show
Documentation introduced by
$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...
90
91
        $this->assertEquals($options, $this->element->getValueOptions());
0 ignored issues
show
Bug introduced by
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...
92
        $this->assertEquals($options, $this->element->getValueOptions());
0 ignored issues
show
Bug introduced by
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...
93
    }
94
95
    public function testOptionsCanBeSetSingle() : void
96
    {
97
        $proxy = $this->createMock('DoctrineModule\Form\Element\Proxy');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $proxy is correct as $this->createMock('Doctr...\Form\\Element\\Proxy') (which targets PHPUnit\Framework\TestCase::createMock()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
98
        $proxy->expects($this->once())->method('setOptions')->with(['is_method' => true]);
0 ignored issues
show
Bug introduced by
The method once() 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...
Bug introduced by
The method expects cannot be called on $proxy (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
99
100
        $this->setProxyViaReflection($proxy);
0 ignored issues
show
Documentation introduced by
$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...
101
102
        $this->element->setOption('is_method', true);
103
    }
104
}
105