Issues (3627)

Tests/Unit/IpLookup/MaxMindDoNotSellListTest.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Mautic\CoreBundle\Tests\Unit\IpLookup;
4
5
use Mautic\CoreBundle\Exception\BadConfigurationException;
6
use Mautic\CoreBundle\Exception\FileNotFoundException;
7
use Mautic\CoreBundle\Helper\CoreParametersHelper;
8
use Mautic\CoreBundle\IpLookup\DoNotSellList\MaxMindDoNotSellList;
9
10
class MaxMindDoNotSellListTest extends \PHPUnit\Framework\TestCase
11
{
12
    /**
13
     * @var \PHPUnit\Framework\MockObject\MockObject|CoreParametersHelper
14
     */
15
    private $coreParamsHelperMock;
16
17
    private $badFilePath = 'bad_list.json';
18
    private $badData     = 'bad data';
19
20
    private $goodFilePath = 'good_list.json';
21
    private $goodData     = '{
22
                                "exclusions": [
23
                                    {
24
                                      "exclusion_type": "ccpa_do_not_sell",
25
                                      "data_type": "network",
26
                                      "value": "108.208.26.166/32",
27
                                      "last_updated": "2020-01-08T18:58:38Z"
28
                                    }
29
                                ]
30
                              }';
31
32
    protected function setUp(): void
33
    {
34
        parent::setUp();
35
36
        $this->coreParamsHelperMock = $this->createMock(CoreParametersHelper::class);
37
38
        file_put_contents($this->badFilePath, $this->badData);
39
        file_put_contents($this->goodFilePath, $this->goodData);
40
    }
41
42
    protected function tearDown(): void
43
    {
44
        parent::tearDown();
45
46
        if (is_file($this->goodFilePath)) {
47
            unlink($this->goodFilePath);
48
        }
49
50
        if (is_file($this->badFilePath)) {
51
            unlink($this->badFilePath);
52
        }
53
    }
54
55
    /**
56
     * Test trying to load the list when the list file path hasn't been configured.
57
     */
58
    public function testListPathNotConfigured()
59
    {
60
        $coreParamsHelperMock = $this->coreParamsHelperMock;
61
        $coreParamsHelperMock->method('get')
0 ignored issues
show
The method method() does not exist on Mautic\CoreBundle\Helper\CoreParametersHelper. ( Ignorable by Annotation )

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

61
        $coreParamsHelperMock->/** @scrutinizer ignore-call */ 
62
                               method('get')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
            ->with('maxmind_do_not_sell_list_path')
63
            ->willReturn('');
64
65
        $this->expectException(BadConfigurationException::class);
66
67
        $doNotSellList = new MaxMindDoNotSellList($this->coreParamsHelperMock);
68
        $doNotSellList->loadList();
69
    }
70
71
    /**
72
     * Test trying to load the list when the list file path hasn't been configured.
73
     */
74
    public function testListFileNotDownloaded()
75
    {
76
        $coreParamsHelperMock = $this->coreParamsHelperMock;
77
        $coreParamsHelperMock->method('get')
78
            ->with('maxmind_do_not_sell_list_path')
79
            ->willReturn('path_to_missing_file.json');
80
81
        $this->expectException(FileNotFoundException::class);
82
83
        $doNotSellList = new MaxMindDoNotSellList($this->coreParamsHelperMock);
84
        $doNotSellList->loadList();
85
    }
86
87
    /**
88
     * Test loading a Do Not Sell List file that is not properly formatted.
89
     */
90
    public function testFileWithBadData()
91
    {
92
        $coreParamsHelperMock = $this->coreParamsHelperMock;
93
        $coreParamsHelperMock->method('get')
94
            ->with('maxmind_do_not_sell_list_path')
95
            ->willReturn($this->badFilePath);
96
97
        $doNotSellList = new MaxMindDoNotSellList($this->coreParamsHelperMock);
98
99
        $this->assertEquals($this->badFilePath, $doNotSellList->getListPath());
100
        $this->assertFalse($doNotSellList->loadList());
101
        $this->assertEquals([], $doNotSellList->getList());
102
    }
103
104
    /**
105
     * Test loading the Do Not Sell List file when everything goes right.
106
     */
107
    public function testSuccessfulFileLoad()
108
    {
109
        $coreParamsHelperMock = $this->coreParamsHelperMock;
110
        $coreParamsHelperMock->method('get')
111
            ->with('maxmind_do_not_sell_list_path')
112
            ->willReturn($this->goodFilePath);
113
114
        $doNotSellList = new MaxMindDoNotSellList($this->coreParamsHelperMock);
115
        $doNotSellList->loadList();
116
117
        $this->assertEquals($this->goodFilePath, $doNotSellList->getListPath());
118
119
        $goodData = (json_decode($this->goodData, true))['exclusions'];
120
        $this->assertEquals($goodData, $doNotSellList->getList());
121
    }
122
}
123