1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Test\Unit\Model\Service;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
/**
* Class InvoiceServiceTest
*/
class InvoiceServiceTest extends \PHPUnit\Framework\TestCase
{
/**
* Repository
*
* @var \Magento\Sales\Api\InvoiceRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $repositoryMock;
/**
* Repository
*
* @var \Magento\Sales\Api\InvoiceCommentRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $commentRepositoryMock;
/**
* Search Criteria Builder
*
* @var \Magento\Framework\Api\SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
*/
protected $searchCriteriaBuilderMock;
/**
* Filter Builder
*
* @var \Magento\Framework\Api\FilterBuilder|\PHPUnit_Framework_MockObject_MockObject
*/
protected $filterBuilderMock;
/**
* Invoice Notifier
*
* @var \Magento\Sales\Model\Order\InvoiceNotifier|\PHPUnit_Framework_MockObject_MockObject
*/
protected $invoiceNotifierMock;
/**
* @var \Magento\Sales\Model\Service\InvoiceService
*/
protected $invoiceService;
/**
* SetUp
*/
protected function setUp()
{
$objectManager = new ObjectManagerHelper($this);
$this->repositoryMock = $this->getMockForAbstractClass(
\Magento\Sales\Api\InvoiceRepositoryInterface::class,
['get'],
'',
false
);
$this->commentRepositoryMock = $this->getMockForAbstractClass(
\Magento\Sales\Api\InvoiceCommentRepositoryInterface::class,
['getList'],
'',
false
);
$this->searchCriteriaBuilderMock = $this->createPartialMock(
\Magento\Framework\Api\SearchCriteriaBuilder::class,
['create', 'addFilters']
);
$this->filterBuilderMock = $this->createPartialMock(
\Magento\Framework\Api\FilterBuilder::class,
['setField', 'setValue', 'setConditionType', 'create']
);
$this->invoiceNotifierMock = $this->createPartialMock(
\Magento\Sales\Model\Order\InvoiceNotifier::class,
['notify']
);
$this->invoiceService = $objectManager->getObject(
\Magento\Sales\Model\Service\InvoiceService::class,
[
'repository' => $this->repositoryMock,
'commentRepository' => $this->commentRepositoryMock,
'criteriaBuilder' => $this->searchCriteriaBuilderMock,
'filterBuilder' => $this->filterBuilderMock,
'notifier' => $this->invoiceNotifierMock
]
);
}
/**
* Run test setCapture method
*/
public function testSetCapture()
{
$id = 145;
$returnValue = true;
$invoiceMock = $this->createPartialMock(\Magento\Sales\Model\Order\Invoice::class, ['capture']);
$this->repositoryMock->expects($this->once())
->method('get')
->with($id)
->will($this->returnValue($invoiceMock));
$invoiceMock->expects($this->once())
->method('capture')
->will($this->returnValue($returnValue));
$this->assertTrue($this->invoiceService->setCapture($id));
}
/**
* Run test getCommentsList method
*/
public function testGetCommentsList()
{
$id = 25;
$returnValue = 'return-value';
$filterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
$searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
$this->filterBuilderMock->expects($this->once())
->method('setField')
->with('parent_id')
->will($this->returnSelf());
$this->filterBuilderMock->expects($this->once())
->method('setValue')
->with($id)
->will($this->returnSelf());
$this->filterBuilderMock->expects($this->once())
->method('setConditionType')
->with('eq')
->will($this->returnSelf());
$this->filterBuilderMock->expects($this->once())
->method('create')
->will($this->returnValue($filterMock));
$this->searchCriteriaBuilderMock->expects($this->once())
->method('addFilters')
->with([$filterMock]);
$this->searchCriteriaBuilderMock->expects($this->once())
->method('create')
->will($this->returnValue($searchCriteriaMock));
$this->commentRepositoryMock->expects($this->once())
->method('getList')
->with($searchCriteriaMock)
->will($this->returnValue($returnValue));
$this->assertEquals($returnValue, $this->invoiceService->getCommentsList($id));
}
/**
* Run test notify method
*/
public function testNotify()
{
$id = 123;
$returnValue = 'return-value';
$modelMock = $this->getMockForAbstractClass(
\Magento\Sales\Model\AbstractModel::class,
[],
'',
false
);
$this->repositoryMock->expects($this->once())
->method('get')
->with($id)
->will($this->returnValue($modelMock));
$this->invoiceNotifierMock->expects($this->once())
->method('notify')
->with($modelMock)
->will($this->returnValue($returnValue));
$this->assertEquals($returnValue, $this->invoiceService->notify($id));
}
/**
* Run test setVoid method
*/
public function testSetVoid()
{
$id = 145;
$returnValue = true;
$invoiceMock = $this->createPartialMock(\Magento\Sales\Model\Order\Invoice::class, ['void']);
$this->repositoryMock->expects($this->once())
->method('get')
->with($id)
->will($this->returnValue($invoiceMock));
$invoiceMock->expects($this->once())
->method('void')
->will($this->returnValue($returnValue));
$this->assertTrue($this->invoiceService->setVoid($id));
}
}