GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 10991c...ad4463 )
by Andreas
03:30
created

CreateUpdateConsumerTest::testConsume()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 32
dl 0
loc 44
rs 9.408
c 1
b 0
f 1
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 */
4
5
namespace CommerceLeague\ActiveCampaign\Test\Unit\MessageQueue\Contact;
6
7
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface;
8
use CommerceLeague\ActiveCampaign\Api\Data\ContactInterface;
9
use CommerceLeague\ActiveCampaign\Logger\Logger;
10
use CommerceLeague\ActiveCampaign\MessageQueue\Contact\CreateUpdateConsumer;
11
use CommerceLeague\ActiveCampaign\MessageQueue\Contact\CreateUpdateMessage;
12
use CommerceLeague\ActiveCampaignApi\Api\ContactApiResourceInterface;
13
use CommerceLeague\ActiveCampaignApi\Exception\HttpException;
14
use Magento\Framework\Exception\CouldNotSaveException;
15
use Magento\Framework\Phrase;
16
use PHPUnit\Framework\MockObject\MockObject;
17
use PHPUnit\Framework\TestCase;
18
use CommerceLeague\ActiveCampaign\Gateway\Client;
19
20
class CreateUpdateConsumerTest extends TestCase
21
{
22
    /**
23
     * @var MockObject|Client
24
     */
25
    protected $client;
26
27
    /**
28
     * @var MockObject|Logger
29
     */
30
    protected $logger;
31
32
    /**
33
     * @var MockObject|ContactRepositoryInterface
34
     */
35
    protected $contactRepository;
36
37
    /**
38
     * @var MockObject|CreateUpdateMessage
39
     */
40
    protected $createUpdateMessage;
41
42
    /**
43
     * @var MockObject|ContactInterface
44
     */
45
    protected $contact;
46
47
    /**
48
     * @var MockObject|ContactApiResourceInterface
49
     */
50
    protected $contactApi;
51
52
    /**
53
     * @var CreateUpdateConsumer
54
     */
55
    protected $createUpdateConsumer;
56
57
    protected function setUp()
58
    {
59
        $this->client = $this->createMock(Client::class);
60
        $this->logger = $this->createMock(Logger::class);
61
        $this->contactRepository = $this->createMock(ContactRepositoryInterface::class);
62
        $this->createUpdateMessage = $this->createMock(CreateUpdateMessage::class);
63
        $this->contact = $this->createMock(ContactInterface::class);
64
        $this->contactApi = $this->createMock(ContactApiResourceInterface::class);
65
66
        $this->createUpdateConsumer = new CreateUpdateConsumer(
67
            $this->client,
68
            $this->logger,
69
            $this->contactRepository
70
        );
71
    }
72
73
    public function testConsumeWithUnknownCustomer()
74
    {
75
        $contactId = 123;
76
77
        $this->contactRepository->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...tactRepositoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
        $this->contactRepository->/** @scrutinizer ignore-call */ 
78
                                  expects($this->once())

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.

Loading history...
78
            ->method('getById')
79
            ->with($contactId)
80
            ->willReturn($this->contact);
81
82
        $this->createUpdateMessage->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...act\CreateUpdateMessage. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        $this->createUpdateMessage->/** @scrutinizer ignore-call */ 
83
                                    expects($this->any())

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.

Loading history...
83
            ->method('getContactId')
84
            ->willReturn($contactId);
85
86
        $this->contact->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...i\Data\ContactInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to CommerceLeague\ActiveCam...i\Data\ContactInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        $this->contact->/** @scrutinizer ignore-call */ 
87
                        expects($this->once())
Loading history...
87
            ->method('getId')
88
            ->willReturn(null);
89
90
        $this->logger->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCampaign\Logger\Logger. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
        $this->logger->/** @scrutinizer ignore-call */ 
91
                       expects($this->once())

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.

Loading history...
91
            ->method('error')
92
            ->with(__('Unable to find contact with id "%1".', $contactId));
93
94
        $this->createUpdateMessage->expects($this->never())
95
            ->method('getSerializedRequest');
96
97
        $this->createUpdateConsumer->consume($this->createUpdateMessage);
98
    }
99
100
    public function testConsumeApiCallFails()
101
    {
102
        $contactId = 123;
103
        $request = ['request'];
104
        /** @var MockObject|HttpException $exception */
105
        $exception = $this->createMock(HttpException::class);
106
107
        $this->contactRepository->expects($this->once())
108
            ->method('getById')
109
            ->with($contactId)
110
            ->willReturn($this->contact);
111
112
        $this->createUpdateMessage->expects($this->any())
113
            ->method('getContactId')
114
            ->willReturn($contactId);
115
116
        $this->contact->expects($this->once())
117
            ->method('getId')
118
            ->willReturn($contactId);
119
120
        $this->createUpdateMessage->expects($this->once())
121
            ->method('getSerializedRequest')
122
            ->willReturn(json_encode($request));
123
124
        $this->client->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCampaign\Gateway\Client. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
        $this->client->/** @scrutinizer ignore-call */ 
125
                       expects($this->once())

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.

Loading history...
125
            ->method('getContactApi')
126
            ->willReturn($this->contactApi);
127
128
        $this->contactApi->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...actApiResourceInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
        $this->contactApi->/** @scrutinizer ignore-call */ 
129
                           expects($this->once())

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.

Loading history...
129
            ->method('upsert')
130
            ->with(['contact' => $request])
131
            ->willThrowException($exception);
132
133
        $this->logger->expects($this->once())
134
            ->method('error');
135
136
        $this->contact->expects($this->never())
137
            ->method('setActiveCampaignId');
138
139
        $this->createUpdateConsumer->consume($this->createUpdateMessage);
140
    }
141
142
    public function testConsumeContactCouldNotSave()
143
    {
144
        $contactId = 123;
145
        $activeCampaignId = 456;
146
        $request = ['request'];
147
148
        $this->contactRepository->expects($this->once())
149
            ->method('getById')
150
            ->with($contactId)
151
            ->willReturn($this->contact);
152
153
        $this->createUpdateMessage->expects($this->any())
154
            ->method('getContactId')
155
            ->willReturn($contactId);
156
157
        $this->contact->expects($this->once())
158
            ->method('getId')
159
            ->willReturn($contactId);
160
161
        $this->createUpdateMessage->expects($this->once())
162
            ->method('getSerializedRequest')
163
            ->willReturn(json_encode($request));
164
165
        $this->client->expects($this->once())
166
            ->method('getContactApi')
167
            ->willReturn($this->contactApi);
168
169
        $this->contactApi->expects($this->once())
170
            ->method('upsert')
171
            ->with(['contact' => $request])
172
            ->willReturn(['contact' => ['id' => $activeCampaignId]]);
173
174
        $this->contact->expects($this->once())
175
            ->method('setActiveCampaignId')
176
            ->with($activeCampaignId);
177
178
        $this->contactRepository->expects($this->once())
179
            ->method('save')
180
            ->with($this->contact)
181
            ->willThrowException(new CouldNotSaveException(new Phrase('an exception')));
182
183
        $this->logger->expects($this->once())
184
            ->method('error')
185
            ->with('an exception');
186
187
        $this->createUpdateConsumer->consume($this->createUpdateMessage);
188
    }
189
190
    public function testConsume()
191
    {
192
        $contactId = 123;
193
        $activeCampaignId = 456;
194
        $request = ['request'];
195
196
        $this->contactRepository->expects($this->once())
197
            ->method('getById')
198
            ->with($contactId)
199
            ->willReturn($this->contact);
200
201
        $this->createUpdateMessage->expects($this->any())
202
            ->method('getContactId')
203
            ->willReturn($contactId);
204
205
        $this->contact->expects($this->once())
206
            ->method('getId')
207
            ->willReturn($contactId);
208
209
        $this->createUpdateMessage->expects($this->once())
210
            ->method('getSerializedRequest')
211
            ->willReturn(json_encode($request));
212
213
        $this->client->expects($this->once())
214
            ->method('getContactApi')
215
            ->willReturn($this->contactApi);
216
217
        $this->contactApi->expects($this->once())
218
            ->method('upsert')
219
            ->with(['contact' => $request])
220
            ->willReturn(['contact' => ['id' => $activeCampaignId]]);
221
222
        $this->contact->expects($this->once())
223
            ->method('setActiveCampaignId')
224
            ->with($activeCampaignId);
225
226
        $this->contactRepository->expects($this->once())
227
            ->method('save')
228
            ->with($this->contact);
229
230
        $this->logger->expects($this->never())
231
            ->method('error');
232
233
        $this->createUpdateConsumer->consume($this->createUpdateMessage);
234
    }
235
}
236