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 ( 0f220b...47b153 )
by Andreas
07:24
created

testConsumeApiUnprocessableEntityHttpExceptionException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 39
nc 1
nop 0
dl 0
loc 55
rs 9.296
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
/**
4
 */
5
6
namespace CommerceLeague\ActiveCampaign\Test\Unit\MessageQueue\Newsletter;
7
8
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface;
9
use CommerceLeague\ActiveCampaign\Api\Data\ContactInterface;
10
use CommerceLeague\ActiveCampaign\Gateway\Client;
11
use CommerceLeague\ActiveCampaign\Gateway\Request\ContactBuilder as ContactRequestBuilder;
12
use CommerceLeague\ActiveCampaign\Logger\Logger;
13
use CommerceLeague\ActiveCampaign\MessageQueue\Newsletter\ExportContactConsumer;
14
use CommerceLeague\ActiveCampaignApi\Api\ContactApiResourceInterface;
15
use CommerceLeague\ActiveCampaignApi\Exception\HttpException;
16
use CommerceLeague\ActiveCampaignApi\Exception\UnprocessableEntityHttpException;
17
use Magento\Framework\Phrase;
18
use Magento\Newsletter\Model\Subscriber;
19
use PHPUnit\Framework\MockObject\MockObject;
20
use PHPUnit\Framework\TestCase;
21
use Magento\Newsletter\Model\SubscriberFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Newsletter\Model\SubscriberFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
class ExportContactConsumerTest extends TestCase
24
{
25
    /**
26
     * @var MockObject|SubscriberFactory
27
     */
28
    protected $subscriberFactory;
29
30
    /**
31
     * @var MockObject|Subscriber
32
     */
33
    protected $subscriber;
34
35
    /**
36
     * @var MockObject|Logger
37
     */
38
    protected $logger;
39
40
    /**
41
     * @var MockObject|ContactRepositoryInterface
42
     */
43
    protected $contactRepository;
44
45
    /**
46
     * @var MockObject|ContactRequestBuilder
47
     */
48
    protected $contactRequestBuilder;
49
50
    /**
51
     * @var MockObject|Client
52
     */
53
    protected $client;
54
55
    /**
56
     * @var MockObject|ContactInterface
57
     */
58
    protected $contact;
59
60
    /**
61
     * @var MockObject|ContactApiResourceInterface
62
     */
63
    protected $contactApi;
64
65
    /**
66
     * @var ExportContactConsumer
67
     */
68
    protected $exportContactConsumer;
69
70
    protected function setUp()
71
    {
72
        $this->subscriberFactory = $this->getMockBuilder(SubscriberFactory::class)
73
            ->disableOriginalConstructor()
74
            ->setMethods(['create'])
75
            ->getMock();
76
77
        $this->subscriber = $this->createMock(Subscriber::class);
78
79
        $this->subscriberFactory->expects($this->any())
80
            ->method('create')
81
            ->willReturn($this->subscriber);
82
83
        $this->logger = $this->createMock(Logger::class);
84
        $this->contactRepository = $this->createMock(ContactRepositoryInterface::class);
85
        $this->contactRequestBuilder = $this->createMock(ContactRequestBuilder::class);
86
        $this->client = $this->createMock(Client::class);
87
        $this->contact = $this->createMock(ContactInterface::class);
88
        $this->contactApi = $this->createMock(ContactApiResourceInterface::class);
89
90
        $this->exportContactConsumer = new ExportContactConsumer(
91
            $this->subscriberFactory,
92
            $this->logger,
93
            $this->contactRepository,
94
            $this->contactRequestBuilder,
95
            $this->client
96
        );
97
    }
98
99
    public function testConsumeWithAbsentSubscriber()
100
    {
101
        $email = '[email protected]';
102
103
        $this->subscriber->expects($this->once())
104
            ->method('loadByEmail')
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

104
            ->/** @scrutinizer ignore-call */ method('loadByEmail')

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...
105
            ->with($email)
0 ignored issues
show
Bug introduced by
The method with() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

105
            ->/** @scrutinizer ignore-call */ with($email)

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...
106
            ->willReturnSelf();
0 ignored issues
show
Bug introduced by
The method willReturnSelf() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

106
            ->/** @scrutinizer ignore-call */ willReturnSelf();

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...
107
108
        $this->subscriber->expects($this->once())
109
            ->method('getId')
110
            ->willReturn(null);
0 ignored issues
show
Bug introduced by
The method willReturn() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

110
            ->/** @scrutinizer ignore-call */ willReturn(null);

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...
111
112
        $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

112
        $this->logger->/** @scrutinizer ignore-call */ 
113
                       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...
113
            ->method('error')
114
            ->with(new Phrase('The Subscriber with the "%1" email doesn\'t exist', [$email]));
115
116
        $this->contactRepository->expects($this->never())
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

116
        $this->contactRepository->/** @scrutinizer ignore-call */ 
117
                                  expects($this->never())

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...
117
            ->method('getOrCreateByEmail');
118
119
        $this->exportContactConsumer->consume(json_encode(['email' => $email]));
120
    }
121
122
    public function testConsumeApiHttpException()
123
    {
124
        $email = '[email protected]';
125
        $request = ['request'];
126
127
        $this->subscriber->expects($this->once())
128
            ->method('loadByEmail')
129
            ->with($email)
130
            ->willReturnSelf();
131
132
        $this->subscriber->expects($this->once())
133
            ->method('getId')
134
            ->willReturn(123);
135
136
        $this->subscriber->expects($this->once())
137
            ->method('getEmail')
138
            ->willReturn($email);
139
140
        $this->contactRepository->expects($this->once())
141
            ->method('getOrCreateByEmail')
142
            ->with($email)
143
            ->willReturn($this->contact);
144
145
        $this->contactRequestBuilder->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...\Request\ContactBuilder. ( Ignorable by Annotation )

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

145
        $this->contactRequestBuilder->/** @scrutinizer ignore-call */ 
146
                                      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...
146
            ->method('buildWithSubscriber')
147
            ->willReturn($request);
148
149
        $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

149
        $this->client->/** @scrutinizer ignore-call */ 
150
                       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...
150
            ->method('getContactApi')
151
            ->willReturn($this->contactApi);
152
153
        /** @var MockObject|HttpException $httpException */
154
        $httpException = $this->createMock(HttpException::class);
155
156
        $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

156
        $this->contactApi->/** @scrutinizer ignore-call */ 
157
                           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...
157
            ->method('upsert')
158
            ->with(['contact' => $request])
159
            ->willThrowException($httpException);
160
161
        $this->logger->expects($this->once())
162
            ->method('error');
163
164
        $this->contact->expects($this->never())
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

164
        $this->contact->/** @scrutinizer ignore-call */ 
165
                        expects($this->never())
Loading history...
165
            ->method('setActiveCampaignId');
166
167
        $this->exportContactConsumer->consume(json_encode(['email' => $email]));
168
    }
169
170
    public function testConsumeApiUnprocessableEntityHttpExceptionException()
171
    {
172
        $email = '[email protected]';
173
        $request = ['request'];
174
        $responseErrors = ['first error', 'second error'];
175
176
        $this->subscriber->expects($this->once())
177
            ->method('loadByEmail')
178
            ->with($email)
179
            ->willReturnSelf();
180
181
        $this->subscriber->expects($this->once())
182
            ->method('getId')
183
            ->willReturn(123);
184
185
        $this->subscriber->expects($this->once())
186
            ->method('getEmail')
187
            ->willReturn($email);
188
189
        $this->contactRepository->expects($this->once())
190
            ->method('getOrCreateByEmail')
191
            ->with($email)
192
            ->willReturn($this->contact);
193
194
        $this->contactRequestBuilder->expects($this->once())
195
            ->method('buildWithSubscriber')
196
            ->willReturn($request);
197
198
        $this->client->expects($this->once())
199
            ->method('getContactApi')
200
            ->willReturn($this->contactApi);
201
202
        /** @var MockObject|UnprocessableEntityHttpException $unprocessableEntityHttpException */
203
        $unprocessableEntityHttpException = $this->createMock(UnprocessableEntityHttpException::class);
204
205
        $this->contactApi->expects($this->once())
206
            ->method('upsert')
207
            ->with(['contact' => $request])
208
            ->willThrowException($unprocessableEntityHttpException);
209
210
        $this->logger->expects($this->exactly(2))
211
            ->method('error');
212
213
        $unprocessableEntityHttpException->expects($this->once())
214
            ->method('getResponseErrors')
215
            ->willReturn($responseErrors);
216
217
        $this->logger->expects($this->at(1))
218
            ->method('error')
219
            ->with(print_r($responseErrors, true));
220
221
        $this->contact->expects($this->never())
222
            ->method('setActiveCampaignId');
223
224
        $this->exportContactConsumer->consume(json_encode(['email' => $email]));
225
    }
226
227
    public function testConsume()
228
    {
229
        $email = '[email protected]';
230
        $request = ['request'];
231
        $activeCampaignId = 456;
232
        $response = ['contact' => ['id' => $activeCampaignId]];
233
234
        $this->subscriber->expects($this->once())
235
            ->method('loadByEmail')
236
            ->with($email)
237
            ->willReturnSelf();
238
239
        $this->subscriber->expects($this->once())
240
            ->method('getId')
241
            ->willReturn(123);
242
243
        $this->subscriber->expects($this->once())
244
            ->method('getEmail')
245
            ->willReturn($email);
246
247
        $this->contactRepository->expects($this->once())
248
            ->method('getOrCreateByEmail')
249
            ->with($email)
250
            ->willReturn($this->contact);
251
252
        $this->contactRequestBuilder->expects($this->once())
253
            ->method('buildWithSubscriber')
254
            ->willReturn($request);
255
256
        $this->client->expects($this->once())
257
            ->method('getContactApi')
258
            ->willReturn($this->contactApi);
259
260
261
        $this->contactApi->expects($this->once())
262
            ->method('upsert')
263
            ->with(['contact' => $request])
264
            ->willReturn($response);
265
266
        $this->contact->expects($this->once())
267
            ->method('setActiveCampaignId')
268
            ->with($activeCampaignId)
269
            ->willReturnSelf();
270
271
        $this->contactRepository->expects($this->once())
272
            ->method('save')
273
            ->with($this->contact);
274
275
        $this->logger->expects($this->never())
276
            ->method('error');
277
278
        $this->exportContactConsumer->consume(json_encode(['email' => $email]));
279
    }
280
}
281