Completed
Push — master ( 93694a...685540 )
by
unknown
09:24
created

testForecastOfOpportunitiesValuesWithCompareDate()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 43
rs 8.8571
cc 2
eloc 29
nc 1
nop 0
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 $securityFacade;
56
57
    protected function setUp()
58
    {
59
        $opportunityRepository = 'OroCRM\Bundle\SalesBundle\Entity\Repository\OpportunityRepository';
60
        $this->opportunityRepository = $this->getMockBuilder($opportunityRepository)
61
            ->disableOriginalConstructor()
62
            ->getMock();
63
64
        $businessUnitRepository = 'Oro\Bundle\OrganizationBundle\Entity\Repository\BusinessUnitRepository';
65
        $this->businessUnitRepository = $this->getMockBuilder($businessUnitRepository)
66
            ->setMethods(['findById'])
67
            ->disableOriginalConstructor()
68
            ->getMock();
69
70
        $this->doctrine = $this->getMockBuilder('Doctrine\Bundle\DoctrineBundle\Registry')
71
            ->disableOriginalConstructor()
72
            ->getMock();
73
74
        $doctrineResult = function ($repository) {
75
            if ($repository == 'OroCRMSalesBundle:Opportunity') {
76
                return $this->opportunityRepository;
77
            } elseif ($repository == 'OroOrganizationBundle:BusinessUnit') {
78
                return $this->businessUnitRepository;
79
            }
80
81
            return null;
82
        };
83
84
        $this->doctrine->expects($this->any())
85
            ->method('getRepository')
86
            ->will($this->returnCallback($doctrineResult));
87
88
        $this->translator = $this->getMockBuilder('Oro\Bundle\TranslationBundle\Translation\Translator')
89
            ->disableOriginalConstructor()
90
            ->getMock();
91
92
        $this->numberFormatter = $this->getMockBuilder('Oro\Bundle\LocaleBundle\Formatter\NumberFormatter')
93
            ->disableOriginalConstructor()
94
            ->getMock();
95
96
        $this->numberFormatter
97
            ->expects($this->any())
98
            ->method($this->anything())
99
            ->withAnyParameters()
100
            ->will($this->returnArgument(0));
101
102
        $this->dateTimeFormatter = $this->getMockBuilder('Oro\Bundle\LocaleBundle\Formatter\DateTimeFormatter')
103
            ->disableOriginalConstructor()
104
            ->getMock();
105
106
        $this->dateTimeFormatter
107
            ->expects($this->any())
108
            ->method($this->anything())
109
            ->withAnyParameters()
110
            ->will($this->returnArgument(0));
111
112
        $this->aclHelper = $this->getMockBuilder('Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper')
113
            ->disableOriginalConstructor()
114
            ->getMock();
115
116
        $this->securityFacade = $this->getMockBuilder('Oro\Bundle\SecurityBundle\SecurityFacade')
117
            ->disableOriginalConstructor()
118
            ->getMock();
119
120
        $this->provider = new ForecastOfOpportunities(
121
            $this->doctrine,
122
            $this->numberFormatter,
123
            $this->dateTimeFormatter,
124
            $this->aclHelper,
125
            $this->translator,
126
            $this->securityFacade
127
        );
128
    }
129
130
    public function tearDown()
131
    {
132
        unset(
133
            $this->doctrine,
134
            $this->numberFormatter,
135
            $this->dateTimeFormatter,
136
            $this->aclHelper,
137
            $this->translator,
138
            $this->securityFacade
139
        );
140
    }
141
142
    public function testForecastOfOpportunitiesValuesWithUserAutoFill()
143
    {
144
        $user = new User();
145
        $user->setId(1);
146
        $options = ['owners' => [], 'businessUnits' => []];
147
        $widgetOptions = new WidgetOptionBag($options);
148
149
        $this->opportunityRepository->expects($this->any())
150
            ->method('getForecastOfOpporunitiesData')
151
            ->with([$user->getId()], null, $this->aclHelper)
152
            ->will($this->returnValue(['inProgressCount' => 5, 'budgetAmount' => 1000, 'weightedForecast' => 500]));
153
154
        $this->securityFacade->expects($this->once())
155
            ->method('getLoggedUser')
156
            ->willReturn($user);
157
158
        $result = $this->provider
159
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getInProgressValues', 'integer', false);
160
        $this->assertEquals(['value' => 5], $result);
161
162
        $result = $this->provider
163
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getTotalForecastValues', 'currency', false);
164
        $this->assertEquals(['value' => 1000], $result);
165
166
        $result = $this->provider
167
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getWeightedForecastValues', 'currency', false);
168
        $this->assertEquals(['value' => 500], $result);
169
    }
170
171
    public function testForecastOfOpportunitiesValues()
172
    {
173
        $user = new User();
174
        $user->setId(1);
175
        $options = ['owners' => [$user], 'businessUnits' => []];
176
        $widgetOptions = new WidgetOptionBag($options);
177
178
        $this->opportunityRepository->expects($this->any())
179
            ->method('getForecastOfOpporunitiesData')
180
            ->with([$user->getId()], null, $this->aclHelper)
181
            ->will($this->returnValue(['inProgressCount' => 5, 'budgetAmount' => 1000, 'weightedForecast' => 500]));
182
183
        $result = $this->provider
184
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getInProgressValues', 'integer', false);
185
        $this->assertEquals(['value' => 5], $result);
186
187
        $result = $this->provider
188
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getTotalForecastValues', 'currency', false);
189
        $this->assertEquals(['value' => 1000], $result);
190
191
        $result = $this->provider
192
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getWeightedForecastValues', 'currency', false);
193
        $this->assertEquals(['value' => 500], $result);
194
    }
195
196
    /**
197
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
198
     */
199
    public function testForecastOfOpportunitiesValuesWithCompareDate()
200
    {
201
        $user = new User();
202
        $user->setId(1);
203
204
        $date = '2015-09-20 00:00:00.000000';
205
206
        $options = [
207
            'owners' => [$user],
208
            'businessUnits' => [],
209
            'compareToDate' => ['useDate' => true, 'date' => $date]
210
        ];
211
        $widgetOptions = new WidgetOptionBag($options);
212
213
        $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...
214
            if ($date === null) {
215
                return ['inProgressCount' => 5, 'budgetAmount' => 1000, 'weightedForecast' => 500];
216
            }
217
218
            return ['inProgressCount' => 2, 'budgetAmount' => 200, 'weightedForecast' => 50];
219
        };
220
221
        $this->opportunityRepository->expects($this->any())
222
            ->method('getForecastOfOpporunitiesData')
223
            ->with($this->logicalOr([$user->getId()], $this->logicalOr($date, null), $this->aclHelper))
224
            ->will($this->returnCallback($resultValues));
225
226
        $result = $this->provider
227
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getInProgressValues', 'integer', false);
228
229
        $expectedResult = ['value' => 5, 'deviation' => '+3 (+1.5)', 'isPositive' => true, 'previousRange' => $date];
230
        $this->assertEquals($expectedResult, $result);
231
232
        $expectedResult = ['value' => 1000, 'deviation' => '+800 (+4)', 'isPositive' => 1, 'previousRange' => $date];
233
        $result = $this->provider
234
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getTotalForecastValues', 'currency', false);
235
        $this->assertEquals($expectedResult, $result);
236
237
        $expectedResult = ['value' => 500, 'deviation' => '+450 (+9)', 'isPositive' => 1, 'previousRange' => $date];
238
        $result = $this->provider
239
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getWeightedForecastValues', 'currency', false);
240
        $this->assertEquals($expectedResult, $result);
241
    }
242
243
    public function testForecastOfOpportunitiesValuesWithBusinessUnits()
244
    {
245
        $user = new User();
246
        $user->setId(1);
247
248
        $businessUnit = new BusinessUnit();
249
        $businessUnit->addUser($user);
250
251
        $options = ['owners' => [], 'businessUnits' => [$businessUnit]];
252
        $widgetOptions = new WidgetOptionBag($options);
253
254
        $this->opportunityRepository->expects($this->any())
255
            ->method('getForecastOfOpporunitiesData')
256
            ->with([$user->getId()], null, $this->aclHelper)
257
            ->will($this->returnValue(['inProgressCount' => 5, 'budgetAmount' => 1000, 'weightedForecast' => 500]));
258
259
        $this->businessUnitRepository->expects($this->any())
260
            ->method('findById')
261
            ->will($this->returnValue([$businessUnit]));
262
263
        $result = $this->provider
264
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getInProgressValues', 'integer', false);
265
        $this->assertEquals(['value' => 5], $result);
266
267
        $result = $this->provider
268
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getTotalForecastValues', 'currency', false);
269
        $this->assertEquals(['value' => 1000], $result);
270
271
        $result = $this->provider
272
            ->getForecastOfOpportunitiesValues($widgetOptions, 'getWeightedForecastValues', 'currency', false);
273
        $this->assertEquals(['value' => 500], $result);
274
    }
275
}
276