File size: 4,003 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
<?php

namespace Mautic\LeadBundle\Tests\Model;

use Mautic\CoreBundle\Helper\Serializer;
use Mautic\LeadBundle\Entity\LeadList;
use Mautic\LeadBundle\Model\ListModel;

class LeadListModelTest extends \PHPUnit\Framework\TestCase
{
    protected $fixture;

    protected function setUp(): void
    {
        $mockListModel = $this->getMockBuilder(ListModel::class)
            ->disableOriginalConstructor()
            ->onlyMethods(['getEntities', 'getEntity'])
            ->getMock();

        $mockListModel->expects($this->any())
            ->method('getEntity')
            ->willReturnCallback(function ($id) {
                $mockEntity = $this->getMockBuilder(LeadList::class)
                    ->disableOriginalConstructor()
                    ->onlyMethods(['getName'])
                    ->getMock();

                $mockEntity->expects($this->once())
                    ->method('getName')
                    ->willReturn((string) $id);

                return $mockEntity;
            });

        $filters = 'a:1:{i:0;a:7:{s:4:"glue";s:3:"and";s:5:"field";s:8:"leadlist";s:6:"object";s:4:"lead";s:4:"type";s:8:"leadlist";s:6:"filter";a:2:{i:0;i:1;i:1;i:3;}s:7:"display";N;s:8:"operator";s:2:"in";}}';

        $filters4 = 'a:1:{i:0;a:7:{s:4:"glue";s:3:"and";s:5:"field";s:8:"leadlist";s:6:"object";s:4:"lead";s:4:"type";s:8:"leadlist";s:6:"filter";a:1:{i:0;i:3;}s:7:"display";N;s:8:"operator";s:2:"in";}}';

        $mockEntity = $this->getMockBuilder(LeadList::class)
            ->disableOriginalConstructor()
            ->getMock();

        $mockEntity1 = clone $mockEntity;
        $mockEntity1->expects($this->once())
            ->method('getFilters')
            ->willReturn([]);
        $mockEntity1->expects($this->any())
            ->method('getId')
            ->willReturn(1);

        $mockEntity2 = clone $mockEntity;
        $mockEntity2->expects($this->once())
            ->method('getFilters')
            ->willReturn(Serializer::decode($filters));
        $mockEntity2->expects($this->any())
            ->method('getId')
            ->willReturn(2);

        $mockEntity3 = clone $mockEntity;
        $mockEntity3->expects($this->once())
            ->method('getFilters')
            ->willReturn([]);
        $mockEntity3->expects($this->any())
            ->method('getId')
            ->willReturn(3);

        $mockEntity4 = clone $mockEntity;
        $mockEntity4->expects($this->once())
            ->method('getFilters')
            ->willReturn(Serializer::decode($filters4));
        $mockEntity4->expects($this->any())
            ->method('getId')
            ->willReturn(4);

        $mockListModel->expects($this->once())
            ->method('getEntities')
            ->willReturn([
                1 => $mockEntity1,
                2 => $mockEntity2,
                3 => $mockEntity3,
                4 => $mockEntity4,
            ]);

        $this->fixture = $mockListModel;
    }

    /**
     * @dataProvider segmentTestDataProvider
     */
    public function testSegmentsCanBeDeletedCorrecty(array $arg, array $expected, $message): void
    {
        $result = $this->fixture->canNotBeDeleted($arg);

        $this->assertEquals($expected, $result, $message);
    }

    public static function segmentTestDataProvider()
    {
        return [
            [
                [1],
                [1 => '1'],
                '2 is dependent on 1, so 1 cannot be deleted.',
            ],
            [
                [1, 3],
                [1 => '1', 3 => '3'],
                '2 is dependent on 1 & 3, so 1 & 3 cannot be deleted.',
            ],
            [
                [1, 2, 3, 4],
                [],
                'Since we are deleting all segments, it should not prevent any from being deleted.',
            ],
            [
                [2],
                [],
                'Segments without any other segment dependent on them should always be able to be deleted.',
            ],
        ];
    }
}