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 ( 5543fb...d29718 )
by Andreas
03:19
created

ContactRepositoryTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 120
c 0
b 0
f 0
dl 0
loc 219
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testSaveThrowsException() 0 11 1
A testGetOrCreateByCustomerCreatesContact() 0 28 1
A testDeleteThrowsException() 0 11 1
A testGetById() 0 4 1
A testGetByCustomerId() 0 10 1
A testDeleteByIdThrowsException() 0 20 1
A testSave() 0 8 1
A testDeleteById() 0 19 1
A testGetOrCreateByCustomerLoadsContact() 0 25 1
A setUp() 0 26 1
A testDelete() 0 8 1
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 PHPUnit\Framework\MockObject\MockObject;
14
use PHPUnit\Framework\TestCase;
15
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Contact as ContactResource;
16
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...
17
18
class ContactRepositoryTest extends TestCase
19
{
20
    /**
21
     * @var MockObject|ContactResource
22
     */
23
    protected $contactResource;
24
25
    /**
26
     * @var MockObject|Contact
27
     */
28
    protected $contact;
29
30
    /**
31
     * @var MockObject|ContactFactory
32
     */
33
    protected $contactFactory;
34
35
    /**
36
     * @var MockObject|Customer
37
     */
38
    protected $customer;
39
40
    /**
41
     * @var ContactRepository
42
     */
43
    protected $contactRepository;
44
45
    protected function setUp()
46
    {
47
        $this->contactResource = $this->getMockBuilder(ContactResource::class)
48
            ->disableOriginalConstructor()
49
            ->getMock();
50
51
        $this->contactFactory = $this->getMockBuilder(ContactFactory::class)
52
            ->disableOriginalConstructor()
53
            ->setMethods(['create'])
54
            ->getMock();
55
56
        $this->contact = $this->getMockBuilder(Contact::class)
57
            ->disableOriginalConstructor()
58
            ->getMock();
59
60
        $this->contactFactory->expects($this->any())
61
            ->method('create')
62
            ->willReturn($this->contact);
63
64
        $this->customer = $this->getMockBuilder(Customer::class)
65
            ->disableOriginalConstructor()
66
            ->getMock();
67
68
        $this->contactRepository = new ContactRepository(
69
            $this->contactResource,
70
            $this->contactFactory
71
        );
72
    }
73
74
    public function testSaveThrowsException()
75
    {
76
        $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

76
        $this->contactResource->/** @scrutinizer ignore-call */ 
77
                                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...
77
            ->method('save')
78
            ->with($this->contact)
79
            ->willThrowException(new \Exception('an exception message'));
80
81
        $this->expectException(CouldNotSaveException::class);
82
        $this->expectExceptionMessage('an exception message');
83
84
        $this->contactRepository->save($this->contact);
85
    }
86
87
    public function testSave()
88
    {
89
        $this->contactResource->expects($this->once())
90
            ->method('save')
91
            ->with($this->contact)
92
            ->willReturnSelf();
93
94
        $this->assertEquals($this->contact, $this->contactRepository->save($this->contact));
95
    }
96
97
    public function testGetById()
98
    {
99
        $contactId = 123;
100
        $this->assertEquals($this->contact, $this->contactRepository->getById($contactId));
101
    }
102
103
    public function testGetByCustomerId()
104
    {
105
        $customerId = 456;
106
107
        $this->contactResource->expects($this->once())
108
            ->method('load')
109
            ->with($this->contact, $customerId, 'customer_id')
110
            ->willReturn($this->contact);
111
112
        $this->assertEquals($this->contact, $this->contactRepository->getByCustomerId($customerId));
113
    }
114
115
    public function testGetOrCreateByCustomerCreatesContact()
116
    {
117
        $customerId = 456;
118
119
        $this->customer->expects($this->any())
120
            ->method('getId')
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

120
            ->/** @scrutinizer ignore-call */ method('getId')

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
            ->willReturn($customerId);
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

121
            ->/** @scrutinizer ignore-call */ willReturn($customerId);

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...
122
123
        $this->contactResource->expects($this->once())
124
            ->method('load')
125
            ->with($this->contact, $customerId, 'customer_id')
126
            ->willReturn($this->contact);
127
128
        $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

128
        $this->contact->/** @scrutinizer ignore-call */ 
129
                        expects($this->once())
Loading history...
129
            ->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

129
            ->/** @scrutinizer ignore-call */ method('getId')
Loading history...
130
            ->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

130
            ->/** @scrutinizer ignore-call */ willReturn(null);
Loading history...
131
132
        $this->contact->expects($this->once())
133
            ->method('setCustomerId')
134
            ->with($customerId)
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

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

134
            ->/** @scrutinizer ignore-call */ with($customerId)

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

135
            ->/** @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...
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

135
            ->/** @scrutinizer ignore-call */ willReturnSelf();
Loading history...
136
137
        $this->contactResource->expects($this->once())
138
            ->method('save')
139
            ->with($this->contact)
140
            ->willReturnSelf();
141
142
        $this->assertSame($this->contact, $this->contactRepository->getOrCreateByCustomer($this->customer));
143
    }
144
145
    public function testGetOrCreateByCustomerLoadsContact()
146
    {
147
        $customerId = 456;
148
        $contactId = 678;
149
150
        $this->customer->expects($this->any())
151
            ->method('getId')
152
            ->willReturn($customerId);
153
154
        $this->contactResource->expects($this->once())
155
            ->method('load')
156
            ->with($this->contact, $customerId, 'customer_id')
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('setCustomerId');
165
166
        $this->contactResource->expects($this->never())
167
            ->method('save');
168
169
        $this->assertSame($this->contact, $this->contactRepository->getOrCreateByCustomer($this->customer));
170
    }
171
172
173
    public function testDeleteThrowsException()
174
    {
175
        $this->contactResource->expects($this->once())
176
            ->method('delete')
177
            ->with($this->contact)
178
            ->willThrowException(new \Exception('an exception message'));
179
180
        $this->expectException(CouldNotDeleteException::class);
181
        $this->expectExceptionMessage('an exception message');
182
183
        $this->contactRepository->delete($this->contact);
184
    }
185
186
    public function testDelete()
187
    {
188
        $this->contactResource->expects($this->once())
189
            ->method('delete')
190
            ->with($this->contact)
191
            ->willReturnSelf();
192
193
        $this->assertTrue($this->contactRepository->delete($this->contact));
194
    }
195
196
    public function testDeleteByIdThrowsException()
197
    {
198
        $contactId = 123;
199
200
        $this->contact->expects($this->once())
201
            ->method('getId')
202
            ->willReturn(null);
203
204
        $this->contactResource->expects($this->once())
205
            ->method('load')
206
            ->with($this->contact, $contactId)
207
            ->willReturn($this->contact);
208
209
        $this->contactResource->expects($this->never())
210
            ->method('delete');
211
212
        $this->expectException(NoSuchEntityException::class);
213
        $this->expectExceptionMessage('The Contact with the "123" ID doesn\'t exist');
214
215
        $this->contactRepository->deleteById($contactId);
216
    }
217
218
    public function testDeleteById()
219
    {
220
        $contactId = 123;
221
222
        $this->contact->expects($this->once())
223
            ->method('getId')
224
            ->willReturn($contactId);
225
226
        $this->contactResource->expects($this->once())
227
            ->method('load')
228
            ->with($this->contact, $contactId)
229
            ->willReturn($this->contact);
230
231
        $this->contactResource->expects($this->once())
232
            ->method('delete')
233
            ->with($this->contact)
234
            ->willReturnSelf();
235
236
        $this->assertTrue($this->contactRepository->deleteById($contactId));
237
    }
238
}
239