File size: 4,631 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php

declare(strict_types=1);

namespace Mautic\LeadBundle\Tests\Model;

use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\LeadModel;
use Mautic\LeadBundle\Model\SegmentActionModel;

class SegmentActionModelTest extends \PHPUnit\Framework\TestCase
{
    private \PHPUnit\Framework\MockObject\MockObject $contactMock5;

    private \PHPUnit\Framework\MockObject\MockObject $contactMock6;

    private \PHPUnit\Framework\MockObject\MockObject $contactModelMock;

    private SegmentActionModel $actionModel;

    protected function setUp(): void
    {
        $this->contactMock5        = $this->createMock(Lead::class);
        $this->contactMock6        = $this->createMock(Lead::class);
        $this->contactModelMock    = $this->createMock(LeadModel::class);
        $this->actionModel         = new SegmentActionModel($this->contactModelMock);
    }

    public function testAddContactsToSegmentsEntityAccess(): void
    {
        $contacts = [5, 6];
        $segments = [4, 5];

        $this->contactModelMock->expects($this->once())
            ->method('getLeadsByIds')
            ->with($contacts)
            ->willReturn([$this->contactMock5, $this->contactMock6]);

        $this->contactModelMock->expects($this->exactly(2))
            ->method('canEditContact')
            ->withConsecutive([$this->contactMock5], [$this->contactMock6])
            ->willReturnOnConsecutiveCalls(false, true);

        $this->contactModelMock->expects($this->once())
            ->method('addToLists')
            ->with($this->contactMock6, $segments);

        $this->contactModelMock->expects($this->once())
            ->method('saveEntities')
            ->with([$this->contactMock5, $this->contactMock6]);

        $this->actionModel->addContacts($contacts, $segments);
    }

    public function testRemoveContactsFromSementsEntityAccess(): void
    {
        $contacts = [5, 6];
        $segments = [1, 2];

        $this->contactModelMock->expects($this->once())
            ->method('getLeadsByIds')
            ->with($contacts)
            ->willReturn([$this->contactMock5, $this->contactMock6]);

        $this->contactModelMock->expects($this->exactly(2))
            ->method('canEditContact')
            ->withConsecutive([$this->contactMock5], [$this->contactMock6])
            ->willReturnOnConsecutiveCalls(false, true);

        $this->contactModelMock->expects($this->once())
            ->method('removeFromLists')
            ->with($this->contactMock6, $segments);

        $this->contactModelMock->expects($this->once())
            ->method('saveEntities')
            ->with([$this->contactMock5, $this->contactMock6]);

        $this->actionModel->removeContacts($contacts, $segments);
    }

    public function testAddContactsToSegments(): void
    {
        $contacts = [5, 6];
        $segments = [1, 2];

        $this->contactModelMock->expects($this->once())
            ->method('getLeadsByIds')
            ->with($contacts)
            ->willReturn([$this->contactMock5, $this->contactMock6]);

        $this->contactModelMock->expects($this->exactly(2))
            ->method('canEditContact')
            ->withConsecutive([$this->contactMock5], [$this->contactMock6])
            ->willReturn(true);

        $this->contactModelMock->expects($this->exactly(2))
            ->method('addToLists')
            ->withConsecutive([$this->contactMock5, $segments], [$this->contactMock6, $segments]);

        $this->contactModelMock->expects($this->once())
            ->method('saveEntities')
            ->with([$this->contactMock5, $this->contactMock6]);

        $this->actionModel->addContacts($contacts, $segments);
    }

    public function testRemoveContactsFromCategories(): void
    {
        $contacts = [5, 6];
        $segments = [1, 2];

        $this->contactModelMock->expects($this->once())
            ->method('getLeadsByIds')
            ->with($contacts)
            ->willReturn([$this->contactMock5, $this->contactMock6]);

        $this->contactModelMock->expects($this->exactly(2))
            ->method('canEditContact')
            ->withConsecutive([$this->contactMock5], [$this->contactMock6])
            ->willReturn(true);

        $this->contactModelMock->expects($this->exactly(2))
            ->method('removeFromLists')
            ->withConsecutive([$this->contactMock5, $segments], [$this->contactMock6]);

        $this->contactModelMock->expects($this->once())
            ->method('saveEntities')
            ->with([$this->contactMock5, $this->contactMock6]);

        $this->actionModel->removeContacts($contacts, $segments);
    }
}