Issues (3627)

WebhookBundle/Tests/Helper/CampaignHelperTest.php (2 issues)

1
<?php
2
3
/*
4
 * @copyright   2018 Mautic Contributors. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\WebhookBundle\Tests\Helper;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Joomla\Http\Http;
16
use Mautic\CoreBundle\Entity\IpAddress;
17
use Mautic\LeadBundle\Entity\Company;
18
use Mautic\LeadBundle\Entity\CompanyRepository;
19
use Mautic\LeadBundle\Entity\Lead;
20
use Mautic\LeadBundle\Model\CompanyModel;
21
use Mautic\WebhookBundle\Helper\CampaignHelper;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
24
class CampaignHelperTest extends \PHPUnit\Framework\TestCase
25
{
26
    private $contact;
27
    private $connector;
28
    private $companyModel;
29
    private $companyRepository;
30
31
    /**
32
     * @var ArrayCollection
33
     */
34
    private $ipCollection;
35
36
    /**
37
     * @var CampaignHelper
38
     */
39
    private $campaignHelper;
40
41
    /**
42
     * @var \PHPUnit\Framework\MockObject\MockObject|EventDispatcherInterface
43
     */
44
    private $dispatcher;
45
46
    protected function setUp(): void
47
    {
48
        parent::setUp();
49
50
        $this->contact           = $this->createMock(Lead::class);
51
        $this->connector         = $this->createMock(Http::class);
52
        $this->companyModel      = $this->createMock(CompanyModel::class);
53
        $this->dispatcher        = $this->createMock(EventDispatcherInterface::class);
54
        $this->ipCollection      = new ArrayCollection();
55
        $this->companyRepository = $this->getMockBuilder(CompanyRepository::class)
56
        ->disableOriginalConstructor()
57
        ->setMethods(['getCompaniesByLeadId'])
58
        ->getMock();
59
60
        $this->companyRepository->method('getCompaniesByLeadId')
61
        ->willReturn([new Company()]);
62
63
        $this->companyModel->method('getRepository')
64
        ->willReturn($this->companyRepository);
65
66
        $this->campaignHelper = new CampaignHelper($this->connector, $this->companyModel, $this->dispatcher);
67
68
        $this->ipCollection->add((new IpAddress())->setIpAddress('127.0.0.1'));
69
        $this->ipCollection->add((new IpAddress())->setIpAddress('127.0.0.2'));
70
71
        $this->contact->expects($this->once())
72
            ->method('getProfileFields')
73
            ->willReturn(['email' => '[email protected]', 'company' => 'Mautic']);
74
75
        $this->contact->expects($this->once())
76
            ->method('getIpAddresses')
77
            ->willReturn($this->ipCollection);
78
    }
79
80
    public function testFireWebhookWithGet()
81
    {
82
        $expectedUrl = 'https://mautic.org?test=tee&email=john%40doe.email&IP=127.0.0.1%2C127.0.0.2';
83
84
        $this->connector->expects($this->once())
85
            ->method('get')
86
            ->with($expectedUrl, ['test' => 'tee', 'company' => 'Mautic'], 10)
87
            ->willReturn((object) ['code' => 200]);
88
89
        $this->campaignHelper->fireWebhook($this->provideSampleConfig(), $this->contact);
90
    }
91
92
    public function testFireWebhookWithPost()
93
    {
94
        $config      = $this->provideSampleConfig('post');
95
        $expectedUrl = 'https://mautic.org?test=tee&email=john%40doe.email&IP=127.0.0.1%2C127.0.0.2';
96
97
        $this->connector->expects($this->once())
98
            ->method('post')
99
            ->with('https://mautic.org', ['test'  => 'tee', 'email' => '[email protected]', 'IP' => '127.0.0.1,127.0.0.2'], ['test' => 'tee', 'company' => 'Mautic'], 10)
100
            ->willReturn((object) ['code' => 200]);
101
102
        $this->campaignHelper->fireWebhook($config, $this->contact);
103
    }
104
105
    public function testFireWebhookWithPostJson()
106
    {
107
        $config      = $this->provideSampleConfig('post');
0 ignored issues
show
The assignment to $config is dead and can be removed.
Loading history...
108
        $expectedUrl = 'https://mautic.org?test=tee&email=john%40doe.email&IP=127.0.0.1%2C127.0.0.2';
0 ignored issues
show
The assignment to $expectedUrl is dead and can be removed.
Loading history...
109
110
        $config      = $this->provideSampleConfig('post', 'application/json');
111
        $this->connector->expects($this->once())
112
            ->method('post')
113
            ->with('https://mautic.org', json_encode(['test'  => 'tee', 'email' => '[email protected]', 'IP' => '127.0.0.1,127.0.0.2']), ['test' => 'tee', 'company' => 'Mautic', 'content-type' => 'application/json'], 10)
114
            ->willReturn((object) ['code' => 200]);
115
116
        $this->campaignHelper->fireWebhook($config, $this->contact);
117
    }
118
119
    public function testFireWebhookWhenReturningNotFound()
120
    {
121
        $this->connector->expects($this->once())
122
            ->method('get')
123
            ->willReturn((object) ['code' => 404]);
124
125
        $this->expectException(\OutOfRangeException::class);
126
127
        $this->campaignHelper->fireWebhook($this->provideSampleConfig(), $this->contact);
128
    }
129
130
    private function provideSampleConfig($method = 'get', $type = 'application/x-www-form-urlencoded')
131
    {
132
        $sample = [
133
            'url'             => 'https://mautic.org',
134
            'method'          => $method,
135
            'timeout'         => 10,
136
            'additional_data' => [
137
                'list' => [
138
                    [
139
                        'label' => 'test',
140
                        'value' => 'tee',
141
                    ],
142
                    [
143
                        'label' => 'email',
144
                        'value' => '{contactfield=email}',
145
                    ],
146
                    [
147
                        'label' => 'IP',
148
                        'value' => '{contactfield=ipAddress}',
149
                    ],
150
                ],
151
            ],
152
            'headers' => [
153
                'list' => [
154
                    [
155
                        'label' => 'test',
156
                        'value' => 'tee',
157
                    ],
158
                    [
159
                        'label' => 'company',
160
                        'value' => '{contactfield=company}',
161
                    ],
162
                ],
163
            ],
164
        ];
165
        if ('application/json' == $type) {
166
            array_push($sample['headers']['list'],
167
            [
168
                'label' => 'content-type',
169
                'value' => 'application/json',
170
            ]);
171
        }
172
173
        return $sample;
174
    }
175
}
176