Issues (3627)

LeadBundle/Tests/Tracker/ContactTrackerTest.php (15 issues)

1
<?php
2
3
/*
4
 * @copyright   2017 Mautic Contributors. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\LeadBundle\Tests\Tracker;
13
14
use Mautic\CoreBundle\Entity\IpAddress;
15
use Mautic\CoreBundle\Helper\CoreParametersHelper;
16
use Mautic\CoreBundle\Helper\IpLookupHelper;
17
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
18
use Mautic\LeadBundle\Entity\Lead;
19
use Mautic\LeadBundle\Entity\LeadDevice;
20
use Mautic\LeadBundle\Entity\LeadRepository;
21
use Mautic\LeadBundle\LeadEvents;
22
use Mautic\LeadBundle\Model\FieldModel;
23
use Mautic\LeadBundle\Tracker\ContactTracker;
24
use Mautic\LeadBundle\Tracker\DeviceTracker;
25
use Mautic\LeadBundle\Tracker\Service\ContactTrackingService\ContactTrackingServiceInterface;
26
use Monolog\Logger;
27
use Symfony\Component\EventDispatcher\EventDispatcher;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpFoundation\RequestStack;
30
31
class ContactTrackerTest extends \PHPUnit\Framework\TestCase
32
{
33
    /**
34
     * @var LeadRepository
35
     */
36
    private $leadRepositoryMock;
37
38
    /**
39
     * @var ContactTrackingServiceInterface
40
     */
41
    private $contactTrackingServiceMock;
42
43
    /**
44
     * @var DeviceTracker
45
     */
46
    private $deviceTrackerMock;
47
48
    /**
49
     * @var CorePermissions
50
     */
51
    private $securityMock;
52
53
    /**
54
     * @var Logger
55
     */
56
    private $loggerMock;
57
58
    /**
59
     * @var IpLookupHelper
60
     */
61
    private $ipLookupHelperMock;
62
63
    /**
64
     * @var RequestStack
65
     */
66
    private $requestStack;
67
68
    /**
69
     * @var CoreParametersHelper
70
     */
71
    private $coreParametersHelperMock;
72
73
    /**
74
     * @var EventDispatcher
75
     */
76
    private $dispatcherMock;
77
78
    /**
79
     * @var FieldModel
80
     */
81
    private $leadFieldModelMock;
82
83
    protected function setUp(): void
84
    {
85
        $this->leadRepositoryMock = $this->getMockBuilder(LeadRepository::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Ma...onstructor()->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Mautic\LeadBundle\Entity\LeadRepository of property $leadRepositoryMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
86
            ->disableOriginalConstructor()
87
            ->getMock();
88
89
        $this->contactTrackingServiceMock = $this->getMockBuilder(ContactTrackingServiceInterface::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Ma...onstructor()->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Mautic\LeadBundle\Tracke...rackingServiceInterface of property $contactTrackingServiceMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
90
            ->disableOriginalConstructor()
91
            ->getMock();
92
93
        $this->deviceTrackerMock = $this->getMockBuilder(DeviceTracker::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Ma...onstructor()->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Mautic\LeadBundle\Tracker\DeviceTracker of property $deviceTrackerMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
94
            ->disableOriginalConstructor()
95
            ->getMock();
96
97
        $this->securityMock = $this->getMockBuilder(CorePermissions::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Ma...onstructor()->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Mautic\CoreBundle\Securi...issions\CorePermissions of property $securityMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
98
            ->disableOriginalConstructor()
99
            ->getMock();
100
        $this->securityMock->method('isAnonymous')
101
            ->willReturn(true);
102
103
        $this->loggerMock = $this->getMockBuilder(Logger::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Mo...onstructor()->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Monolog\Logger of property $loggerMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
104
            ->disableOriginalConstructor()
105
            ->getMock();
106
107
        $this->ipLookupHelperMock = $this->getMockBuilder(IpLookupHelper::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Ma...onstructor()->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Mautic\CoreBundle\Helper\IpLookupHelper of property $ipLookupHelperMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
108
            ->disableOriginalConstructor()
109
            ->getMock();
110
111
        $this->requestStack = new RequestStack();
112
        $request            = new Request();
113
        $this->requestStack->push($request);
114
115
        $this->coreParametersHelperMock = $this->getMockBuilder(CoreParametersHelper::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Ma...onstructor()->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Mautic\CoreBundle\Helper\CoreParametersHelper of property $coreParametersHelperMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
116
            ->disableOriginalConstructor()
117
            ->getMock();
118
119
        $this->dispatcherMock = $this->getMockBuilder(EventDispatcher::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Sy...onstructor()->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\EventDispatcher\EventDispatcher of property $dispatcherMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
120
            ->disableOriginalConstructor()
121
            ->getMock();
122
123
        $this->leadFieldModelMock = $this->createMock(FieldModel::class);
124
    }
125
126
    public function testSystemContactIsUsedOverTrackedContact()
127
    {
128
        $contactTracker = $this->getContactTracker();
129
130
        $this->leadRepositoryMock->expects($this->any())
0 ignored issues
show
The method expects() does not exist on Mautic\LeadBundle\Entity\LeadRepository. 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
        $this->leadRepositoryMock->/** @scrutinizer ignore-call */ 
131
                                   expects($this->any())
Loading history...
131
            ->method('getFieldValues')
132
            ->willReturn([]);
133
134
        $lead1 = new Lead();
135
        $lead1->setEmail('[email protected]');
136
        $contactTracker->setTrackedContact($lead1);
137
        $this->assertEquals($lead1->getEmail(), $contactTracker->getContact()->getEmail());
138
139
        $lead2 = new Lead();
140
        $lead1->setEmail('[email protected]');
141
        $contactTracker->setSystemContact($lead2);
142
        $this->assertEquals($lead2->getEmail(), $contactTracker->getContact()->getEmail());
143
    }
144
145
    public function testContactIsTrackedByDevice()
146
    {
147
        $contactTracker = $this->getContactTracker();
148
149
        $this->leadRepositoryMock->expects($this->once())
150
            ->method('getFieldValues')
151
            ->willReturn(
152
                [
153
                    'core' => [
154
                        'email' => [
155
                            'alias' => 'email',
156
                            'type'  => 'email',
157
                            'value' => '[email protected]',
158
                        ],
159
                    ],
160
                ]
161
            );
162
163
        $device = new LeadDevice();
164
        $lead   = new Lead();
165
        $device->setLead($lead);
166
167
        $this->deviceTrackerMock->method('getTrackedDevice')
0 ignored issues
show
The method method() does not exist on Mautic\LeadBundle\Tracker\DeviceTracker. ( Ignorable by Annotation )

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

167
        $this->deviceTrackerMock->/** @scrutinizer ignore-call */ 
168
                                  method('getTrackedDevice')

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...
168
            ->willReturn($device);
169
170
        $contact = $contactTracker->getContact();
171
172
        $this->assertEquals('[email protected]', $contact->getFieldValue('email'));
173
    }
174
175
    public function testContactIsTrackedByOldCookie()
176
    {
177
        $contactTracker = $this->getContactTracker();
178
179
        $this->leadRepositoryMock->expects($this->never())
180
            ->method('getFieldValues');
181
182
        $lead = new Lead();
183
        $lead->setEmail('[email protected]');
184
185
        $this->contactTrackingServiceMock->expects($this->once())
0 ignored issues
show
The method expects() does not exist on Mautic\LeadBundle\Tracke...rackingServiceInterface. ( Ignorable by Annotation )

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

185
        $this->contactTrackingServiceMock->/** @scrutinizer ignore-call */ 
186
                                           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...
186
            ->method('getTrackedLead')
187
            ->willReturn($lead);
188
189
        $contact = $contactTracker->getContact();
190
191
        $this->assertEquals('[email protected]', $contact->getEmail());
192
    }
193
194
    public function testContactIsTrackedByIp()
195
    {
196
        $contactTracker = $this->getContactTracker();
197
198
        $this->ipLookupHelperMock->expects($this->exactly(2))
0 ignored issues
show
The method expects() does not exist on Mautic\CoreBundle\Helper\IpLookupHelper. ( Ignorable by Annotation )

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

198
        $this->ipLookupHelperMock->/** @scrutinizer ignore-call */ 
199
                                   expects($this->exactly(2))

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...
199
            ->method('getIpAddress')
200
            ->willReturn(new IpAddress());
201
202
        $this->leadRepositoryMock->expects($this->never())
203
            ->method('getFieldValues');
204
205
        $lead = new Lead();
206
        $lead->setEmail('[email protected]');
207
208
        $this->contactTrackingServiceMock->expects($this->once())
209
            ->method('getTrackedLead')
210
            ->willReturn(null);
211
212
        $this->coreParametersHelperMock->expects($this->any())
0 ignored issues
show
The method expects() does not exist on Mautic\CoreBundle\Helper\CoreParametersHelper. ( Ignorable by Annotation )

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

212
        $this->coreParametersHelperMock->/** @scrutinizer ignore-call */ 
213
                                         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...
213
            ->method('get')
214
            ->willReturn(true);
215
216
        $this->leadRepositoryMock->expects($this->once())
217
            ->method('getLeadsByIp')
218
            ->willReturn([$lead]);
219
220
        $contact = $contactTracker->getContact();
221
222
        $this->assertEquals('[email protected]', $contact->getEmail());
223
    }
224
225
    public function testNewContactIsCreated()
226
    {
227
        $contactTracker = $this->getContactTracker();
228
229
        $this->leadRepositoryMock->expects($this->once())
230
            ->method('getFieldValues')
231
            ->willReturn([]);
232
233
        $this->ipLookupHelperMock->expects($this->exactly(2))
234
            ->method('getIpAddress')
235
            ->willReturn(new IpAddress());
236
237
        $this->leadRepositoryMock->expects($this->once())
238
            ->method('getFieldValues');
239
240
        $this->contactTrackingServiceMock->expects($this->once())
241
            ->method('getTrackedLead')
242
            ->willReturn(null);
243
244
        $this->coreParametersHelperMock->expects($this->once())
245
            ->method('get')
246
            ->willReturn(false);
247
248
        $this->leadRepositoryMock->expects($this->never())
249
            ->method('getLeadsByIp');
250
        $this->leadFieldModelMock->expects($this->any())->method('getFieldListWithProperties')->willReturn([]);
251
252
        $contact = $contactTracker->getContact();
253
        $this->assertEquals(true, $contact->isNewlyCreated());
254
    }
255
256
    public function testEventIsDispatchedWithChangeOfContact()
257
    {
258
        $contactTracker = $this->getContactTracker();
259
260
        $device = new LeadDevice();
261
        $device->setTrackingId('abc123');
262
263
        $lead = $this->getMockBuilder(Lead::class)
264
            ->getMock();
265
        $lead->method('getId')
266
            ->willReturn(1);
267
268
        $lead2 = $this->getMockBuilder(Lead::class)
269
            ->getMock();
270
        $lead2->method('getId')
271
            ->willReturn(2);
272
273
        $this->dispatcherMock->expects($this->once())
0 ignored issues
show
The method expects() does not exist on Symfony\Component\EventDispatcher\EventDispatcher. ( Ignorable by Annotation )

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

273
        $this->dispatcherMock->/** @scrutinizer ignore-call */ 
274
                               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...
274
            ->method('hasListeners')
275
            ->withConsecutive([LeadEvents::CURRENT_LEAD_CHANGED])
276
            ->willReturn(true);
277
278
        $this->dispatcherMock->expects($this->once())
279
            ->method('dispatch')
280
            ->withConsecutive([LeadEvents::CURRENT_LEAD_CHANGED, $this->anything()])
281
            ->willReturn(true);
282
283
        $leadDevice1 = new LeadDevice();
284
        $leadDevice1->setTrackingId('abc123');
285
        $this->deviceTrackerMock->expects($this->at(0))
0 ignored issues
show
The method expects() does not exist on Mautic\LeadBundle\Tracker\DeviceTracker. ( Ignorable by Annotation )

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

285
        $this->deviceTrackerMock->/** @scrutinizer ignore-call */ 
286
                                  expects($this->at(0))

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...
286
            ->method('getTrackedDevice')
287
            ->willReturn($leadDevice1);
288
289
        $leadDevice2 = new LeadDevice();
290
        $leadDevice2->setTrackingId('def456');
291
        $this->deviceTrackerMock->expects($this->at(2))
292
            ->method('getTrackedDevice')
293
            ->willReturn($leadDevice2);
294
295
        $contactTracker->setTrackedContact($lead);
296
        $contactTracker->setTrackedContact($lead2);
297
    }
298
299
    /**
300
     * @return ContactTracker
301
     */
302
    private function getContactTracker()
303
    {
304
        return new ContactTracker(
305
            $this->leadRepositoryMock,
306
            $this->contactTrackingServiceMock,
307
            $this->deviceTrackerMock,
308
            $this->securityMock,
309
            $this->loggerMock,
310
            $this->ipLookupHelperMock,
311
            $this->requestStack,
312
            $this->coreParametersHelperMock,
313
            $this->dispatcherMock,
314
            $this->leadFieldModelMock
315
        );
316
    }
317
}
318