Issues (3627)

Tests/EventListener/CompanySubscriberTest.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2018 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://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\CoreBundle\Helper\IpLookupHelper;
15
use Mautic\CoreBundle\Model\AuditLogModel;
16
use Mautic\LeadBundle\Entity\Company;
17
use Mautic\LeadBundle\Event\CompanyEvent;
18
use Mautic\LeadBundle\EventListener\CompanySubscriber;
19
use Mautic\LeadBundle\LeadEvents;
20
21
class CompanySubscriberTest extends \PHPUnit\Framework\TestCase
22
{
23
    public function testGetSubscribedEvents()
24
    {
25
        $ipLookupHelper = $this->createMock(IpLookupHelper::class);
26
        $auditLogModel  = $this->createMock(AuditLogModel::class);
27
        $subscriber     = new CompanySubscriber($ipLookupHelper, $auditLogModel);
28
29
        $this->assertEquals(
30
            [
31
                LeadEvents::COMPANY_POST_SAVE   => ['onCompanyPostSave', 0],
32
                LeadEvents::COMPANY_POST_DELETE => ['onCompanyDelete', 0],
33
            ],
34
            $subscriber->getSubscribedEvents()
35
        );
36
    }
37
38
    public function testOnCompanyPostSave()
39
    {
40
        $this->onCompanyPostSaveMethodCall(false); // update company log
41
        $this->onCompanyPostSaveMethodCall(true); // create company log
42
    }
43
44
    public function testOnCompanyDelete()
45
    {
46
        $companyId        = 1;
47
        $companyName      = 'name';
48
        $ip               = '127.0.0.2';
49
50
        $log = [
51
            'bundle'    => 'lead',
52
            'object'    => 'company',
53
            'objectId'  => $companyId,
54
            'action'    => 'delete',
55
            'details'   => ['name', $companyName],
56
            'ipAddress' => $ip,
57
        ];
58
59
        $ipLookupHelper = $this->createMock(IpLookupHelper::class);
60
        $ipLookupHelper->expects($this->once())
61
            ->method('getIpAddressFromRequest')
62
            ->will($this->returnValue($ip));
63
64
        $auditLogModel = $this->createMock(AuditLogModel::class);
65
        $auditLogModel->expects($this->once())
66
            ->method('writeToLog')
67
            ->with($log);
68
69
        $subscriber = new CompanySubscriber($ipLookupHelper, $auditLogModel);
70
71
        $company            = $this->createMock(Company::class);
72
        $company->deletedId = $companyId;
0 ignored issues
show
Accessing deletedId on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
73
        $company->expects($this->once())
74
            ->method('getPrimaryIdentifier')
75
            ->will($this->returnValue($companyName));
76
77
        $event = $this->createMock(CompanyEvent::class);
78
        $event->expects($this->once())
79
            ->method('getCompany')
80
            ->will($this->returnValue($company));
81
82
        $subscriber->onCompanyDelete($event);
83
    }
84
85
    /**
86
     * Test create or update company logging.
87
     *
88
     * @param bool $isNew
89
     */
90
    private function onCompanyPostSaveMethodCall($isNew)
91
    {
92
        $companyId = 1;
93
        $changes   = ['changes'];
94
        $ip        = '127.0.0.2';
95
96
        $log = [
97
            'bundle'    => 'lead',
98
            'object'    => 'company',
99
            'objectId'  => $companyId,
100
            'action'    => ($isNew) ? 'create' : 'update',
101
            'details'   => $changes,
102
            'ipAddress' => $ip,
103
        ];
104
105
        $ipLookupHelper = $this->createMock(IpLookupHelper::class);
106
        $ipLookupHelper->expects($this->once())
107
            ->method('getIpAddressFromRequest')
108
            ->will($this->returnValue($ip));
109
110
        $auditLogModel = $this->createMock(AuditLogModel::class);
111
        $auditLogModel->expects($this->once())
112
            ->method('writeToLog')
113
            ->with($log);
114
115
        $subscriber = new CompanySubscriber($ipLookupHelper, $auditLogModel);
116
117
        $company = $this->createMock(Company::class);
118
        $company->expects($this->once())
119
            ->method('getId')
120
            ->will($this->returnValue($companyId));
121
122
        $event = $this->createMock(CompanyEvent::class);
123
        $event->expects($this->once())
124
            ->method('getCompany')
125
            ->will($this->returnValue($company));
126
        $event->expects($this->once())
127
            ->method('getChanges')
128
            ->will($this->returnValue($changes));
129
        $event->expects($this->once())
130
            ->method('isNew')
131
            ->will($this->returnValue($isNew));
132
133
        $subscriber->onCompanyPostSave($event);
134
    }
135
}
136