Issues (3627)

Tests/EventListener/SegmentSubscriberTest.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\LeadList;
17
use Mautic\LeadBundle\Event\LeadListEvent as SegmentEvent;
18
use Mautic\LeadBundle\EventListener\SegmentSubscriber;
19
use Mautic\LeadBundle\LeadEvents;
20
21
class SegmentSubscriberTest 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 SegmentSubscriber($ipLookupHelper, $auditLogModel);
28
29
        $this->assertEquals(
30
            [
31
                LeadEvents::LIST_POST_SAVE   => ['onSegmentPostSave', 0],
32
                LeadEvents::LIST_POST_DELETE => ['onSegmentDelete', 0],
33
            ],
34
            $subscriber->getSubscribedEvents()
35
        );
36
    }
37
38
    public function testOnSegmentPostSave()
39
    {
40
        $this->onSegmentPostSaveMethodCall(false); // update segment log
41
        $this->onSegmentPostSaveMethodCall(true); // create segment log
42
    }
43
44
    public function testOnSegmentDelete()
45
    {
46
        $segmentId        = 1;
47
        $segmentName      = 'name';
48
        $ip               = '127.0.0.2';
49
        $log              = [
50
            'bundle'    => 'lead',
51
            'object'    => 'segment',
52
            'objectId'  => $segmentId,
53
            'action'    => 'delete',
54
            'details'   => ['name', $segmentName],
55
            'ipAddress' => $ip,
56
        ];
57
58
        $ipLookupHelper = $this->createMock(IpLookupHelper::class);
59
        $ipLookupHelper->expects($this->once())
60
            ->method('getIpAddressFromRequest')
61
            ->will($this->returnValue($ip));
62
63
        $auditLogModel = $this->createMock(AuditLogModel::class);
64
        $auditLogModel->expects($this->once())
65
            ->method('writeToLog')
66
            ->with($log);
67
68
        $subscriber = new SegmentSubscriber($ipLookupHelper, $auditLogModel);
69
70
        $segment            = $this->createMock(LeadList::class);
71
        $segment->deletedId = $segmentId;
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...
72
        $segment->expects($this->once())
73
            ->method('getName')
74
            ->will($this->returnValue($segmentName));
75
76
        $event = $this->createMock(SegmentEvent::class);
77
        $event->expects($this->once())
78
            ->method('getList')
79
            ->will($this->returnValue($segment));
80
81
        $subscriber->onSegmentDelete($event);
82
    }
83
84
    /**
85
     * Test create or update segment logging.
86
     *
87
     * @param bool $isNew
88
     */
89
    private function onSegmentPostSaveMethodCall($isNew)
90
    {
91
        $segmentId = 1;
92
        $changes   = ['changes'];
93
        $ip        = '127.0.0.2';
94
95
        $log = [
96
            'bundle'    => 'lead',
97
            'object'    => 'segment',
98
            'objectId'  => $segmentId,
99
            'action'    => ($isNew) ? 'create' : 'update',
100
            'details'   => $changes,
101
            'ipAddress' => $ip,
102
        ];
103
104
        $ipLookupHelper = $this->createMock(IpLookupHelper::class);
105
        $ipLookupHelper->expects($this->once())
106
            ->method('getIpAddressFromRequest')
107
            ->will($this->returnValue($ip));
108
109
        $auditLogModel = $this->createMock(AuditLogModel::class);
110
        $auditLogModel->expects($this->once())
111
            ->method('writeToLog')
112
            ->with($log);
113
114
        $subscriber = new SegmentSubscriber($ipLookupHelper, $auditLogModel);
115
116
        $segment = $this->createMock(LeadList::class);
117
        $segment->expects($this->once())
118
            ->method('getId')
119
            ->will($this->returnValue($segmentId));
120
121
        $event = $this->createMock(SegmentEvent::class);
122
        $event->expects($this->once())
123
            ->method('getList')
124
            ->will($this->returnValue($segment));
125
        $event->expects($this->once())
126
            ->method('getChanges')
127
            ->will($this->returnValue($changes));
128
        $event->expects($this->once())
129
            ->method('isNew')
130
            ->will($this->returnValue($isNew));
131
132
        $subscriber->onSegmentPostSave($event);
133
    }
134
}
135