Completed
Push — master ( 36acf6...c2375b )
by
unknown
13:20
created

CustomerVoterTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Tests\Unit\Acl\Voter;
4
5
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
6
7
use OroCRM\Bundle\MagentoBundle\Acl\Voter\CustomerVoter;
8
use OroCRM\Bundle\MagentoBundle\Entity\Customer;
9
10
class CustomerVoterTest extends AbstractTwoWaySyncVoterTest
11
{
12
    /**
13
     * @var CustomerVoter
14
     */
15
    protected $voter;
16
17
    protected function setUp()
18
    {
19
        parent::setUp();
20
21
        $this->voter = new CustomerVoter($this->doctrineHelper);
22
        $this->voter->setSettingsProvider($this->settingsProvider);
23
    }
24
25
    protected function tearDown()
26
    {
27
        unset($this->voter, $this->doctrineHelper);
28
    }
29
30
    /**
31
     * @param object $object
32
     * @param string $className
33
     * @param array $attributes
34
     * @param bool $hasApplicableChannels
35
     * @param bool $isChannelApplicable
36
     * @param bool $expected
37
     *
38
     * @dataProvider attributesDataProvider
39
     */
40 View Code Duplication
    public function testVote($object, $className, $attributes, $hasApplicableChannels, $isChannelApplicable, $expected)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $this->doctrineHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Oro\Bundle\EntityBundle\ORM\DoctrineHelper.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
43
            ->method('getEntityClass')
44
            ->with($object)
45
            ->will($this->returnValue($className));
46
47
        $this->voter->setClassName('OroCRM\Bundle\MagentoBundle\Entity\Customer');
48
49
        $this->doctrineHelper->expects($this->any())
50
            ->method('getSingleEntityIdentifier')
51
            ->with($object, false)
52
            ->will($this->returnValue(1));
53
54
        $this->settingsProvider->expects($this->any())
55
            ->method('isChannelApplicable')
56
            ->will($this->returnValue($isChannelApplicable));
57
58
        $this->settingsProvider->expects($this->any())
59
            ->method('hasApplicableChannels')
60
            ->will($this->returnValue($hasApplicableChannels));
61
62
        /** @var TokenInterface $token */
63
        $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
64
        $this->assertEquals(
65
            $expected,
66
            $this->voter->vote($token, $object, $attributes)
67
        );
68
    }
69
70
    /**
71
     * @return array
72
     *
73
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
74
     */
75
    public function attributesDataProvider()
76
    {
77
        $className = 'OroCRM\Bundle\MagentoBundle\Entity\Customer';
78
        $objectIdentityClass = 'Symfony\Component\Security\Acl\Model\ObjectIdentityInterface';
79
80
81
        return [
82
            // has not applicable channels
83
            [
84
                $this->getObjectIdentity($objectIdentityClass, $className),
85
                $objectIdentityClass, ['VIEW'],
86
                false,
87
                false,
88
                CustomerVoter::ACCESS_ABSTAIN
89
            ],
90
            [
91
                $this->getObjectIdentity($objectIdentityClass, $className),
92
                $objectIdentityClass,
93
                ['CREATE'],
94
                false,
95
                false,
96
                CustomerVoter::ACCESS_DENIED
97
            ],
98
            [
99
                $this->getObjectIdentity($objectIdentityClass, $className),
100
                $objectIdentityClass,
101
                ['EDIT'],
102
                false,
103
                false,
104
                CustomerVoter::ACCESS_DENIED
105
            ],
106
            [
107
                $this->getObjectIdentity($objectIdentityClass, $className),
108
                $objectIdentityClass,
109
                ['DELETE'],
110
                false,
111
                false,
112
                CustomerVoter::ACCESS_ABSTAIN
113
            ],
114
            [
115
                $this->getObjectIdentity($objectIdentityClass, $className),
116
                $objectIdentityClass,
117
                ['ASSIGN'],
118
                false,
119
                false,
120
                CustomerVoter::ACCESS_ABSTAIN
121
            ],
122
            // channel not applicable
123
            [$this->getCustomer(), $className, ['VIEW'], true, false, CustomerVoter::ACCESS_ABSTAIN],
124
            [$this->getCustomer(), $className, ['CREATE'], true, false, CustomerVoter::ACCESS_DENIED],
125
            [$this->getCustomer(), $className, ['EDIT'], true, false, CustomerVoter::ACCESS_DENIED],
126
            [$this->getCustomer(), $className, ['DELETE'], true, false, CustomerVoter::ACCESS_ABSTAIN],
127
            [$this->getCustomer(), $className, ['ASSIGN'], true, false, CustomerVoter::ACCESS_ABSTAIN],
128
            // applicable but without origin id
129
            [$this->getCustomer(), $className, ['VIEW'], true, true, CustomerVoter::ACCESS_ABSTAIN],
130
            [$this->getCustomer(), $className, ['CREATE'], true, true, CustomerVoter::ACCESS_ABSTAIN],
131
            [$this->getCustomer(), $className, ['EDIT'], true, true, CustomerVoter::ACCESS_DENIED],
132
            [$this->getCustomer(), $className, ['DELETE'], true, true, CustomerVoter::ACCESS_ABSTAIN],
133
            [$this->getCustomer(), $className, ['ASSIGN'], true, true, CustomerVoter::ACCESS_ABSTAIN],
134
            // applicable with origin id
135
            [$this->getCustomer(1), $className, ['VIEW'], true, true, CustomerVoter::ACCESS_ABSTAIN],
136
            [$this->getCustomer(1), $className, ['CREATE'], true, true, CustomerVoter::ACCESS_ABSTAIN],
137
            [$this->getCustomer(1), $className, ['EDIT'], true, true, CustomerVoter::ACCESS_ABSTAIN],
138
            [$this->getCustomer(1), $className, ['DELETE'], true, true, CustomerVoter::ACCESS_ABSTAIN],
139
            [$this->getCustomer(1), $className, ['ASSIGN'], true, true, CustomerVoter::ACCESS_ABSTAIN],
140
            // applicable but without channels
141
            [$this->getCustomer(1), $className, ['VIEW'], false, true, CustomerVoter::ACCESS_ABSTAIN],
142
            [$this->getCustomer(1), $className, ['CREATE'], false, true, CustomerVoter::ACCESS_DENIED],
143
            [$this->getCustomer(1), $className, ['EDIT'], false, true, CustomerVoter::ACCESS_DENIED],
144
            [$this->getCustomer(1), $className, ['DELETE'], false, true, CustomerVoter::ACCESS_ABSTAIN],
145
            [$this->getCustomer(1), $className, ['ASSIGN'], false, true, CustomerVoter::ACCESS_ABSTAIN]
146
        ];
147
    }
148
149
    /**
150
     * @param string $objectIdentityClass
151
     * @param string $className
152
     *
153
     * @return \PHPUnit_Framework_MockObject_MockObject|Customer
154
     */
155 View Code Duplication
    public function getObjectIdentity($objectIdentityClass, $className)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157
        $objectIdentity = $this->getMock($objectIdentityClass);
158
        $objectIdentity->expects($this->any())
159
            ->method('getType')
160
            ->will($this->returnValue($className));
161
162
        return $objectIdentity;
163
    }
164
165
    /**
166
     * @param int $originId
167
     *
168
     * @return \PHPUnit_Framework_MockObject_MockObject|Customer
169
     */
170
    protected function getCustomer($originId = null)
171
    {
172
        $customer = $this->getMock('OroCRM\Bundle\MagentoBundle\Entity\Customer');
173
        $channel = $this->getMock('Oro\Bundle\IntegrationBundle\Entity\Channel');
174
        $customer->expects($this->any())
175
            ->method('getChannel')
176
            ->will($this->returnValue($channel));
177
178
        if ($originId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $originId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
179
            $customer->expects($this->any())
180
                ->method('getOriginId')
181
                ->will($this->returnValue($originId));
182
        }
183
184
        return $customer;
185
    }
186
}
187