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

NewsletterSubscriberVoterTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 229
Duplicated Lines 16.59 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 8
lcom 1
cbo 4
dl 38
loc 229
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 4 1
A setUp() 0 7 1
B testVote() 29 29 1
B attributesDataProvider() 0 115 1
A getObjectIdentity() 9 9 1
B getSubscriber() 0 25 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Entity\NewsletterSubscriber;
8
use OroCRM\Bundle\MagentoBundle\Acl\Voter\NewsletterSubscriberVoter;
9
10
class NewsletterSubscriberVoterTest extends AbstractTwoWaySyncVoterTest
11
{
12
    /**
13
     * @var NewsletterSubscriberVoter
14
     */
15
    protected $voter;
16
17
    protected function setUp()
18
    {
19
        parent::setUp();
20
21
        $this->voter = new NewsletterSubscriberVoter($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\NewsletterSubscriber');
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\NewsletterSubscriber';
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,
86
                ['VIEW'],
87
                false,
88
                false,
89
                NewsletterSubscriberVoter::ACCESS_ABSTAIN,
90
            ],
91
            [
92
                $this->getObjectIdentity($objectIdentityClass, $className),
93
                $objectIdentityClass,
94
                ['CREATE'],
95
                false,
96
                false,
97
                NewsletterSubscriberVoter::ACCESS_DENIED,
98
            ],
99
            [
100
                $this->getObjectIdentity($objectIdentityClass, $className),
101
                $objectIdentityClass,
102
                ['EDIT'],
103
                false,
104
                false,
105
                NewsletterSubscriberVoter::ACCESS_DENIED,
106
            ],
107
            [
108
                $this->getObjectIdentity($objectIdentityClass, $className),
109
                $objectIdentityClass,
110
                ['DELETE'],
111
                false,
112
                false,
113
                NewsletterSubscriberVoter::ACCESS_ABSTAIN
114
            ],
115
            [
116
                $this->getObjectIdentity($objectIdentityClass, $className),
117
                $objectIdentityClass,
118
                ['ASSIGN'],
119
                false,
120
                false,
121
                NewsletterSubscriberVoter::ACCESS_ABSTAIN
122
            ],
123
            // channel not applicable
124
            [$this->getSubscriber(), $className, ['VIEW'], true, false, NewsletterSubscriberVoter::ACCESS_ABSTAIN],
125
            [$this->getSubscriber(), $className, ['CREATE'], true, false, NewsletterSubscriberVoter::ACCESS_DENIED],
126
            [$this->getSubscriber(), $className, ['EDIT'], true, false, NewsletterSubscriberVoter::ACCESS_DENIED],
127
            [$this->getSubscriber(), $className, ['DELETE'], true, false, NewsletterSubscriberVoter::ACCESS_ABSTAIN],
128
            [$this->getSubscriber(), $className, ['ASSIGN'], true, false, NewsletterSubscriberVoter::ACCESS_ABSTAIN],
129
            // without customer
130
            [$this->getSubscriber(), $className, ['VIEW'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN],
131
            [$this->getSubscriber(), $className, ['CREATE'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN],
132
            [$this->getSubscriber(), $className, ['EDIT'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN],
133
            [$this->getSubscriber(), $className, ['DELETE'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN],
134
            [$this->getSubscriber(), $className, ['ASSIGN'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN],
135
            // with customer and without origin id
136
            [$this->getSubscriber(true), $className, ['VIEW'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN],
137
            [$this->getSubscriber(true), $className, ['CREATE'], true, true, NewsletterSubscriberVoter::ACCESS_DENIED],
138
            [$this->getSubscriber(true), $className, ['EDIT'], true, true, NewsletterSubscriberVoter::ACCESS_DENIED],
139
            [$this->getSubscriber(true), $className, ['DELETE'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN],
140
            [$this->getSubscriber(true), $className, ['ASSIGN'], true, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN],
141
            // applicable with origin id
142
            [
143
                $this->getSubscriber(true, 1),
144
                $className,
145
                ['VIEW'],
146
                true,
147
                true,
148
                NewsletterSubscriberVoter::ACCESS_ABSTAIN
149
            ],
150
            [
151
                $this->getSubscriber(true, 1),
152
                $className,
153
                ['CREATE'],
154
                true,
155
                true,
156
                NewsletterSubscriberVoter::ACCESS_ABSTAIN
157
            ],
158
            [
159
                $this->getSubscriber(true, 1),
160
                $className,
161
                ['EDIT'],
162
                true,
163
                true,
164
                NewsletterSubscriberVoter::ACCESS_ABSTAIN
165
            ],
166
            [
167
                $this->getSubscriber(true, 1),
168
                $className,
169
                ['DELETE'],
170
                true,
171
                true,
172
                NewsletterSubscriberVoter::ACCESS_ABSTAIN
173
            ],
174
            [
175
                $this->getSubscriber(true, 1),
176
                $className,
177
                ['ASSIGN'],
178
                true,
179
                true,
180
                NewsletterSubscriberVoter::ACCESS_ABSTAIN
181
            ],
182
            // applicable but without channels
183
            [$this->getSubscriber(1), $className, ['VIEW'], false, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN],
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a boolean.

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...
184
            [$this->getSubscriber(1), $className, ['CREATE'], false, true, NewsletterSubscriberVoter::ACCESS_DENIED],
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a boolean.

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...
185
            [$this->getSubscriber(1), $className, ['EDIT'], false, true, NewsletterSubscriberVoter::ACCESS_DENIED],
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a boolean.

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...
186
            [$this->getSubscriber(1), $className, ['DELETE'], false, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN],
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a boolean.

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...
187
            [$this->getSubscriber(1), $className, ['ASSIGN'], false, true, NewsletterSubscriberVoter::ACCESS_ABSTAIN]
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a boolean.

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...
188
        ];
189
    }
190
191
    /**
192
     * @param string $objectIdentityClass
193
     * @param string $className
194
     *
195
     * @return \PHPUnit_Framework_MockObject_MockObject|NewsletterSubscriber
196
     */
197 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...
198
    {
199
        $objectIdentity = $this->getMock($objectIdentityClass);
200
        $objectIdentity->expects($this->any())
201
            ->method('getType')
202
            ->will($this->returnValue($className));
203
204
        return $objectIdentity;
205
    }
206
207
    /**
208
     * @param bool $hasCustomer
209
     * @param int $customerOriginId
210
     *
211
     * @return \PHPUnit_Framework_MockObject_MockObject|NewsletterSubscriber
212
     */
213
    protected function getSubscriber($hasCustomer = false, $customerOriginId = null)
214
    {
215
        $newsletterSubscriber = $this->getMock('OroCRM\Bundle\MagentoBundle\Entity\NewsletterSubscriber');
216
217
        $channel = $this->getMock('Oro\Bundle\IntegrationBundle\Entity\Channel');
218
        $newsletterSubscriber->expects($this->any())
219
            ->method('getChannel')
220
            ->will($this->returnValue($channel));
221
222
        if ($hasCustomer) {
223
            $customer = $this->getMock('OroCRM\Bundle\MagentoBundle\Entity\Customer');
224
225
            $newsletterSubscriber->expects($this->any())
226
                ->method('getCustomer')
227
                ->will($this->returnValue($customer));
228
229
            if ($customerOriginId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $customerOriginId 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...
230
                $customer->expects($this->once())
231
                    ->method('getOriginId')
232
                    ->will($this->returnValue($customerOriginId));
233
            }
234
        }
235
236
        return $newsletterSubscriber;
237
    }
238
}
239