Completed
Push — 1.9 ( 66be26...482137 )
by
unknown
62:03
created

ForecastOfOpportunitiesTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 295
Duplicated Lines 16.27 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 48
loc 295
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 70 1
A tearDown() 0 11 1
B testForecastOfOpportunitiesValuesWithUserAutoFill() 24 24 1
B testForecastOfOpportunitiesValues() 24 24 1
B testForecastOfOpportunitiesValuesWithCompareDate() 0 43 2
A testForecastOfOpportunitiesValuesWithBusinessUnits() 0 62 1

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\SalesBundle\Tests\Unit\Provider;
4
5
use Oro\Bundle\DashboardBundle\Model\WidgetOptionBag;
6
use Oro\Bundle\OrganizationBundle\Entity\BusinessUnit;
7
use OroCRM\Bundle\SalesBundle\Provider\ForecastOfOpportunities;
8
use Oro\Bundle\UserBundle\Entity\User;
9
10
class ForecastOfOpportunitiesTest extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     * @var \PHPUnit_Framework_MockObject_MockBuilder
14
     */
15
    protected $doctrine;
16
17
    /**
18
     * @var \PHPUnit_Framework_MockObject_MockBuilder
19
     */
20
    protected $translator;
21
22
    /**
23
     * @var \PHPUnit_Framework_MockObject_MockBuilder
24
     */
25
    protected $numberFormatter;
26
27
    /**
28
     * @var \PHPUnit_Framework_MockObject_MockBuilder
29
     */
30
    protected $dateTimeFormatter;
31
32
    /**
33
     * @var \PHPUnit_Framework_MockObject_MockBuilder
34
     */
35
    protected $aclHelper;
36
37
    /**
38
     * @var ForecastOfOpportunities
39
     */
40
    protected $provider;
41
42
    /**
43
     * @var \PHPUnit_Framework_MockObject_MockBuilder
44
     */
45
    protected $opportunityRepository;
46
47
    /**
48
     * @var \PHPUnit_Framework_MockObject_MockBuilder
49
     */
50
    protected $businessUnitRepository;
51
52
    /**
53
     * @var \PHPUnit_Framework_MockObject_MockBuilder
54
     */
55
    protected $userRepository;
56
57
    /**
58
     * @var \PHPUnit_Framework_MockObject_MockBuilder
59
     */
60
    protected $securityFacade;
61
62
    protected function setUp()
63
    {
64
        $opportunityRepository = 'OroCRM\Bundle\SalesBundle\Entity\Repository\OpportunityRepository';
65
        $this->opportunityRepository = $this->getMockBuilder($opportunityRepository)
66
            ->disableOriginalConstructor()
67
            ->getMock();
68
69
        $businessUnitRepository = 'Oro\Bundle\OrganizationBundle\Entity\Repository\BusinessUnitRepository';
70
        $this->businessUnitRepository = $this->getMockBuilder($businessUnitRepository)
71
            ->setMethods(['findById'])
72
            ->disableOriginalConstructor()
73
            ->getMock();
74
75
        $this->userRepository = $this->getMockBuilder('Oro\Bundle\UserBundle\Entity\Repository\UserRepository')
76
            ->disableOriginalConstructor()
77
            ->getMock();
78
79
        $this->doctrine = $this->getMockBuilder('Doctrine\Bundle\DoctrineBundle\Registry')
80
            ->disableOriginalConstructor()
81
            ->getMock();
82
83
        $this->doctrine->expects($this->any())
84
            ->method('getRepository')
85
            ->will($this->returnValueMap([
86
                ['OroCRMSalesBundle:Opportunity', null, $this->opportunityRepository],
87
                ['OroOrganizationBundle:BusinessUnit', null, $this->businessUnitRepository],
88
                ['OroUserBundle:User', null, $this->userRepository],
89
            ]));
90
91
        $this->translator = $this->getMockBuilder('Oro\Bundle\TranslationBundle\Translation\Translator')
92
            ->disableOriginalConstructor()
93
            ->getMock();
94
95
        $this->numberFormatter = $this->getMockBuilder('Oro\Bundle\LocaleBundle\Formatter\NumberFormatter')
96
            ->disableOriginalConstructor()
97
            ->getMock();
98
99
        $this->numberFormatter
100
            ->expects($this->any())
101
            ->method($this->anything())
102
            ->withAnyParameters()
103
            ->will($this->returnArgument(0));
104
105
        $this->dateTimeFormatter = $this->getMockBuilder('Oro\Bundle\LocaleBundle\Formatter\DateTimeFormatter')
106
            ->disableOriginalConstructor()
107
            ->getMock();
108
109
        $this->dateTimeFormatter
110
            ->expects($this->any())
111
            ->method($this->anything())
112
            ->withAnyParameters()
113
            ->will($this->returnArgument(0));
114
115
        $this->aclHelper = $this->getMockBuilder('Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper')
116
            ->disableOriginalConstructor()
117
            ->getMock();
118
119
        $this->securityFacade = $this->getMockBuilder('Oro\Bundle\SecurityBundle\SecurityFacade')
120
            ->disableOriginalConstructor()
121
            ->getMock();
122
123
        $this->provider = new ForecastOfOpportunities(
124
            $this->doctrine,
125
            $this->numberFormatter,
126
            $this->dateTimeFormatter,
127
            $this->aclHelper,
128
            $this->translator,
129
            $this->securityFacade
0 ignored issues
show
Unused Code introduced by
The call to ForecastOfOpportunities::__construct() has too many arguments starting with $this->securityFacade.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
130
        );
131
    }
132
133
    public function tearDown()
134
    {
135
        unset(
136
            $this->doctrine,
137
            $this->numberFormatter,
138
            $this->dateTimeFormatter,
139
            $this->aclHelper,
140
            $this->translator,
141
            $this->securityFacade
142
        );
143
    }
144
145 View Code Duplication
    public function testForecastOfOpportunitiesValuesWithUserAutoFill()
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...
146
    {
147
        $user = new User();
148
        $user->setId(1);
149
        $options = ['owners' => [], 'businessUnits' => []];
150
        $widgetOptions = new WidgetOptionBag($options);
151
152
        $this->opportunityRepository->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<PHPUnit_Framework_MockObject_MockBuilder>.

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...
153
            ->method('getForecastOfOpporunitiesData')
154
            ->with([], null, $this->aclHelper)
155
            ->will($this->returnValue(['inProgressCount' => 5, 'budgetAmount' => 1000, 'weightedForecast' => 500]));
156
157
        $result = $this->provider
158
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getInProgressValues', 'integer', false);
159
        $this->assertEquals(['value' => 5], $result);
160
161
        $result = $this->provider
162
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getTotalForecastValues', 'currency', false);
163
        $this->assertEquals(['value' => 1000], $result);
164
165
        $result = $this->provider
166
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getWeightedForecastValues', 'currency', false);
167
        $this->assertEquals(['value' => 500], $result);
168
    }
169
170 View Code Duplication
    public function testForecastOfOpportunitiesValues()
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...
171
    {
172
        $user = new User();
173
        $user->setId(1);
174
        $options = ['owners' => [$user], 'businessUnits' => []];
175
        $widgetOptions = new WidgetOptionBag($options);
176
177
        $this->opportunityRepository->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<PHPUnit_Framework_MockObject_MockBuilder>.

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...
178
            ->method('getForecastOfOpporunitiesData')
179
            ->with([$user->getId()], null, $this->aclHelper)
180
            ->will($this->returnValue(['inProgressCount' => 5, 'budgetAmount' => 1000, 'weightedForecast' => 500]));
181
182
        $result = $this->provider
183
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getInProgressValues', 'integer', false);
184
        $this->assertEquals(['value' => 5], $result);
185
186
        $result = $this->provider
187
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getTotalForecastValues', 'currency', false);
188
        $this->assertEquals(['value' => 1000], $result);
189
190
        $result = $this->provider
191
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getWeightedForecastValues', 'currency', false);
192
        $this->assertEquals(['value' => 500], $result);
193
    }
194
195
    /**
196
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
197
     */
198
    public function testForecastOfOpportunitiesValuesWithCompareDate()
199
    {
200
        $user = new User();
201
        $user->setId(1);
202
203
        $date = '2015-09-20 00:00:00.000000';
204
205
        $options = [
206
            'owners' => [$user],
207
            'businessUnits' => [],
208
            'compareToDate' => ['useDate' => true, 'date' => $date]
209
        ];
210
        $widgetOptions = new WidgetOptionBag($options);
211
212
        $resultValues = function ($users, $date, $aclHelper) {
0 ignored issues
show
Unused Code introduced by
The parameter $aclHelper is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
213
            if ($date === null) {
214
                return ['inProgressCount' => 5, 'budgetAmount' => 1000, 'weightedForecast' => 500];
215
            }
216
217
            return ['inProgressCount' => 2, 'budgetAmount' => 200, 'weightedForecast' => 50];
218
        };
219
220
        $this->opportunityRepository->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<PHPUnit_Framework_MockObject_MockBuilder>.

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...
221
            ->method('getForecastOfOpporunitiesData')
222
            ->with($this->logicalOr([$user->getId()], $this->logicalOr($date, null), $this->aclHelper))
223
            ->will($this->returnCallback($resultValues));
224
225
        $result = $this->provider
226
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getInProgressValues', 'integer', false);
227
228
        $expectedResult = ['value' => 5, 'deviation' => '+3 (+1.5)', 'isPositive' => true, 'previousRange' => $date];
229
        $this->assertEquals($expectedResult, $result);
230
231
        $expectedResult = ['value' => 1000, 'deviation' => '+800 (+4)', 'isPositive' => 1, 'previousRange' => $date];
232
        $result = $this->provider
233
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getTotalForecastValues', 'currency', false);
234
        $this->assertEquals($expectedResult, $result);
235
236
        $expectedResult = ['value' => 500, 'deviation' => '+450 (+9)', 'isPositive' => 1, 'previousRange' => $date];
237
        $result = $this->provider
238
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getWeightedForecastValues', 'currency', false);
239
        $this->assertEquals($expectedResult, $result);
240
    }
241
242
    public function testForecastOfOpportunitiesValuesWithBusinessUnits()
243
    {
244
        $user = new User();
245
        $user->setId(1);
246
247
        $businessUnit = new BusinessUnit();
248
        $businessUnit->addUser($user);
249
250
        $options = ['owners' => [], 'businessUnits' => [$businessUnit]];
251
        $widgetOptions = new WidgetOptionBag($options);
252
253
        $this->opportunityRepository->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<PHPUnit_Framework_MockObject_MockBuilder>.

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...
254
            ->method('getForecastOfOpporunitiesData')
255
            ->with([$user->getId()], null, $this->aclHelper)
256
            ->will($this->returnValue(['inProgressCount' => 5, 'budgetAmount' => 1000, 'weightedForecast' => 500]));
257
258
259
        $query = $this->getMockBuilder('Doctrine\ORM\AbstractQuery')
260
            ->disableOriginalConstructor()
261
            ->setMethods(['getResult'])
262
            ->getMockForAbstractClass();
263
264
        $expr = $this->getMockBuilder('Doctrine\ORM\Query\Expr')
265
            ->disableOriginalConstructor()
266
            ->getMock();
267
268
        $qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
269
            ->disableOriginalConstructor()
270
            ->getMock();
271
        $qb->expects($this->once())
272
            ->method('select')
273
            ->will($this->returnSelf());
274
        $qb->expects($this->any())
275
            ->method('expr')
276
            ->will($this->returnValue($expr));
277
        $qb->expects($this->once())
278
            ->method('getQuery')
279
            ->will($this->returnValue($query));
280
        $query->expects($this->once())
281
            ->method('getResult')
282
            ->will($this->returnValue([['id' => $user->getId()]]));
283
284
        $this->userRepository->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<PHPUnit_Framework_MockObject_MockBuilder>.

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...
285
            ->method('createQueryBuilder')
286
            ->will($this->returnValue($qb));
287
288
        $this->businessUnitRepository->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<PHPUnit_Framework_MockObject_MockBuilder>.

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...
289
            ->method('findById')
290
            ->will($this->returnValue([$businessUnit]));
291
292
        $result = $this->provider
293
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getInProgressValues', 'integer', false);
294
        $this->assertEquals(['value' => 5], $result);
295
296
        $result = $this->provider
297
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getTotalForecastValues', 'currency', false);
298
        $this->assertEquals(['value' => 1000], $result);
299
300
        $result = $this->provider
301
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getWeightedForecastValues', 'currency', false);
302
        $this->assertEquals(['value' => 500], $result);
303
    }
304
}
305