Issues (3627)

Tests/EventListener/WebhookSubscriberTest.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2016 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\EventListener;
13
14
use Mautic\LeadBundle\Entity\Company;
15
use Mautic\LeadBundle\Entity\DoNotContact;
16
use Mautic\LeadBundle\Entity\Lead;
17
use Mautic\LeadBundle\Event\ChannelSubscriptionChange;
18
use Mautic\LeadBundle\Event\CompanyEvent;
19
use Mautic\LeadBundle\Event\LeadChangeCompanyEvent;
20
use Mautic\LeadBundle\Event\LeadEvent;
21
use Mautic\LeadBundle\EventListener\WebhookSubscriber;
22
use Mautic\LeadBundle\LeadEvents;
23
use Mautic\WebhookBundle\Model\WebhookModel;
24
use Symfony\Component\EventDispatcher\EventDispatcher;
25
26
class WebhookSubscriberTest extends \PHPUnit\Framework\TestCase
27
{
28
    /**
29
     * @var EventDispatcher|MockObject
0 ignored issues
show
The type Mautic\LeadBundle\Tests\EventListener\MockObject 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...
30
     */
31
    private $dispatcher;
32
33
    protected function setUp(): void
34
    {
35
        $this->dispatcher = new EventDispatcher();
36
    }
37
38
    public function testNewContactEventIsFiredWhenIdentified()
39
    {
40
        $mockModel  = $this->createMock(WebhookModel::class);
41
42
        $mockModel->expects($this->once())
43
            ->method('queueWebhooksByType')
44
            ->with(
45
                $this->callback(
46
                    function ($type) {
47
                        return LeadEvents::LEAD_POST_SAVE.'_new' === $type;
48
                    }
49
                )
50
            );
51
52
        $webhookSubscriber = new WebhookSubscriber($mockModel);
53
54
        $this->dispatcher->addSubscriber($webhookSubscriber);
55
56
        $lead = new Lead();
57
        $lead->setEmail('[email protected]');
58
        $lead->setDateIdentified(new \DateTime());
59
        $event = new LeadEvent($lead, true);
60
        $this->dispatcher->dispatch(LeadEvents::LEAD_POST_SAVE, $event);
61
    }
62
63
    public function testUpdateContactEventIsFiredWhenUpdatedButWithoutDateIdentified()
64
    {
65
        $mockModel  = $this->createMock(WebhookModel::class);
66
67
        $mockModel->expects($this->once())
68
            ->method('queueWebhooksByType')
69
            ->with(
70
                $this->callback(
71
                    function ($type) {
72
                        return LeadEvents::LEAD_POST_SAVE.'_update' === $type;
73
                    }
74
                )
75
            );
76
77
        $webhookSubscriber = new WebhookSubscriber($mockModel);
78
79
        $this->dispatcher->addSubscriber($webhookSubscriber);
80
81
        $lead = new Lead();
82
        $lead->setEmail('[email protected]');
83
        // remove date identified so it'll simulate a simple update
84
        $lead->resetChanges();
85
        $event = new LeadEvent($lead, false);
86
        $this->dispatcher->dispatch(LeadEvents::LEAD_POST_SAVE, $event);
87
    }
88
89
    public function testWebhookIsNotDeliveredIfContactIsAVisitor()
90
    {
91
        $mockModel  = $this->createMock(WebhookModel::class);
92
93
        $mockModel->expects($this->exactly(0))
94
            ->method('queueWebhooksByType');
95
96
        $webhookSubscriber = new WebhookSubscriber($mockModel);
97
98
        $this->dispatcher->addSubscriber($webhookSubscriber);
99
100
        $lead  = new Lead();
101
        $event = new LeadEvent($lead, false);
102
        $this->dispatcher->dispatch(LeadEvents::LEAD_POST_SAVE, $event);
103
    }
104
105
    /**
106
     * @testdox Test that webhook is queued for channel subscription changes
107
     */
108
    public function testChannelChangeIsPickedUpByWebhook()
109
    {
110
        $mockModel = $this->getMockBuilder(WebhookModel::class)
111
            ->disableOriginalConstructor()
112
            ->getMock();
113
114
        $lead    = new Lead();
115
        $channel = 'email';
116
117
        $mockModel->expects($this->exactly(1))
118
            ->method('queueWebhooksByType')
119
            ->with(
120
                LeadEvents::CHANNEL_SUBSCRIPTION_CHANGED,
121
                [
122
                    'contact'    => $lead,
123
                    'channel'    => $channel,
124
                    'old_status' => 'contactable',
125
                    'new_status' => 'unsubscribed',
126
                ],
127
128
                [
129
                    'leadDetails',
130
                    'userList',
131
                    'publishDetails',
132
                    'ipAddress',
133
                    'doNotContactList',
134
                    'tagList',
135
                ]
136
            );
137
138
        $webhookSubscriber = new WebhookSubscriber($mockModel);
139
140
        $this->dispatcher->addSubscriber($webhookSubscriber);
141
142
        $event = new ChannelSubscriptionChange($lead, $channel, DoNotContact::IS_CONTACTABLE, DoNotContact::UNSUBSCRIBED);
143
        $this->dispatcher->dispatch(LeadEvents::CHANNEL_SUBSCRIPTION_CHANGED, $event);
144
    }
145
146
    /**
147
     * @testdox Test that webhook is queued for lead company changes
148
     */
149
    public function testLeadCompanyChangeIsPickedUpByWebhook()
150
    {
151
        $mockModel = $this->getMockBuilder(WebhookModel::class)
152
            ->disableOriginalConstructor()
153
            ->getMock();
154
155
        $lead    = new Lead();
156
        $company = new Company();
157
158
        $mockModel->expects($this->exactly(1))
159
            ->method('queueWebhooksByType')
160
            ->with(
161
                LeadEvents::LEAD_COMPANY_CHANGE,
162
                [
163
                    'added'      => true,
164
                    'contact'    => $lead,
165
                    'company'    => $company,
166
                ],
167
                [
168
                ]
169
            );
170
171
        $webhookSubscriber = new WebhookSubscriber($mockModel);
172
173
        $this->dispatcher->addSubscriber($webhookSubscriber);
174
175
        $event = new LeadChangeCompanyEvent($lead, $company);
176
        $this->dispatcher->dispatch(LeadEvents::LEAD_COMPANY_CHANGE, $event);
177
    }
178
179
    public function testOnCompanySaveAndDelete()
180
    {
181
        $dispatcher = new EventDispatcher();
182
        $mockModel  = $this->createMock(WebhookModel::class);
183
184
        $mockModel->expects($this->exactly(2))
185
            ->method('queueWebhooksByType');
186
187
        $webhookSubscriber = new WebhookSubscriber($mockModel);
188
189
        $dispatcher->addSubscriber($webhookSubscriber);
190
191
        $company = new Company();
192
        $company->setName('company');
193
        $event = new CompanyEvent($company);
194
        $dispatcher->dispatch(LeadEvents::COMPANY_POST_SAVE, $event);
195
        $dispatcher->dispatch(LeadEvents::COMPANY_POST_DELETE, $event);
196
    }
197
}
198