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\LeadBundle\Tests\EventListener;
13
14
use Mautic\CampaignBundle\Event\CampaignExecutionEvent;
15
use Mautic\CampaignBundle\Model\CampaignModel;
16
use Mautic\CoreBundle\Helper\CoreParametersHelper;
17
use Mautic\CoreBundle\Helper\IpLookupHelper;
18
use Mautic\LeadBundle\Entity\Company;
19
use Mautic\LeadBundle\Entity\CompanyLeadRepository;
20
use Mautic\LeadBundle\Entity\Lead;
21
use Mautic\LeadBundle\EventListener\CampaignSubscriber;
22
use Mautic\LeadBundle\Model\CompanyModel;
23
use Mautic\LeadBundle\Model\FieldModel;
24
use Mautic\LeadBundle\Model\LeadModel;
25
use Mautic\LeadBundle\Model\ListModel;
26
27
class CampaignSubscriberTest extends \PHPUnit\Framework\TestCase
28
{
29
    /** @var array */
30
    private $configFrom = [
31
        'id'          => 111,
32
        'companyname' => 'Mautic',
33
        'companemail' => '[email protected]',
34
    ];
35
36
    private $configTo = [
37
        'id'          => '112',
38
        'companyname' => 'Mautic2',
39
        'companemail' => '[email protected]',
40
    ];
41
42
    public function testOnCampaignTriggerActiononUpdateCompany()
43
    {
44
        $mockIpLookupHelper = $this->createMock(IpLookupHelper::class);
45
        $mockLeadModel      = $this->createMock(LeadModel::class);
46
        $mockLeadFieldModel = $this->createMock(FieldModel::class);
47
        $mockListModel      = $this->createMock(ListModel::class);
48
        $mockCompanyModel   = $this->createMock(CompanyModel::class);
49
        $mockCampaignModel  = $this->createMock(CampaignModel::class);
50
        $companyEntityFrom  = $this->createMock(Company::class);
51
52
        $companyEntityFrom->method('getId')
53
            ->willReturn($this->configFrom['id']);
54
        $companyEntityFrom->method('getName')
55
            ->willReturn($this->configFrom['companyname']);
56
57
        $companyEntityTo = $this->createMock(Company::class);
58
        $companyEntityTo->method('getId')
59
            ->willReturn($this->configTo['id']);
60
        $companyEntityTo->method('getName')
61
            ->willReturn($this->configTo['companyname']);
62
63
        $mockCompanyModel->expects($this->once())->method('getEntity')->willReturn($companyEntityFrom);
64
65
        $mockCompanyModel->expects($this->once())
66
            ->method('getEntities')
67
            ->willReturn([$companyEntityTo]);
68
69
        $mockCompanyLeadRepo  = $this->createMock(CompanyLeadRepository::class);
70
        $mockCompanyLeadRepo->expects($this->once())->method('getCompaniesByLeadId')->willReturn(null);
71
72
        $mockCompanyModel->expects($this->once())
73
            ->method('getCompanyLeadRepository')
74
            ->willReturn($mockCompanyLeadRepo);
75
76
        $mockCoreParametersHelper = $this->createMock(CoreParametersHelper::class);
77
        $mockCoreParametersHelper->method('get')
78
            ->with('default_timezone')
79
            ->willReturn('UTC');
80
81
        $subscriber = new CampaignSubscriber(
82
            $mockIpLookupHelper,
83
            $mockLeadModel,
84
            $mockLeadFieldModel,
85
            $mockListModel,
86
            $mockCompanyModel,
87
            $mockCampaignModel,
88
            $mockCoreParametersHelper
89
        );
90
91
        /** @var LeadModel $leadModel */
92
        $lead = new Lead();
93
        $lead->setId(99);
94
        $lead->setPrimaryCompany($this->configFrom);
95
96
        $mockLeadModel->expects($this->once())->method('setPrimaryCompany')->willReturnCallback(
97
            function () use ($lead) {
98
                $lead->setPrimaryCompany($this->configTo);
99
            }
100
        );
101
102
        $args = [
103
            'lead'  => $lead,
104
            'event' => [
105
                'type'       => 'lead.updatecompany',
106
                'properties' => $this->configTo,
107
            ],
108
            'eventDetails'    => [],
109
            'systemTriggered' => true,
110
            'eventSettings'   => [],
111
        ];
112
113
        $event = new CampaignExecutionEvent($args, true);
0 ignored issues
show
Deprecated Code introduced by
The class Mautic\CampaignBundle\Event\CampaignExecutionEvent has been deprecated: 2.13.0; to be removed in 3.0 ( Ignorable by Annotation )

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

113
        $event = /** @scrutinizer ignore-deprecated */ new CampaignExecutionEvent($args, true);
Loading history...
114
        $subscriber->onCampaignTriggerActionUpdateCompany($event);
115
        $this->assertTrue($event->getResult());
116
117
        $primaryCompany = $lead->getPrimaryCompany();
118
119
        $this->assertSame($this->configTo['companyname'], $primaryCompany['companyname']);
120
    }
121
}
122