|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace InvoiceNinjaModuleTest\Service; |
|
5
|
|
|
|
|
6
|
|
|
use InvoiceNinjaModule\Exception\NotFoundException; |
|
7
|
|
|
use InvoiceNinjaModule\Model\Interfaces\BaseInterface; |
|
8
|
|
|
use InvoiceNinjaModule\Model\Interfaces\InvoiceInterface; |
|
9
|
|
|
use InvoiceNinjaModule\Service\Interfaces\InvoiceManagerInterface; |
|
10
|
|
|
use InvoiceNinjaModule\Service\Interfaces\ObjectServiceInterface; |
|
11
|
|
|
use InvoiceNinjaModule\Service\InvoiceManager; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
|
|
14
|
|
|
class InvoiceManagerTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var InvoiceManagerInterface */ |
|
17
|
|
|
private $invoiceManager; |
|
18
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */ |
|
19
|
|
|
private $objectManagerMock; |
|
20
|
|
|
|
|
21
|
|
|
protected function setUp() : void |
|
22
|
|
|
{ |
|
23
|
|
|
parent::setUp(); |
|
24
|
|
|
|
|
25
|
|
|
$this->objectManagerMock = $this->createMock(ObjectServiceInterface::class); |
|
26
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testCreate() : void |
|
30
|
|
|
{ |
|
31
|
|
|
self::assertInstanceOf(InvoiceManagerInterface::class, $this->invoiceManager); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testCreateClient() : void |
|
35
|
|
|
{ |
|
36
|
|
|
$invoiceMock = $this->createMock(InvoiceInterface::class); |
|
37
|
|
|
|
|
38
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
39
|
|
|
->method('createObject') |
|
40
|
|
|
->with( |
|
41
|
|
|
self::isInstanceOf(InvoiceInterface::class), |
|
42
|
|
|
self::stringContains('/invoices') |
|
43
|
|
|
) |
|
44
|
|
|
->willReturn($invoiceMock); |
|
45
|
|
|
|
|
46
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
47
|
|
|
|
|
48
|
|
|
self::assertInstanceOf(InvoiceInterface::class, $this->invoiceManager->createInvoice($invoiceMock)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testDelete() : void |
|
52
|
|
|
{ |
|
53
|
|
|
$invoiceMock = $this->createMock(InvoiceInterface::class); |
|
54
|
|
|
|
|
55
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
56
|
|
|
->method('deleteObject') |
|
57
|
|
|
->with( |
|
58
|
|
|
self::isInstanceOf(InvoiceInterface::class), |
|
59
|
|
|
self::stringContains('/invoices') |
|
60
|
|
|
) |
|
61
|
|
|
->willReturn($invoiceMock); |
|
62
|
|
|
|
|
63
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
64
|
|
|
|
|
65
|
|
|
self::assertInstanceOf(InvoiceInterface::class, $this->invoiceManager->delete($invoiceMock)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
public function testGetClientById() : void |
|
70
|
|
|
{ |
|
71
|
|
|
$invoiceMock = $this->createMock(InvoiceInterface::class); |
|
72
|
|
|
|
|
73
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
74
|
|
|
->method('getObjectById') |
|
75
|
|
|
->with( |
|
76
|
|
|
self::isInstanceOf(InvoiceInterface::class), |
|
77
|
|
|
self::isType('integer'), |
|
78
|
|
|
self::stringContains('/invoices') |
|
79
|
|
|
) |
|
80
|
|
|
->willReturn($invoiceMock); |
|
81
|
|
|
|
|
82
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
83
|
|
|
|
|
84
|
|
|
self::assertInstanceOf(InvoiceInterface::class, $this->invoiceManager->getInvoiceById(777)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\NotFoundException |
|
89
|
|
|
*/ |
|
90
|
|
|
public function testGetClientByIdException() : void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
93
|
|
|
->method('getObjectById') |
|
94
|
|
|
->with( |
|
95
|
|
|
self::isInstanceOf(InvoiceInterface::class), |
|
96
|
|
|
self::isType('integer'), |
|
97
|
|
|
self::stringContains('/invoices') |
|
98
|
|
|
) |
|
99
|
|
|
->willThrowException(new NotFoundException()); |
|
100
|
|
|
|
|
101
|
|
|
self::assertInstanceOf(InvoiceInterface::class, $this->invoiceManager->getInvoiceById(777)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testGetInvoiceByNumber() : void |
|
105
|
|
|
{ |
|
106
|
|
|
$invoiceMock = $this->createMock(InvoiceInterface::class); |
|
107
|
|
|
|
|
108
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
109
|
|
|
->method('findObjectBy') |
|
110
|
|
|
->with( |
|
111
|
|
|
self::isInstanceOf(InvoiceInterface::class), |
|
112
|
|
|
self::isType('array'), |
|
113
|
|
|
self::stringContains('/invoices') |
|
114
|
|
|
) |
|
115
|
|
|
->willReturn([$invoiceMock]); |
|
116
|
|
|
|
|
117
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
118
|
|
|
|
|
119
|
|
|
$result = $this->invoiceManager->getInvoiceByNumber('12345'); |
|
120
|
|
|
self::assertInstanceOf(InvoiceInterface::class, $result); |
|
121
|
|
|
self::assertNotEmpty($result); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\NotFoundException |
|
126
|
|
|
*/ |
|
127
|
|
|
public function testGetInvoiceByNumberNotFound() : void |
|
128
|
|
|
{ |
|
129
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
130
|
|
|
->method('findObjectBy') |
|
131
|
|
|
->with( |
|
132
|
|
|
self::isInstanceOf(InvoiceInterface::class), |
|
133
|
|
|
self::isType('array'), |
|
134
|
|
|
self::stringContains('/invoices') |
|
135
|
|
|
) |
|
136
|
|
|
->willReturn([]); |
|
137
|
|
|
|
|
138
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
139
|
|
|
|
|
140
|
|
|
$result = $this->invoiceManager->getInvoiceByNumber('12345'); |
|
141
|
|
|
self::assertInstanceOf(InvoiceInterface::class, $result); |
|
142
|
|
|
self::assertNotEmpty($result); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\InvalidResultException |
|
147
|
|
|
*/ |
|
148
|
|
|
public function testGetInvoiceByNumberInvalid() : void |
|
149
|
|
|
{ |
|
150
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
151
|
|
|
->method('findObjectBy') |
|
152
|
|
|
->with( |
|
153
|
|
|
self::isInstanceOf(InvoiceInterface::class), |
|
154
|
|
|
self::isType('array'), |
|
155
|
|
|
self::stringContains('/invoices') |
|
156
|
|
|
) |
|
157
|
|
|
->willReturn([1,2,3]); |
|
158
|
|
|
|
|
159
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
160
|
|
|
|
|
161
|
|
|
$result = $this->invoiceManager->getInvoiceByNumber('12345'); |
|
162
|
|
|
self::assertInstanceOf(InvoiceInterface::class, $result); |
|
163
|
|
|
self::assertNotEmpty($result); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function testUpdate() : void |
|
167
|
|
|
{ |
|
168
|
|
|
$invoiceMock = $this->createMock(InvoiceInterface::class); |
|
169
|
|
|
|
|
170
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
171
|
|
|
->method('updateObject') |
|
172
|
|
|
->with( |
|
173
|
|
|
self::isInstanceOf(InvoiceInterface::class), |
|
174
|
|
|
self::stringContains('/invoices') |
|
175
|
|
|
) |
|
176
|
|
|
->willReturn($invoiceMock); |
|
177
|
|
|
|
|
178
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
179
|
|
|
|
|
180
|
|
|
self::assertInstanceOf(InvoiceInterface::class, $this->invoiceManager->update($invoiceMock)); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function testRestore() : void |
|
184
|
|
|
{ |
|
185
|
|
|
$invoiceMock = $this->createMock(InvoiceInterface::class); |
|
186
|
|
|
|
|
187
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
188
|
|
|
->method('restoreObject') |
|
189
|
|
|
->with( |
|
190
|
|
|
self::isInstanceOf(InvoiceInterface::class), |
|
191
|
|
|
self::stringContains('/invoices') |
|
192
|
|
|
) |
|
193
|
|
|
->willReturn($invoiceMock); |
|
194
|
|
|
|
|
195
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
196
|
|
|
|
|
197
|
|
|
self::assertInstanceOf(InvoiceInterface::class, $this->invoiceManager->restore($invoiceMock)); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function testArchive() : void |
|
201
|
|
|
{ |
|
202
|
|
|
$invoiceMock = $this->createMock(InvoiceInterface::class); |
|
203
|
|
|
|
|
204
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
205
|
|
|
->method('archiveObject') |
|
206
|
|
|
->with( |
|
207
|
|
|
self::isInstanceOf(InvoiceInterface::class), |
|
208
|
|
|
self::stringContains('/invoices') |
|
209
|
|
|
) |
|
210
|
|
|
->willReturn($invoiceMock); |
|
211
|
|
|
|
|
212
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
213
|
|
|
|
|
214
|
|
|
self::assertInstanceOf(InvoiceInterface::class, $this->invoiceManager->archive($invoiceMock)); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function testGetAllInvoicesEmpty() : void |
|
218
|
|
|
{ |
|
219
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
220
|
|
|
->method('getAllObjects') |
|
221
|
|
|
->with( |
|
222
|
|
|
self::isInstanceOf(InvoiceInterface::class), |
|
223
|
|
|
self::stringContains('/invoices'), |
|
224
|
|
|
self::isType('integer'), |
|
225
|
|
|
self::isType('integer') |
|
226
|
|
|
) |
|
227
|
|
|
->willReturn([]); |
|
228
|
|
|
|
|
229
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
230
|
|
|
|
|
231
|
|
|
self::assertInternalType('array', $this->invoiceManager->getAllInvoices()); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
public function testGetAllInvoices() : void |
|
235
|
|
|
{ |
|
236
|
|
|
$invoiceMock = $this->createMock(InvoiceInterface::class); |
|
237
|
|
|
|
|
238
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
239
|
|
|
->method('getAllObjects') |
|
240
|
|
|
->with( |
|
241
|
|
|
self::isInstanceOf(InvoiceInterface::class), |
|
242
|
|
|
self::stringContains('/invoices'), |
|
243
|
|
|
self::isType('integer'), |
|
244
|
|
|
self::isType('integer') |
|
245
|
|
|
) |
|
246
|
|
|
->willReturn([ 'test' => $invoiceMock ]); |
|
247
|
|
|
|
|
248
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
249
|
|
|
|
|
250
|
|
|
self::assertInternalType('array', $this->invoiceManager->getAllInvoices()); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @expectedException \InvoiceNinjaModule\Exception\InvalidResultException |
|
255
|
|
|
*/ |
|
256
|
|
|
public function testGetAllInvoicesOtherResult() : void |
|
257
|
|
|
{ |
|
258
|
|
|
$invoiceMock = $this->createMock(BaseInterface::class); |
|
259
|
|
|
|
|
260
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
261
|
|
|
->method('getAllObjects') |
|
262
|
|
|
->with( |
|
263
|
|
|
self::isInstanceOf(InvoiceInterface::class), |
|
264
|
|
|
self::stringContains('/invoices'), |
|
265
|
|
|
self::isType('integer'), |
|
266
|
|
|
self::isType('integer') |
|
267
|
|
|
) |
|
268
|
|
|
->willReturn([ 'test' => $invoiceMock ]); |
|
269
|
|
|
|
|
270
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
271
|
|
|
|
|
272
|
|
|
self::assertInternalType('array', $this->invoiceManager->getAllInvoices()); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
public function testDownloadInvoice() : void |
|
277
|
|
|
{ |
|
278
|
|
|
$this->objectManagerMock->expects(self::once()) |
|
279
|
|
|
->method('downloadFile') |
|
280
|
|
|
->with(self::isType('integer')) |
|
281
|
|
|
->willReturn([ 'test' => 'test' ]); |
|
282
|
|
|
|
|
283
|
|
|
$this->invoiceManager = new InvoiceManager($this->objectManagerMock); |
|
284
|
|
|
|
|
285
|
|
|
self::assertInternalType('array', $this->invoiceManager->downloadInvoice(10)); |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|