Issues (3627)

Tests/EventListener/CampaignSubscriberTest.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\EmailBundle\Tests\EventListener;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Mautic\CampaignBundle\Entity\Event;
16
use Mautic\CampaignBundle\Entity\LeadEventLog;
17
use Mautic\CampaignBundle\Event\PendingEvent;
18
use Mautic\CampaignBundle\EventCollector\Accessor\Event\ActionAccessor;
19
use Mautic\CampaignBundle\Executioner\RealTimeExecutioner;
20
use Mautic\EmailBundle\EventListener\CampaignSubscriber;
21
use Mautic\EmailBundle\Exception\EmailCouldNotBeSentException;
22
use Mautic\EmailBundle\Model\EmailModel;
23
use Mautic\EmailBundle\Model\SendEmailToUser;
24
use Mautic\LeadBundle\Entity\Lead;
25
use Symfony\Component\Translation\TranslatorInterface;
26
27
class CampaignSubscriberTest extends \PHPUnit\Framework\TestCase
28
{
29
    /**
30
     * @var array
31
     */
32
    private $config = [
0 ignored issues
show
The private property $config is not used, and could be removed.
Loading history...
33
        'useremail' => [
34
            'email' => 0,
35
        ],
36
        'user_id'  => [6, 7],
37
        'to_owner' => true,
38
        'to'       => '[email protected], [email protected]',
39
        'bcc'      => '[email protected]',
40
    ];
41
42
    /**
43
     * @var EmailModel|\PHPUnit\Framework\MockObject\MockObject
44
     */
45
    private $emailModel;
46
47
    /**
48
     * @var RealTimeExecutioner|\PHPUnit_Framework_MockObject_MockObject
49
     */
50
    private $realTimeExecutioner;
51
52
    /**
53
     * @var SendEmailToUser|\PHPUnit\Framework\MockObject\MockObject
54
     */
55
    private $sendEmailToUser;
56
57
    /**
58
     * @var TranslatorInterface|\PHPUnit\Framework\MockObject\MockObject
59
     */
60
    private $translator;
61
62
    /**
63
     * @var CampaignSubscriber
64
     */
65
    private $subscriber;
66
67
    protected function setUp(): void
68
    {
69
        parent::setUp();
70
71
        $this->emailModel          = $this->createMock(EmailModel::class);
72
        $this->realTimeExecutioner = $this->createMock(RealTimeExecutioner::class);
73
        $this->sendEmailToUser     = $this->createMock(SendEmailToUser::class);
74
        $this->translator          = $this->createMock(TranslatorInterface::class);
75
76
        $this->subscriber = new CampaignSubscriber(
77
            $this->emailModel,
78
            $this->realTimeExecutioner,
79
            $this->sendEmailToUser,
80
            $this->translator
81
        );
82
    }
83
84
    public function testOnCampaignTriggerActionSendEmailToUserWithWrongEventType()
85
    {
86
        $eventAccessor = $this->createMock(ActionAccessor::class);
87
        $event         = new Event();
88
        $lead          = (new Lead())->setEmail('[email protected]');
89
90
        $leadEventLog = $this->createMock(LeadEventLog::class);
91
        $leadEventLog
92
            ->method('getLead')
93
            ->willReturn($lead);
94
        $leadEventLog
95
            ->method('getId')
96
            ->willReturn(6);
97
98
        $logs = new ArrayCollection([$leadEventLog]);
99
100
        $pendingEvent = new PendingEvent($eventAccessor, $event, $logs);
101
        $this->subscriber->onCampaignTriggerActionSendEmailToUser($pendingEvent);
102
103
        $this->assertCount(0, $pendingEvent->getSuccessful());
104
        $this->assertCount(0, $pendingEvent->getFailures());
105
    }
106
107
    public function testOnCampaignTriggerActionSendEmailToUserWithSendingTheEmail()
108
    {
109
        $eventAccessor = $this->createMock(ActionAccessor::class);
110
        $event         = (new Event())->setType('email.send.to.user');
111
        $lead          = (new Lead())->setEmail('[email protected]');
112
113
        $leadEventLog = $this->createMock(LeadEventLog::class);
114
        $leadEventLog
115
            ->method('getLead')
116
            ->willReturn($lead);
117
        $leadEventLog
118
            ->method('getId')
119
            ->willReturn(0);
120
        $leadEventLog
121
            ->method('setIsScheduled')
122
            ->with(false)
123
            ->willReturn($leadEventLog);
124
125
        $logs = new ArrayCollection([$leadEventLog]);
126
127
        $pendingEvent = new PendingEvent($eventAccessor, $event, $logs);
128
        $this->subscriber->onCampaignTriggerActionSendEmailToUser($pendingEvent);
129
130
        $this->assertCount(1, $pendingEvent->getSuccessful());
131
        $this->assertCount(0, $pendingEvent->getFailures());
132
    }
133
134
    public function testOnCampaignTriggerActionSendEmailToUserWithError()
135
    {
136
        $eventAccessor = $this->createMock(ActionAccessor::class);
137
        $event         = (new Event())->setType('email.send.to.user');
138
        $lead          = (new Lead())->setEmail('[email protected]');
139
140
        $leadEventLog = $this->createMock(LeadEventLog::class);
141
        $leadEventLog
142
            ->method('getLead')
143
            ->willReturn($lead);
144
        $leadEventLog
145
            ->method('getId')
146
            ->willReturn(0);
147
        $leadEventLog
148
            ->method('setIsScheduled')
149
            ->with(false)
150
            ->willReturn($leadEventLog);
151
        $leadEventLog
152
            ->method('getMetadata')
153
            ->willReturn([]);
154
155
        $logs = new ArrayCollection([$leadEventLog]);
156
157
        $this->sendEmailToUser->expects($this->once())
158
            ->method('sendEmailToUsers')
159
            ->with([], $lead)
160
            ->will($this->throwException(new EmailCouldNotBeSentException('Something happened')));
161
162
        $pendingEvent = new PendingEvent($eventAccessor, $event, $logs);
163
        $this->subscriber->onCampaignTriggerActionSendEmailToUser($pendingEvent);
164
165
        $this->assertCount(0, $pendingEvent->getSuccessful());
166
167
        $failures = $pendingEvent->getFailures();
168
        $this->assertCount(1, $failures);
169
        /** @var LeadEventLog $failure */
170
        $failure    = $failures->first();
171
        $failedLead = $failure->getLead();
172
173
        $this->assertSame('[email protected]', $failedLead->getEmail());
174
    }
175
}
176