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 ( ad4463...4d14c7 )
by Andreas
03:15
created

ContactRepositoryTest::testDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 */
4
5
namespace CommerceLeague\ActiveCampaign\Test\Unit\Model;
6
7
use CommerceLeague\ActiveCampaign\Model\Contact;
8
use CommerceLeague\ActiveCampaign\Model\ContactRepository;
9
use Magento\Customer\Model\Customer;
10
use Magento\Framework\Exception\CouldNotDeleteException;
11
use Magento\Framework\Exception\CouldNotSaveException;
12
use Magento\Framework\Exception\NoSuchEntityException;
13
use Magento\Newsletter\Model\Subscriber;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use PHPUnit\Framework\TestCase;
16
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Contact as ContactResource;
17
use CommerceLeague\ActiveCampaign\Model\ContactFactory;
0 ignored issues
show
Bug introduced by
The type CommerceLeague\ActiveCampaign\Model\ContactFactory 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...
18
19
class ContactRepositoryTest extends TestCase
20
{
21
    /**
22
     * @var MockObject|ContactResource
23
     */
24
    protected $contactResource;
25
26
    /**
27
     * @var MockObject|Contact
28
     */
29
    protected $contact;
30
31
    /**
32
     * @var MockObject|ContactFactory
33
     */
34
    protected $contactFactory;
35
36
    /**
37
     * @var MockObject|Customer
38
     */
39
    protected $customer;
40
41
    /**
42
     * @var MockObject|Subscriber
43
     */
44
    protected $subscriber;
45
46
    /**
47
     * @var ContactRepository
48
     */
49
    protected $contactRepository;
50
51
    protected function setUp()
52
    {
53
        $this->contactResource = $this->getMockBuilder(ContactResource::class)
54
            ->disableOriginalConstructor()
55
            ->getMock();
56
57
        $this->contactFactory = $this->getMockBuilder(ContactFactory::class)
58
            ->disableOriginalConstructor()
59
            ->setMethods(['create'])
60
            ->getMock();
61
62
        $this->contact = $this->getMockBuilder(Contact::class)
63
            ->disableOriginalConstructor()
64
            ->getMock();
65
66
        $this->contactFactory->expects($this->any())
67
            ->method('create')
68
            ->willReturn($this->contact);
69
70
        $this->customer = $this->getMockBuilder(Customer::class)
71
            ->disableOriginalConstructor()
72
            ->getMock();
73
74
        $this->subscriber = $this->getMockBuilder(Subscriber::class)
75
            ->disableOriginalConstructor()
76
            ->getMock();
77
78
        $this->contactRepository = new ContactRepository(
79
            $this->contactResource,
80
            $this->contactFactory
81
        );
82
    }
83
84
    public function testSaveThrowsException()
85
    {
86
        $this->contactResource->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCam...l\ResourceModel\Contact. ( 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->contactResource->/** @scrutinizer ignore-call */ 
87
                                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...
87
            ->method('save')
88
            ->with($this->contact)
89
            ->willThrowException(new \Exception('an exception message'));
90
91
        $this->expectException(CouldNotSaveException::class);
92
        $this->expectExceptionMessage('an exception message');
93
94
        $this->contactRepository->save($this->contact);
95
    }
96
97
    public function testSave()
98
    {
99
        $this->contactResource->expects($this->once())
100
            ->method('save')
101
            ->with($this->contact)
102
            ->willReturnSelf();
103
104
        $this->assertEquals($this->contact, $this->contactRepository->save($this->contact));
105
    }
106
107
    public function testGetById()
108
    {
109
        $contactId = 123;
110
        $this->assertEquals($this->contact, $this->contactRepository->getById($contactId));
111
    }
112
113
    public function testGetOrCreateByCustomerCreatesContact()
114
    {
115
        $email = '[email protected]';
116
117
        $this->customer->expects($this->any())
118
            ->method('getData')
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

118
            ->/** @scrutinizer ignore-call */ method('getData')

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...
119
            ->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

119
            ->/** @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...
120
            ->willReturn($email);
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

120
            ->/** @scrutinizer ignore-call */ willReturn($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...
121
122
        $this->contactResource->expects($this->once())
123
            ->method('load')
124
            ->with($this->contact, $email, 'email')
125
            ->willReturn($this->contact);
126
127
        $this->contact->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on CommerceLeague\ActiveCampaign\Model\Contact. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

127
        $this->contact->/** @scrutinizer ignore-call */ 
128
                        expects($this->once())
Loading history...
128
            ->method('getId')
0 ignored issues
show
Bug introduced by
The method method() does not exist on CommerceLeague\ActiveCampaign\Model\Contact. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

128
            ->/** @scrutinizer ignore-call */ method('getId')
Loading history...
129
            ->willReturn(null);
0 ignored issues
show
Bug introduced by
The method willReturn() does not exist on CommerceLeague\ActiveCampaign\Model\Contact. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

129
            ->/** @scrutinizer ignore-call */ willReturn(null);
Loading history...
130
131
        $this->contact->expects($this->once())
132
            ->method('setEmail')
133
            ->with($email)
0 ignored issues
show
Bug introduced by
The method with() does not exist on CommerceLeague\ActiveCampaign\Model\Contact. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

133
            ->/** @scrutinizer ignore-call */ with($email)
Loading history...
134
            ->willReturnSelf();
0 ignored issues
show
Bug introduced by
The method willReturnSelf() does not exist on CommerceLeague\ActiveCampaign\Model\Contact. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

134
            ->/** @scrutinizer ignore-call */ willReturnSelf();
Loading history...
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

134
            ->/** @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...
135
136
        $this->contactResource->expects($this->once())
137
            ->method('save')
138
            ->with($this->contact)
139
            ->willReturnSelf();
140
141
        $this->assertSame($this->contact, $this->contactRepository->getOrCreateByCustomer($this->customer));
142
    }
143
144
    public function testGetOrCreateByCustomerLoadsContact()
145
    {
146
        $contactId = 678;
147
        $email = '[email protected]';
148
149
        $this->customer->expects($this->any())
150
            ->method('getData')
151
            ->with('email')
152
            ->willReturn($email);
153
154
        $this->contactResource->expects($this->once())
155
            ->method('load')
156
            ->with($this->contact, $email, 'email')
157
            ->willReturn($this->contact);
158
159
        $this->contact->expects($this->once())
160
            ->method('getId')
161
            ->willReturn($contactId);
162
163
        $this->contact->expects($this->never())
164
            ->method('setEmail');
165
166
        $this->contactResource->expects($this->never())
167
            ->method('save');
168
169
        $this->assertSame($this->contact, $this->contactRepository->getOrCreateByCustomer($this->customer));
170
    }
171
172
    public function testGetOrCreateBySubscriberCreatesContact()
173
    {
174
        $email = '[email protected]';
175
176
        $this->subscriber->expects($this->any())
177
            ->method('getEmail')
178
            ->willReturn($email);
179
180
        $this->contactResource->expects($this->once())
181
            ->method('load')
182
            ->with($this->contact, $email, 'email')
183
            ->willReturn($this->contact);
184
185
        $this->contact->expects($this->once())
186
            ->method('getId')
187
            ->willReturn(null);
188
189
        $this->contact->expects($this->once())
190
            ->method('setEmail')
191
            ->with($email)
192
            ->willReturnSelf();
193
194
        $this->contactResource->expects($this->once())
195
            ->method('save')
196
            ->with($this->contact)
197
            ->willReturnSelf();
198
199
        $this->assertSame($this->contact, $this->contactRepository->getOrCreateBySubscriber($this->subscriber));
200
    }
201
202
203
    public function testGetOrCreateBySubscriberLoadsContact()
204
    {
205
        $contactId = 678;
206
        $email = '[email protected]';
207
208
        $this->subscriber->expects($this->any())
209
            ->method('getEmail')
210
            ->willReturn($email);
211
212
        $this->contactResource->expects($this->once())
213
            ->method('load')
214
            ->with($this->contact, $email, 'email')
215
            ->willReturn($this->contact);
216
217
        $this->contact->expects($this->once())
218
            ->method('getId')
219
            ->willReturn($contactId);
220
221
        $this->contact->expects($this->never())
222
            ->method('setEmail');
223
224
        $this->contactResource->expects($this->never())
225
            ->method('save');
226
227
        $this->assertSame($this->contact, $this->contactRepository->getOrCreateBySubscriber($this->subscriber));
228
    }
229
230
231
    public function testDeleteThrowsException()
232
    {
233
        $this->contactResource->expects($this->once())
234
            ->method('delete')
235
            ->with($this->contact)
236
            ->willThrowException(new \Exception('an exception message'));
237
238
        $this->expectException(CouldNotDeleteException::class);
239
        $this->expectExceptionMessage('an exception message');
240
241
        $this->contactRepository->delete($this->contact);
242
    }
243
244
    public function testDelete()
245
    {
246
        $this->contactResource->expects($this->once())
247
            ->method('delete')
248
            ->with($this->contact)
249
            ->willReturnSelf();
250
251
        $this->assertTrue($this->contactRepository->delete($this->contact));
252
    }
253
254
    public function testDeleteByIdThrowsException()
255
    {
256
        $contactId = 123;
257
258
        $this->contact->expects($this->once())
259
            ->method('getId')
260
            ->willReturn(null);
261
262
        $this->contactResource->expects($this->once())
263
            ->method('load')
264
            ->with($this->contact, $contactId)
265
            ->willReturn($this->contact);
266
267
        $this->contactResource->expects($this->never())
268
            ->method('delete');
269
270
        $this->expectException(NoSuchEntityException::class);
271
        $this->expectExceptionMessage('The Contact with the "123" ID doesn\'t exist');
272
273
        $this->contactRepository->deleteById($contactId);
274
    }
275
276
    public function testDeleteById()
277
    {
278
        $contactId = 123;
279
280
        $this->contact->expects($this->once())
281
            ->method('getId')
282
            ->willReturn($contactId);
283
284
        $this->contactResource->expects($this->once())
285
            ->method('load')
286
            ->with($this->contact, $contactId)
287
            ->willReturn($this->contact);
288
289
        $this->contactResource->expects($this->once())
290
            ->method('delete')
291
            ->with($this->contact)
292
            ->willReturnSelf();
293
294
        $this->assertTrue($this->contactRepository->deleteById($contactId));
295
    }
296
}
297