ProxyAwareElementTestCase::prepareProxy()   B
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 8.6545
c 0
b 0
f 0
cc 3
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModuleTest\Form\Element;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use DoctrineModuleTest\Form\Element\TestAsset\FormObject;
9
use PHPUnit\Framework\MockObject\MockObject;
10
use PHPUnit\Framework\TestCase;
11
use ReflectionProperty;
12
use function array_shift;
13
use function func_get_args;
14
use function get_class;
15
16
class ProxyAwareElementTestCase extends TestCase
17
{
18
    /** @var MockObject */
19
    protected $element;
20
21
    protected function prepareProxy() : void
22
    {
23
        $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject';
24
        $objectOne   = new FormObject();
25
        $objectTwo   = new FormObject();
26
27
        $objectOne->setId(1)
28
            ->setUsername('object one username')
29
            ->setPassword('object one password')
30
            ->setEmail('object one email')
31
            ->setFirstname('object one firstname')
32
            ->setSurname('object one surname');
33
34
        $objectTwo->setId(2)
35
            ->setUsername('object two username')
36
            ->setPassword('object two password')
37
            ->setEmail('object two email')
38
            ->setFirstname('object two firstname')
39
            ->setSurname('object two surname');
40
41
        $result       = new ArrayCollection([$objectOne, $objectTwo]);
42
        $this->values = $result;
0 ignored issues
show
Bug introduced by
The property values does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
44
        $metadata = $this->createMock('Doctrine\Persistence\Mapping\ClassMetadata');
45
        $metadata
46
            ->expects($this->any())
47
            ->method('getIdentifierValues')
48
            ->will(
49
                $this->returnCallback(
50
                    static function () use ($objectOne, $objectTwo) {
51
                        $input = func_get_args();
52
                        $input = array_shift($input);
53
54
                        if ($input === $objectOne) {
55
                            return ['id' => 1];
56
                        }
57
58
                        if ($input === $objectTwo) {
59
                            return ['id' => 2];
60
                        }
61
62
                        return [];
63
                    }
64
                )
65
            );
66
67
        $objectRepository = $this->createMock('Doctrine\Persistence\ObjectRepository');
68
        $objectRepository->expects($this->any())
69
            ->method('findAll')
70
            ->will($this->returnValue($result));
71
72
        $objectManager = $this->createMock('Doctrine\Persistence\ObjectManager');
73
        $objectManager->expects($this->any())
74
            ->method('getClassMetadata')
75
            ->with($this->equalTo($objectClass))
76
            ->will($this->returnValue($metadata));
77
78
        $objectManager
79
            ->expects($this->any())
80
            ->method('getRepository')
81
            ->with($this->equalTo($objectClass))
82
            ->will($this->returnValue($objectRepository));
83
84
        $this->element->getProxy()->setOptions([
0 ignored issues
show
Bug introduced by
The method getProxy() 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...
85
            'object_manager' => $objectManager,
86
            'target_class'   => $objectClass,
87
        ]);
88
89
        $this->metadata = $metadata;
0 ignored issues
show
Bug introduced by
The property metadata does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
90
    }
91
92
    /**
93
     * Proxy should stay read only, use with care
94
     */
95
    protected function setProxyViaReflection(MockObject $proxy, ?MockObject $element = null) : void
96
    {
97
        if (! $element) {
98
            $element = $this->element;
99
        }
100
101
        $prop = new ReflectionProperty(get_class($this->element), 'proxy');
102
        $prop->setAccessible(true);
103
        $prop->setValue($element, $proxy);
104
    }
105
}
106