1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace CommerceLeague\ActiveCampaign\Test\Unit\MessageQueue; |
7
|
|
|
|
8
|
|
|
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface; |
9
|
|
|
use CommerceLeague\ActiveCampaign\Api\Data\ContactInterface; |
10
|
|
|
use CommerceLeague\ActiveCampaign\Logger\Logger; |
11
|
|
|
use CommerceLeague\ActiveCampaign\MessageQueue\SyncContactConsumer; |
12
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\ContactApiResourceInterface; |
13
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\HttpException; |
14
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use CommerceLeague\ActiveCampaign\Helper\Client as ClientHelper; |
17
|
|
|
|
18
|
|
|
class SyncContactConsumerTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var MockObject|ContactRepositoryInterface |
22
|
|
|
*/ |
23
|
|
|
protected $contactRepository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var MockObject|Logger |
27
|
|
|
*/ |
28
|
|
|
protected $logger; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var MockObject|ClientHelper |
32
|
|
|
*/ |
33
|
|
|
protected $clientHelper; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var MockObject|ContactInterface |
37
|
|
|
*/ |
38
|
|
|
protected $contact; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var MockObject|ContactApiResourceInterface |
42
|
|
|
*/ |
43
|
|
|
protected $contactApi; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var SyncContactConsumer |
47
|
|
|
*/ |
48
|
|
|
protected $syncContactConsumer; |
49
|
|
|
|
50
|
|
|
protected function setUp() |
51
|
|
|
{ |
52
|
|
|
$this->contactRepository = $this->createMock(ContactRepositoryInterface::class); |
53
|
|
|
$this->logger = $this->createMock(Logger::class); |
54
|
|
|
$this->clientHelper = $this->createMock(ClientHelper::class); |
55
|
|
|
$this->contact = $this->createMock(ContactInterface::class); |
56
|
|
|
$this->contactApi = $this->createMock(ContactApiResourceInterface::class); |
57
|
|
|
|
58
|
|
|
$this->syncContactConsumer = new SyncContactConsumer( |
59
|
|
|
$this->contactRepository, |
60
|
|
|
$this->logger, |
61
|
|
|
$this->clientHelper |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testConsumeApiResponseException() |
66
|
|
|
{ |
67
|
|
|
$email = '[email protected]'; |
68
|
|
|
$request = ['email' => $email]; |
69
|
|
|
|
70
|
|
|
$this->contactRepository->expects($this->once()) |
|
|
|
|
71
|
|
|
->method('getOrCreateByEmail') |
72
|
|
|
->with($email) |
73
|
|
|
->willReturn($this->contact); |
74
|
|
|
|
75
|
|
|
$this->clientHelper->expects($this->once()) |
|
|
|
|
76
|
|
|
->method('getContactApi') |
77
|
|
|
->willReturn($this->contactApi); |
78
|
|
|
|
79
|
|
|
/** @var MockObject|HttpException $httpException */ |
80
|
|
|
$httpException = $this->createMock(HttpException::class); |
81
|
|
|
|
82
|
|
|
$this->contactApi->expects($this->once()) |
|
|
|
|
83
|
|
|
->method('upsert') |
84
|
|
|
->with(['contact' => $request]) |
85
|
|
|
->willThrowException($httpException); |
86
|
|
|
|
87
|
|
|
$this->logger->expects($this->once()) |
|
|
|
|
88
|
|
|
->method('error'); |
89
|
|
|
|
90
|
|
|
$this->contact->expects($this->never()) |
|
|
|
|
91
|
|
|
->method('setActiveCampaignId'); |
92
|
|
|
|
93
|
|
|
$this->syncContactConsumer->consume(json_encode(['email' => $email, 'request' => $request])); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testConsume() |
97
|
|
|
{ |
98
|
|
|
$email = '[email protected]'; |
99
|
|
|
$request = ['email' => $email]; |
100
|
|
|
$activeCampaignId = 123; |
101
|
|
|
$response = ['contact' => ['id' => $activeCampaignId]]; |
102
|
|
|
|
103
|
|
|
$this->contactRepository->expects($this->once()) |
104
|
|
|
->method('getOrCreateByEmail') |
105
|
|
|
->with($email) |
106
|
|
|
->willReturn($this->contact); |
107
|
|
|
|
108
|
|
|
$this->clientHelper->expects($this->once()) |
109
|
|
|
->method('getContactApi') |
110
|
|
|
->willReturn($this->contactApi); |
111
|
|
|
|
112
|
|
|
$this->contactApi->expects($this->once()) |
113
|
|
|
->method('upsert') |
114
|
|
|
->with(['contact' => $request]) |
115
|
|
|
->willReturn($response); |
116
|
|
|
|
117
|
|
|
$this->contact->expects($this->once()) |
118
|
|
|
->method('setActiveCampaignId') |
119
|
|
|
->with($activeCampaignId) |
120
|
|
|
->willReturnSelf(); |
121
|
|
|
|
122
|
|
|
$this->contactRepository->expects($this->once()) |
123
|
|
|
->method('save') |
124
|
|
|
->with($this->contact); |
125
|
|
|
|
126
|
|
|
$this->syncContactConsumer->consume(json_encode(['email' => $email, 'request' => $request])); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.