1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\SalesBundle\Tests\Unit\Provider; |
4
|
|
|
|
5
|
|
|
use Oro\Bundle\DashboardBundle\Helper\DateHelper; |
6
|
|
|
use Oro\Bundle\DashboardBundle\Model\WidgetOptionBag; |
7
|
|
|
use Oro\Bundle\OrganizationBundle\Entity\BusinessUnit; |
8
|
|
|
use OroCRM\Bundle\SalesBundle\Provider\ForecastOfOpportunities; |
9
|
|
|
use Oro\Bundle\UserBundle\Entity\User; |
10
|
|
|
|
11
|
|
|
class ForecastOfOpportunitiesTest extends \PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \PHPUnit_Framework_MockObject_MockBuilder |
15
|
|
|
*/ |
16
|
|
|
protected $doctrine; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \PHPUnit_Framework_MockObject_MockBuilder |
20
|
|
|
*/ |
21
|
|
|
protected $translator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \PHPUnit_Framework_MockObject_MockBuilder |
25
|
|
|
*/ |
26
|
|
|
protected $numberFormatter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \PHPUnit_Framework_MockObject_MockBuilder |
30
|
|
|
*/ |
31
|
|
|
protected $dateTimeFormatter; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \PHPUnit_Framework_MockObject_MockBuilder |
35
|
|
|
*/ |
36
|
|
|
protected $aclHelper; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ForecastOfOpportunities |
40
|
|
|
*/ |
41
|
|
|
protected $provider; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \PHPUnit_Framework_MockObject_MockBuilder |
45
|
|
|
*/ |
46
|
|
|
protected $opportunityRepository; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \PHPUnit_Framework_MockObject_MockBuilder |
50
|
|
|
*/ |
51
|
|
|
protected $businessUnitRepository; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \PHPUnit_Framework_MockObject_MockBuilder |
55
|
|
|
*/ |
56
|
|
|
protected $userRepository; |
57
|
|
|
|
58
|
|
|
/** @var DateHelper|\PHPUnit_Framework_MockObject_MockBuilder */ |
59
|
|
|
protected $dateHelper; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var \PHPUnit_Framework_MockObject_MockBuilder |
63
|
|
|
*/ |
64
|
|
|
protected $ownerHelper; |
65
|
|
|
|
66
|
|
|
protected function setUp() |
67
|
|
|
{ |
68
|
|
|
$opportunityRepository = 'OroCRM\Bundle\SalesBundle\Entity\Repository\OpportunityRepository'; |
69
|
|
|
$this->opportunityRepository = $this->getMockBuilder($opportunityRepository) |
70
|
|
|
->disableOriginalConstructor() |
71
|
|
|
->getMock(); |
72
|
|
|
|
73
|
|
|
$businessUnitRepository = 'Oro\Bundle\OrganizationBundle\Entity\Repository\BusinessUnitRepository'; |
74
|
|
|
$this->businessUnitRepository = $this->getMockBuilder($businessUnitRepository) |
75
|
|
|
->setMethods(['findById']) |
76
|
|
|
->disableOriginalConstructor() |
77
|
|
|
->getMock(); |
78
|
|
|
|
79
|
|
|
$this->userRepository = $this->getMockBuilder('Oro\Bundle\UserBundle\Entity\Repository\UserRepository') |
80
|
|
|
->disableOriginalConstructor() |
81
|
|
|
->getMock(); |
82
|
|
|
|
83
|
|
|
$this->doctrine = $this->getMockBuilder('Doctrine\Bundle\DoctrineBundle\Registry') |
84
|
|
|
->disableOriginalConstructor() |
85
|
|
|
->getMock(); |
86
|
|
|
|
87
|
|
|
$this->doctrine->expects($this->any()) |
88
|
|
|
->method('getRepository') |
89
|
|
|
->will($this->returnValueMap([ |
90
|
|
|
['OroCRMSalesBundle:Opportunity', null, $this->opportunityRepository], |
91
|
|
|
['OroOrganizationBundle:BusinessUnit', null, $this->businessUnitRepository], |
92
|
|
|
['OroUserBundle:User', null, $this->userRepository], |
93
|
|
|
])); |
94
|
|
|
|
95
|
|
|
$this->translator = $this->getMockBuilder('Oro\Bundle\TranslationBundle\Translation\Translator') |
96
|
|
|
->disableOriginalConstructor() |
97
|
|
|
->getMock(); |
98
|
|
|
|
99
|
|
|
$this->numberFormatter = $this->getMockBuilder('Oro\Bundle\LocaleBundle\Formatter\NumberFormatter') |
100
|
|
|
->disableOriginalConstructor() |
101
|
|
|
->getMock(); |
102
|
|
|
|
103
|
|
|
$this->numberFormatter |
104
|
|
|
->expects($this->any()) |
105
|
|
|
->method($this->anything()) |
106
|
|
|
->withAnyParameters() |
107
|
|
|
->will($this->returnArgument(0)); |
108
|
|
|
|
109
|
|
|
$this->dateTimeFormatter = $this->getMockBuilder('Oro\Bundle\LocaleBundle\Formatter\DateTimeFormatter') |
110
|
|
|
->disableOriginalConstructor() |
111
|
|
|
->getMock(); |
112
|
|
|
|
113
|
|
|
$this->dateTimeFormatter |
114
|
|
|
->expects($this->any()) |
115
|
|
|
->method($this->anything()) |
116
|
|
|
->withAnyParameters() |
117
|
|
|
->will($this->returnArgument(0)); |
118
|
|
|
|
119
|
|
|
$this->aclHelper = $this->getMockBuilder('Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper') |
120
|
|
|
->disableOriginalConstructor() |
121
|
|
|
->getMock(); |
122
|
|
|
|
123
|
|
|
$this->dateHelper = $this->getMockBuilder('Oro\Bundle\DashboardBundle\Helper\DateHelper') |
124
|
|
|
->disableOriginalConstructor() |
125
|
|
|
->getMock(); |
126
|
|
|
|
127
|
|
|
$this->ownerHelper = $this->getMockBuilder('\Oro\Bundle\UserBundle\Dashboard\OwnerHelper') |
128
|
|
|
->disableOriginalConstructor() |
129
|
|
|
->getMock(); |
130
|
|
|
|
131
|
|
|
$this->provider = new ForecastOfOpportunities( |
132
|
|
|
$this->doctrine, |
133
|
|
|
$this->numberFormatter, |
134
|
|
|
$this->dateTimeFormatter, |
135
|
|
|
$this->aclHelper, |
136
|
|
|
$this->translator, |
137
|
|
|
$this->dateHelper, |
138
|
|
|
$this->ownerHelper |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function tearDown() |
143
|
|
|
{ |
144
|
|
|
unset( |
145
|
|
|
$this->doctrine, |
146
|
|
|
$this->numberFormatter, |
147
|
|
|
$this->dateTimeFormatter, |
148
|
|
|
$this->aclHelper, |
149
|
|
|
$this->translator, |
150
|
|
|
$this->dateHelper, |
151
|
|
|
$this->ownerHelper |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testForecastOfOpportunitiesValuesWithUserAutoFill() |
156
|
|
|
{ |
157
|
|
|
$user = new User(); |
158
|
|
|
$user->setId(1); |
159
|
|
|
$options = [ |
160
|
|
|
'owners' => ['users' => [], 'businessUnits' => [], 'roles' => []], |
161
|
|
|
'dateRange' => ['start' => null, 'end' => null] |
162
|
|
|
]; |
163
|
|
|
$widgetOptions = new WidgetOptionBag($options); |
164
|
|
|
$this->ownerHelper->expects($this->any()) |
165
|
|
|
->method('getOwnerIds') |
166
|
|
|
->willReturn([]); |
167
|
|
|
|
168
|
|
|
$this->opportunityRepository->expects($this->any()) |
169
|
|
|
->method('getForecastOfOpportunitiesData') |
170
|
|
|
->with([], null, $this->aclHelper) |
171
|
|
|
->will($this->returnValue(['inProgressCount' => 5, 'budgetAmount' => 1000, 'weightedForecast' => 500])); |
172
|
|
|
|
173
|
|
|
$result = $this->provider |
174
|
|
|
->getForecastOfOpportunitiesValues($widgetOptions, 'getInProgressValues', 'integer', false); |
175
|
|
|
$this->assertEquals(['value' => 5], $result); |
176
|
|
|
|
177
|
|
|
$result = $this->provider |
178
|
|
|
->getForecastOfOpportunitiesValues($widgetOptions, 'getTotalForecastValues', 'currency', false); |
179
|
|
|
$this->assertEquals(['value' => 1000], $result); |
180
|
|
|
|
181
|
|
|
$result = $this->provider |
182
|
|
|
->getForecastOfOpportunitiesValues($widgetOptions, 'getWeightedForecastValues', 'currency', false); |
183
|
|
|
$this->assertEquals(['value' => 500], $result); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testForecastOfOpportunitiesValues() |
187
|
|
|
{ |
188
|
|
|
$user = new User(); |
189
|
|
|
$user->setId(1); |
190
|
|
|
$options = [ |
191
|
|
|
'owners' => ['users' => [$user->getId()], 'businessUnits' => [], 'roles' => []], |
192
|
|
|
'dateRange' => ['start' => null, 'end' => null] |
193
|
|
|
]; |
194
|
|
|
$widgetOptions = new WidgetOptionBag($options); |
195
|
|
|
$this->ownerHelper->expects($this->any()) |
196
|
|
|
->method('getOwnerIds') |
197
|
|
|
->willReturn([$user->getId()]); |
198
|
|
|
|
199
|
|
|
$this->opportunityRepository->expects($this->any()) |
200
|
|
|
->method('getForecastOfOpportunitiesData') |
201
|
|
|
->with([$user->getId()], null, $this->aclHelper) |
202
|
|
|
->will($this->returnValue(['inProgressCount' => 5, 'budgetAmount' => 1000, 'weightedForecast' => 500])); |
203
|
|
|
|
204
|
|
|
$result = $this->provider |
205
|
|
|
->getForecastOfOpportunitiesValues($widgetOptions, 'getInProgressValues', 'integer', false); |
206
|
|
|
$this->assertEquals(['value' => 5], $result); |
207
|
|
|
|
208
|
|
|
$result = $this->provider |
209
|
|
|
->getForecastOfOpportunitiesValues($widgetOptions, 'getTotalForecastValues', 'currency', false); |
210
|
|
|
$this->assertEquals(['value' => 1000], $result); |
211
|
|
|
|
212
|
|
|
$result = $this->provider |
213
|
|
|
->getForecastOfOpportunitiesValues($widgetOptions, 'getWeightedForecastValues', 'currency', false); |
214
|
|
|
$this->assertEquals(['value' => 500], $result); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
219
|
|
|
*/ |
220
|
|
|
public function testForecastOfOpportunitiesValuesWithCompareDate() |
221
|
|
|
{ |
222
|
|
|
$this->markTestSkipped('Skipped until CRM-5569 and CRM-5565 will be done'); |
223
|
|
|
$user = new User(); |
224
|
|
|
$user->setId(1); |
225
|
|
|
|
226
|
|
|
$date = '2015-09-20 00:00:00.000000'; |
227
|
|
|
|
228
|
|
|
$options = [ |
229
|
|
|
'owners' => ['users' => [$user->getId()]], |
230
|
|
|
'businessUnits' => [], |
231
|
|
|
'compareToDate' => ['useDate' => true, 'date' => $date] |
232
|
|
|
]; |
233
|
|
|
$widgetOptions = new WidgetOptionBag($options); |
234
|
|
|
$this->ownerHelper->expects($this->any()) |
235
|
|
|
->method('getOwnerIds') |
236
|
|
|
->willReturn([$user->getId()]); |
237
|
|
|
|
238
|
|
|
$resultValues = function ($users, $date, $aclHelper) { |
|
|
|
|
239
|
|
|
if ($date === null) { |
240
|
|
|
return ['inProgressCount' => 5, 'budgetAmount' => 1000, 'weightedForecast' => 500]; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return ['inProgressCount' => 2, 'budgetAmount' => 200, 'weightedForecast' => 50]; |
244
|
|
|
}; |
245
|
|
|
|
246
|
|
|
$this->opportunityRepository->expects($this->any()) |
247
|
|
|
->method('getForecastOfOpportunitiesData') |
248
|
|
|
->with($this->logicalOr([$user->getId()], $this->logicalOr($date, null), $this->aclHelper)) |
249
|
|
|
->will($this->returnCallback($resultValues)); |
250
|
|
|
|
251
|
|
|
$result = $this->provider |
252
|
|
|
->getForecastOfOpportunitiesValues($widgetOptions, 'getInProgressValues', 'integer', false); |
253
|
|
|
|
254
|
|
|
$expectedResult = ['value' => 5, 'deviation' => '+3 (+1.5)', 'isPositive' => true, 'previousRange' => $date]; |
255
|
|
|
$this->assertEquals($expectedResult, $result); |
256
|
|
|
|
257
|
|
|
$expectedResult = ['value' => 1000, 'deviation' => '+800 (+4)', 'isPositive' => 1, 'previousRange' => $date]; |
258
|
|
|
$result = $this->provider |
259
|
|
|
->getForecastOfOpportunitiesValues($widgetOptions, 'getTotalForecastValues', 'currency', false); |
260
|
|
|
$this->assertEquals($expectedResult, $result); |
261
|
|
|
|
262
|
|
|
$expectedResult = ['value' => 500, 'deviation' => '+450 (+9)', 'isPositive' => 1, 'previousRange' => $date]; |
263
|
|
|
$result = $this->provider |
264
|
|
|
->getForecastOfOpportunitiesValues($widgetOptions, 'getWeightedForecastValues', 'currency', false); |
265
|
|
|
$this->assertEquals($expectedResult, $result); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function testForecastOfOpportunitiesValuesWithBusinessUnits() |
269
|
|
|
{ |
270
|
|
|
$user = new User(); |
271
|
|
|
$user->setId(1); |
272
|
|
|
|
273
|
|
|
$businessUnit = new BusinessUnit(); |
274
|
|
|
$businessUnit->addUser($user); |
275
|
|
|
|
276
|
|
|
$options = [ |
277
|
|
|
'owners' => ['businessUnits' => [$businessUnit->getId()]], |
278
|
|
|
'dateRange' => ['start' => null, 'end' => null] |
279
|
|
|
]; |
280
|
|
|
$widgetOptions = new WidgetOptionBag($options); |
281
|
|
|
$this->ownerHelper->expects($this->any()) |
282
|
|
|
->method('getOwnerIds') |
283
|
|
|
->willReturn([$user->getId()]); |
284
|
|
|
|
285
|
|
|
$this->opportunityRepository->expects($this->any()) |
286
|
|
|
->method('getForecastOfOpportunitiesData') |
287
|
|
|
->with([$user->getId()], null, $this->aclHelper) |
288
|
|
|
->will($this->returnValue(['inProgressCount' => 5, 'budgetAmount' => 1000, 'weightedForecast' => 500])); |
289
|
|
|
|
290
|
|
|
$this->businessUnitRepository->expects($this->any()) |
291
|
|
|
->method('findById') |
292
|
|
|
->will($this->returnValue([$businessUnit])); |
293
|
|
|
|
294
|
|
|
$result = $this->provider |
295
|
|
|
->getForecastOfOpportunitiesValues($widgetOptions, 'getInProgressValues', 'integer', false); |
296
|
|
|
$this->assertEquals(['value' => 5], $result); |
297
|
|
|
|
298
|
|
|
$result = $this->provider |
299
|
|
|
->getForecastOfOpportunitiesValues($widgetOptions, 'getTotalForecastValues', 'currency', false); |
300
|
|
|
$this->assertEquals(['value' => 1000], $result); |
301
|
|
|
|
302
|
|
|
$result = $this->provider |
303
|
|
|
->getForecastOfOpportunitiesValues($widgetOptions, 'getWeightedForecastValues', 'currency', false); |
304
|
|
|
$this->assertEquals(['value' => 500], $result); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.