1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace prgTW\BaseCRM\Tests\Service; |
4
|
|
|
|
5
|
|
|
use prgTW\BaseCRM\BaseCrm; |
6
|
|
|
use prgTW\BaseCRM\Client\GuzzleClient; |
7
|
|
|
use prgTW\BaseCRM\Resource\CustomFieldsCollection; |
8
|
|
|
use prgTW\BaseCRM\Resource\Resource; |
9
|
|
|
use prgTW\BaseCRM\Service\Detached\Source as DetachedSource; |
10
|
|
|
use prgTW\BaseCRM\Service\Source; |
11
|
|
|
use prgTW\BaseCRM\Tests\AbstractTest; |
12
|
|
|
|
13
|
|
|
class SourcesTest extends AbstractTest |
14
|
|
|
{ |
15
|
|
|
public function testAll() |
16
|
|
|
{ |
17
|
|
|
$client = \Mockery::mock(GuzzleClient::class); |
18
|
|
|
$client |
19
|
|
|
->shouldReceive('request') |
20
|
|
|
->once() |
21
|
|
|
->with('GET', sprintf('%s/%s/sources.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery()) |
22
|
|
|
->andReturn($this->getResponse(200, ' |
23
|
|
|
[ |
24
|
|
|
{ |
25
|
|
|
"source": { |
26
|
|
|
"name": "test", |
27
|
|
|
"id": 1 |
28
|
|
|
} |
29
|
|
|
}, |
30
|
|
|
{ |
31
|
|
|
"source": { |
32
|
|
|
"name": "test", |
33
|
|
|
"id": 2 |
34
|
|
|
} |
35
|
|
|
}, |
36
|
|
|
{ |
37
|
|
|
"source": { |
38
|
|
|
"name": "test", |
39
|
|
|
"id": 3 |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
] |
43
|
|
|
')); |
44
|
|
|
$baseCrm = new BaseCrm('', $client); |
45
|
|
|
$sources = $baseCrm->getSources(); |
46
|
|
|
$this->assertCount(3, $sources); |
47
|
|
|
|
48
|
|
|
$i = 1; |
49
|
|
|
/** @var Source $source */ |
50
|
|
|
foreach ($sources as $source) |
51
|
|
|
{ |
52
|
|
|
$this->assertInstanceOf(Source::class, $source); |
53
|
|
|
$this->assertEquals($i, $source->id); |
54
|
|
|
$this->assertEquals('test', $source->name); |
55
|
|
|
|
56
|
|
|
$client |
57
|
|
|
->shouldReceive('request') |
58
|
|
|
->once() |
59
|
|
|
->with('PUT', sprintf('%s/%s/sources/%d.json', Resource::ENDPOINT_SALES, Resource::PREFIX, $source->id), $this->getQuery([ |
|
|
|
|
60
|
|
|
'query' => [ |
61
|
|
|
'source' => [ |
62
|
|
|
'name' => 'test', |
63
|
|
|
], |
64
|
|
|
], |
65
|
|
|
])) |
66
|
|
|
->andReturn($this->getResponse(200, ' |
67
|
|
|
{ |
68
|
|
|
"source": { |
69
|
|
|
"name": "test", |
70
|
|
|
"id": ' . $i . ' |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
')); |
74
|
|
|
$source->save([ |
75
|
|
|
'name', |
76
|
|
|
]); |
77
|
|
|
++$i; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testAllWithOther() |
82
|
|
|
{ |
83
|
|
|
$client = \Mockery::mock(GuzzleClient::class); |
84
|
|
|
$client |
85
|
|
|
->shouldReceive('request') |
86
|
|
|
->once() |
87
|
|
|
->with('GET', sprintf('%s/%s/sources.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery([ |
88
|
|
|
'query' => [ |
89
|
|
|
'other' => 1, |
90
|
|
|
] |
91
|
|
|
])) |
92
|
|
|
->andReturn($this->getResponse(200, ' |
93
|
|
|
[ |
94
|
|
|
{ |
95
|
|
|
"source": { |
96
|
|
|
"name": "test", |
97
|
|
|
"id": 1 |
98
|
|
|
} |
99
|
|
|
}, |
100
|
|
|
{ |
101
|
|
|
"source": { |
102
|
|
|
"name": "test", |
103
|
|
|
"id": 2 |
104
|
|
|
} |
105
|
|
|
}, |
106
|
|
|
{ |
107
|
|
|
"source": { |
108
|
|
|
"name": "test", |
109
|
|
|
"id": 3 |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
] |
113
|
|
|
')); |
114
|
|
|
$baseCrm = new BaseCrm('', $client); |
115
|
|
|
$baseCrm->getSources()->all(true); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testGet() |
119
|
|
|
{ |
120
|
|
|
$client = \Mockery::mock(GuzzleClient::class); |
121
|
|
|
$client |
122
|
|
|
->shouldReceive('request') |
123
|
|
|
->once() |
124
|
|
|
->with('GET', sprintf('%s/%s/sources/123.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery()) |
125
|
|
|
->andReturn($this->getResponse(200, ' |
126
|
|
|
{ |
127
|
|
|
"source": { |
128
|
|
|
"name": "test", |
129
|
|
|
"id": 123 |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
')); |
133
|
|
|
$baseCrm = new BaseCrm('', $client); |
134
|
|
|
$sources = $baseCrm->getSources(); |
135
|
|
|
/** @var Source $source */ |
136
|
|
|
$source = $sources->get(123); |
137
|
|
|
|
138
|
|
|
$this->assertInstanceOf(Source::class, $source); |
139
|
|
|
$this->assertEquals(123, $source->id); |
|
|
|
|
140
|
|
|
$this->assertEquals('test', $source->name); |
|
|
|
|
141
|
|
|
|
142
|
|
|
$client |
143
|
|
|
->shouldReceive('request') |
144
|
|
|
->once() |
145
|
|
|
->with('PUT', sprintf('%s/%s/sources/123.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery([ |
146
|
|
|
'query' => [ |
147
|
|
|
'source' => [ |
148
|
|
|
'name' => 'modified', |
149
|
|
|
] |
150
|
|
|
], |
151
|
|
|
])) |
152
|
|
|
->andReturn($this->getResponse(200, ' |
153
|
|
|
{ |
154
|
|
|
"source": { |
155
|
|
|
"name": "modified", |
156
|
|
|
"id": 123 |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
')); |
160
|
|
|
|
161
|
|
|
$source->name = 'modified'; |
|
|
|
|
162
|
|
|
$source->save([ |
163
|
|
|
'name', |
164
|
|
|
]); |
165
|
|
|
$this->assertEquals('modified', $source->name); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testCreate() |
169
|
|
|
{ |
170
|
|
|
$client = \Mockery::mock(GuzzleClient::class); |
171
|
|
|
$client |
172
|
|
|
->shouldReceive('request') |
173
|
|
|
->once() |
174
|
|
|
->with('POST', sprintf('%s/%s/sources.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery([ |
175
|
|
|
'query' => [ |
176
|
|
|
'source' => [ |
177
|
|
|
'name' => 'test', |
178
|
|
|
'custom_field_values' => [], |
179
|
|
|
], |
180
|
|
|
], |
181
|
|
|
])) |
182
|
|
|
->andReturn($this->getResponse(200, ' |
183
|
|
|
{ |
184
|
|
|
"source": { |
185
|
|
|
"name": "test", |
186
|
|
|
"id": 405 |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
')); |
190
|
|
|
$baseCrm = new BaseCrm('', $client); |
191
|
|
|
$sources = $baseCrm->getSources(); |
192
|
|
|
/** @var Source $source */ |
193
|
|
|
$newSource = (new DetachedSource); |
194
|
|
|
$newSource->name = 'test'; |
|
|
|
|
195
|
|
|
$source = $sources->create($newSource, [ |
196
|
|
|
'name', |
197
|
|
|
]); |
198
|
|
|
$this->assertInstanceOf(Source::class, $source); |
199
|
|
|
$this->assertEquals(405, $source->id); |
200
|
|
|
$this->assertEquals('test', $source->name); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function testDelete() |
204
|
|
|
{ |
205
|
|
|
$client = \Mockery::mock(GuzzleClient::class); |
206
|
|
|
$client |
207
|
|
|
->shouldReceive('request') |
208
|
|
|
->once() |
209
|
|
|
->with('DELETE', sprintf('%s/%s/sources/123.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery()) |
210
|
|
|
->andReturn($this->getResponse(200, '')); |
211
|
|
|
$baseCrm = new BaseCrm('', $client); |
212
|
|
|
$sources = $baseCrm->getSources(); |
213
|
|
|
$result = $sources->delete(123); |
214
|
|
|
$this->assertTrue($result); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function testCustomFields() |
218
|
|
|
{ |
219
|
|
|
$client = \Mockery::mock(GuzzleClient::class); |
220
|
|
|
$client |
221
|
|
|
->shouldReceive('request') |
222
|
|
|
->once() |
223
|
|
|
->with('GET', sprintf('%s/%s/sources.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery()) |
224
|
|
|
->andReturn($this->getResponse(200, ' |
225
|
|
|
[ |
226
|
|
|
{ |
227
|
|
|
"source": { |
228
|
|
|
"name": "test", |
229
|
|
|
"id": 1, |
230
|
|
|
"custom_fields": [] |
231
|
|
|
} |
232
|
|
|
}, |
233
|
|
|
{ |
234
|
|
|
"source": { |
235
|
|
|
"name": "test", |
236
|
|
|
"id": 2, |
237
|
|
|
"custom_fields": { |
238
|
|
|
"custom1": { |
239
|
|
|
"id": null |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
}, |
244
|
|
|
{ |
245
|
|
|
"source": { |
246
|
|
|
"name": "test", |
247
|
|
|
"id": 3, |
248
|
|
|
"custom_fields": { |
249
|
|
|
"custom1": { |
250
|
|
|
"id": null |
251
|
|
|
}, |
252
|
|
|
"custom2": { |
253
|
|
|
"id": 12345, |
254
|
|
|
"value": "some value" |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
] |
260
|
|
|
')); |
261
|
|
|
$baseCrm = new BaseCrm('', $client); |
262
|
|
|
$sources = $baseCrm->getSources()->all(); |
263
|
|
|
|
264
|
|
|
$i = 1; |
265
|
|
|
/** @var Source $source */ |
266
|
|
|
foreach ($sources as $source) |
267
|
|
|
{ |
268
|
|
|
$customFieldsCollection = $source->getCustomFields(); |
|
|
|
|
269
|
|
|
$this->assertInstanceOf(CustomFieldsCollection::class, $customFieldsCollection); |
270
|
|
|
switch ($i) |
271
|
|
|
{ |
272
|
|
|
case 1: |
273
|
|
|
$this->assertCount(0, $customFieldsCollection); |
274
|
|
|
$this->assertArrayNotHasKey('custom1', $customFieldsCollection); |
275
|
|
|
$this->assertEquals([], $customFieldsCollection->toArray()); |
276
|
|
|
break; |
277
|
|
|
|
278
|
|
|
case 2: |
279
|
|
|
$this->assertCount(1, $customFieldsCollection); |
280
|
|
|
$this->assertArrayHasKey('custom1', $customFieldsCollection); |
281
|
|
|
$this->assertNull($customFieldsCollection['custom1']->getId()); |
282
|
|
|
$this->assertArrayNotHasKey('custom2', $customFieldsCollection); |
283
|
|
|
$this->assertEquals(['custom1' => null], $customFieldsCollection->toArray()); |
284
|
|
|
break; |
285
|
|
|
|
286
|
|
|
case 3: |
287
|
|
|
$this->assertCount(2, $customFieldsCollection); |
288
|
|
|
$this->assertArrayHasKey('custom1', $customFieldsCollection); |
289
|
|
|
$this->assertArrayHasKey('custom2', $customFieldsCollection); |
290
|
|
|
$this->assertEquals('custom2', $customFieldsCollection['custom2']->getName()); |
291
|
|
|
$this->assertEquals('12345', $customFieldsCollection['custom2']->getId()); |
292
|
|
|
$this->assertEquals('some value', $customFieldsCollection['custom2']->getValue()); |
293
|
|
|
$this->assertEquals(['custom1' => null, 'custom2' => 'some value'], $customFieldsCollection->toArray()); |
294
|
|
|
break; |
295
|
|
|
} |
296
|
|
|
++$i; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
public function testLazyLoadingWithCustomFields() |
301
|
|
|
{ |
302
|
|
|
$client = \Mockery::mock(GuzzleClient::class); |
303
|
|
|
$baseCrm = new BaseCrm('', $client); |
304
|
|
|
/** @var Source $source */ |
305
|
|
|
$source = $baseCrm->getSources()->get(123); |
306
|
|
|
|
307
|
|
|
$client |
308
|
|
|
->shouldReceive('request') |
309
|
|
|
->once() |
310
|
|
|
->with('GET', sprintf('%s/%s/sources/123.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery()) |
311
|
|
|
->andReturn($this->getResponse(200, ' |
312
|
|
|
{ |
313
|
|
|
"source": { |
314
|
|
|
"name": "test", |
315
|
|
|
"id": 123, |
316
|
|
|
"custom_fields": { |
317
|
|
|
"custom1": { |
318
|
|
|
"id": null |
319
|
|
|
}, |
320
|
|
|
"custom2": { |
321
|
|
|
"id": 12345, |
322
|
|
|
"value": "some value" |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
')); |
328
|
|
|
|
329
|
|
|
$this->assertTrue($source->hasCustomField('custom1')); |
330
|
|
|
$this->assertTrue($source->hasCustomField('custom2')); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function testSavingCustomFields() |
334
|
|
|
{ |
335
|
|
|
$client = \Mockery::mock(GuzzleClient::class); |
336
|
|
|
$baseCrm = new BaseCrm('', $client); |
337
|
|
|
/** @var Source $source */ |
338
|
|
|
$source = $baseCrm->getSources()->get(123); |
339
|
|
|
|
340
|
|
|
$client |
341
|
|
|
->shouldReceive('request') |
342
|
|
|
->once() |
343
|
|
|
->with('GET', sprintf('%s/%s/sources/123.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery()) |
344
|
|
|
->andReturn($this->getResponse(200, ' |
345
|
|
|
{ |
346
|
|
|
"source": { |
347
|
|
|
"name": "test", |
348
|
|
|
"id": 123, |
349
|
|
|
"custom_fields": { |
350
|
|
|
"custom1": { |
351
|
|
|
"id": null |
352
|
|
|
}, |
353
|
|
|
"custom2": { |
354
|
|
|
"id": 12345, |
355
|
|
|
"value": "some value" |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
')); |
361
|
|
|
|
362
|
|
|
$source->setCustomField('custom1', 'new_value1'); |
363
|
|
|
$source->setCustomField('custom2', 'new_value2'); |
364
|
|
|
|
365
|
|
|
$client |
366
|
|
|
->shouldReceive('request') |
367
|
|
|
->once() |
368
|
|
|
->with('PUT', sprintf('%s/%s/sources/123.json', Resource::ENDPOINT_SALES, Resource::PREFIX), $this->getQuery([ |
369
|
|
|
'query' => [ |
370
|
|
|
'source' => [ |
371
|
|
|
'name' => 'test', |
372
|
|
|
'custom_field_values' => [ |
373
|
|
|
'custom1' => 'new_value1', |
374
|
|
|
'custom2' => 'new_value2', |
375
|
|
|
], |
376
|
|
|
], |
377
|
|
|
], |
378
|
|
|
])) |
379
|
|
|
->andReturn($this->getResponse(200, ' |
380
|
|
|
{ |
381
|
|
|
"source": { |
382
|
|
|
"name": "test", |
383
|
|
|
"id": 123, |
384
|
|
|
"custom_fields": { |
385
|
|
|
"custom1": { |
386
|
|
|
"id": 123, |
387
|
|
|
"value": "new_value1" |
388
|
|
|
}, |
389
|
|
|
"custom2": { |
390
|
|
|
"id": 456, |
391
|
|
|
"value": "new_value2" |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
')); |
397
|
|
|
|
398
|
|
|
$source->save(); |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.