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 ( c8c4e6...db9143 )
by Andreas
03:30
created

testConsumeWithAbsentMagentoCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 20
rs 9.8333
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 */
5
6
namespace CommerceLeague\ActiveCampaign\Test\Unit\MessageQueue\Customer;
7
8
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface;
9
use CommerceLeague\ActiveCampaign\Api\Data\ContactInterface;
10
use CommerceLeague\ActiveCampaign\Gateway\Request\ContactBuilder as ContactRequestBuilder;
11
use CommerceLeague\ActiveCampaign\Logger\Logger;
12
use CommerceLeague\ActiveCampaign\MessageQueue\Customer\ExportContactConsumer;
13
use CommerceLeague\ActiveCampaignApi\Api\ContactApiResourceInterface;
14
use CommerceLeague\ActiveCampaignApi\Exception\HttpException;
15
use Magento\Customer\Api\CustomerRepositoryInterface as MagentoCustomerRepositoryInterface;
16
use Magento\Customer\Api\Data\CustomerInterface as MagentoCustomerInterface;
17
use Magento\Framework\Exception\NoSuchEntityException;
18
use Magento\Framework\Phrase;
19
use PHPUnit\Framework\MockObject\MockObject;
20
use PHPUnit\Framework\TestCase;
21
use CommerceLeague\ActiveCampaign\Gateway\Client;
22
23
class ExportContactConsumerTest extends TestCase
24
{
25
    /**
26
     * @var MockObject|MagentoCustomerRepositoryInterface
27
     */
28
    protected $magentoCustomerRepository;
29
30
    /**
31
     * @var MockObject|Logger
32
     */
33
    protected $logger;
34
35
    /**
36
     * @var MockObject|ContactRepositoryInterface
37
     */
38
    protected $contactRepository;
39
40
    /**
41
     * @var MockObject|ContactRequestBuilder
42
     */
43
    protected $contactRequestBuilder;
44
45
    /**
46
     * @var MockObject|Client
47
     */
48
    protected $client;
49
50
    /**
51
     * @var MockObject|ContactInterface
52
     */
53
    protected $contact;
54
55
    /**
56
     * @var MockObject|ContactApiResourceInterface
57
     */
58
    protected $contactApi;
59
60
    /**
61
     * @var MockObject|MagentoCustomerInterface
62
     */
63
    protected $magentoCustomer;
64
65
    /**
66
     * @var ExportContactConsumer
67
     */
68
    protected $exportContactConsumer;
69
70
    protected function setUp()
71
    {
72
        $this->magentoCustomerRepository = $this->createMock(MagentoCustomerRepositoryInterface::class);
73
        $this->logger = $this->createMock(Logger::class);
74
        $this->contactRepository = $this->createMock(ContactRepositoryInterface::class);
75
        $this->contactRequestBuilder = $this->createMock(ContactRequestBuilder::class);
76
        $this->client = $this->createMock(Client::class);
77
        $this->contact = $this->createMock(ContactInterface::class);
78
        $this->contactApi = $this->createMock(ContactApiResourceInterface::class);
79
        $this->magentoCustomer = $this->createMock(MagentoCustomerInterface::class);
80
81
        $this->exportContactConsumer = new ExportContactConsumer(
82
            $this->magentoCustomerRepository,
83
            $this->logger,
84
            $this->contactRepository,
85
            $this->contactRequestBuilder,
86
            $this->client
87
        );
88
    }
89
90
    public function testConsumeWithAbsentMagentoCustomer()
91
    {
92
        $magentoCustomerId = 123;
93
94
        $exceptionMessage = 'an exception message';
95
        $exception = new NoSuchEntityException(new Phrase($exceptionMessage));
96
97
        $this->magentoCustomerRepository->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Magento\Customer\Api\CustomerRepositoryInterface. ( Ignorable by Annotation )

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

97
        $this->magentoCustomerRepository->/** @scrutinizer ignore-call */ 
98
                                          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...
98
            ->method('getById')
99
            ->with($magentoCustomerId)
100
            ->willThrowException($exception);
101
102
        $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

102
        $this->logger->/** @scrutinizer ignore-call */ 
103
                       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...
103
            ->method('error')
104
            ->with($exceptionMessage);
105
106
        $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

106
        $this->contactRepository->/** @scrutinizer ignore-call */ 
107
                                  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...
107
            ->method('getOrCreateByEmail');
108
109
        $this->exportContactConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId]));
110
    }
111
112
    public function testConsumeApiRequestException()
113
    {
114
        $magentoCustomerId = 123;
115
        $email = '[email protected]';
116
        $request = ['request'];
117
118
        $this->magentoCustomerRepository->expects($this->once())
119
            ->method('getById')
120
            ->with($magentoCustomerId)
121
            ->willReturn($this->magentoCustomer);
122
123
        $this->magentoCustomer->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Magento\Customer\Api\Data\CustomerInterface. ( Ignorable by Annotation )

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

123
        $this->magentoCustomer->/** @scrutinizer ignore-call */ 
124
                                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...
124
            ->method('getEmail')
125
            ->willReturn($email);
126
127
        $this->contactRepository->expects($this->once())
128
            ->method('getOrCreateByEmail')
129
            ->with($email)
130
            ->willReturn($this->contact);
131
132
        $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

132
        $this->contactRequestBuilder->/** @scrutinizer ignore-call */ 
133
                                      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...
133
            ->method('buildWithMagentoCustomer')
134
            ->willReturn($request);
135
136
        $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

136
        $this->client->/** @scrutinizer ignore-call */ 
137
                       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...
137
            ->method('getContactApi')
138
            ->willReturn($this->contactApi);
139
140
        /** @var MockObject|HttpException $httpException */
141
        $httpException = $this->createMock(HttpException::class);
142
143
        $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

143
        $this->contactApi->/** @scrutinizer ignore-call */ 
144
                           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...
144
            ->method('upsert')
145
            ->with(['contact' => $request])
146
            ->willThrowException($httpException);
147
148
        $this->logger->expects($this->once())
149
            ->method('error');
150
151
        $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

151
        $this->contact->/** @scrutinizer ignore-call */ 
152
                        expects($this->never())
Loading history...
152
            ->method('setActiveCampaignId');
153
154
        $this->exportContactConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId]));
155
    }
156
157
    public function testConsume()
158
    {
159
        $magentoCustomerId = 123;
160
        $email = '[email protected]';
161
        $request = ['request'];
162
        $activeCampaignId = 456;
163
        $response = ['contact' => ['id' => $activeCampaignId]];
164
165
        $this->magentoCustomerRepository->expects($this->once())
166
            ->method('getById')
167
            ->with($magentoCustomerId)
168
            ->willReturn($this->magentoCustomer);
169
170
        $this->magentoCustomer->expects($this->once())
171
            ->method('getEmail')
172
            ->willReturn($email);
173
174
        $this->contactRepository->expects($this->once())
175
            ->method('getOrCreateByEmail')
176
            ->with($email)
177
            ->willReturn($this->contact);
178
179
        $this->contactRequestBuilder->expects($this->once())
180
            ->method('buildWithMagentoCustomer')
181
            ->with($this->magentoCustomer)
182
            ->willReturn($request);
183
184
        $this->client->expects($this->once())
185
            ->method('getContactApi')
186
            ->willReturn($this->contactApi);
187
188
        $this->contactApi->expects($this->once())
189
            ->method('upsert')
190
            ->with(['contact' => $request])
191
            ->willReturn($response);
192
193
        $this->contact->expects($this->once())
194
            ->method('setActiveCampaignId')
195
            ->with($activeCampaignId)
196
            ->willReturnSelf();
197
198
        $this->contactRepository->expects($this->once())
199
            ->method('save')
200
            ->with($this->contact);
201
202
        $this->logger->expects($this->never())
203
            ->method('error');
204
205
        $this->exportContactConsumer->consume(json_encode(['magento_customer_id' => $magentoCustomerId]));
206
    }
207
}
208