Spaces:
No application file
No application file
File size: 6,581 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
<?php
namespace Mautic\WebhookBundle\Tests\Helper;
use Doctrine\Common\Collections\ArrayCollection;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Mautic\CoreBundle\Entity\IpAddress;
use Mautic\LeadBundle\Entity\Company;
use Mautic\LeadBundle\Entity\CompanyRepository;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\CompanyModel;
use Mautic\WebhookBundle\Helper\CampaignHelper;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
final class CampaignHelperTest extends \PHPUnit\Framework\TestCase
{
/**
* @var MockObject&Lead
*/
private MockObject $contact;
/**
* @var MockObject|Client
*/
private MockObject $client;
/**
* @var MockObject|CompanyModel
*/
private MockObject $companyModel;
/**
* @var MockObject|CompanyRepository
*/
private MockObject $companyRepository;
/**
* @var ArrayCollection<int,IpAddress>
*/
private ArrayCollection $ipCollection;
private CampaignHelper $campaignHelper;
/**
* @var MockObject|EventDispatcherInterface
*/
private MockObject $dispatcher;
protected function setUp(): void
{
parent::setUp();
$this->contact = $this->createMock(Lead::class);
$this->client = $this->createMock(Client::class);
$this->companyModel = $this->createMock(CompanyModel::class);
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->ipCollection = new ArrayCollection();
$this->companyRepository = $this->getMockBuilder(CompanyRepository::class)
->disableOriginalConstructor()
->onlyMethods(['getCompaniesByLeadId'])
->getMock();
$this->companyRepository->method('getCompaniesByLeadId')->willReturn([new Company()]);
$this->companyModel->method('getRepository')->willReturn($this->companyRepository);
$this->campaignHelper = new CampaignHelper($this->client, $this->companyModel, $this->dispatcher);
$this->ipCollection->add((new IpAddress())->setIpAddress('127.0.0.1'));
$this->ipCollection->add((new IpAddress())->setIpAddress('127.0.0.2'));
$this->contact->expects($this->once())
->method('getProfileFields')
->willReturn(['email' => '[email protected]', 'company' => 'Mautic']);
$this->contact->expects($this->once())
->method('getIpAddresses')
->willReturn($this->ipCollection);
}
public function testFireWebhookWithGet(): void
{
$expectedUrl = 'https://mautic.org?test=tee&email=john%40doe.email&IP=127.0.0.1%2C127.0.0.2';
$this->client->expects($this->once())
->method('get')
->with($expectedUrl, [
\GuzzleHttp\RequestOptions::HEADERS => ['test' => 'tee', 'company' => 'Mautic'],
\GuzzleHttp\RequestOptions::TIMEOUT => 10,
])
->willReturn(new Response(200));
$this->campaignHelper->fireWebhook($this->provideSampleConfig(), $this->contact);
}
public function testFireWebhookWithPost(): void
{
$config = $this->provideSampleConfig('post');
$this->client->expects($this->once())
->method('request')
->with('post', 'https://mautic.org', [
\GuzzleHttp\RequestOptions::FORM_PARAMS => ['test' => 'tee', 'email' => '[email protected]', 'IP' => '127.0.0.1,127.0.0.2'],
\GuzzleHttp\RequestOptions::HEADERS => ['test' => 'tee', 'company' => 'Mautic'],
\GuzzleHttp\RequestOptions::TIMEOUT => 10,
])
->willReturn(new Response(200));
$this->campaignHelper->fireWebhook($config, $this->contact);
}
public function testFireWebhookWithPostJson(): void
{
$config = $this->provideSampleConfig('post', 'application/json');
$this->client->expects($this->once())
->method('request')
->with('post', 'https://mautic.org', [
\GuzzleHttp\RequestOptions::HEADERS => [
'test' => 'tee',
'company' => 'Mautic',
'content-type' => 'application/json',
],
\GuzzleHttp\RequestOptions::TIMEOUT => 10,
\GuzzleHttp\RequestOptions::BODY => json_encode(
['test' => 'tee', 'email' => '[email protected]', 'IP' => '127.0.0.1,127.0.0.2']
),
])
->willReturn(new Response(200));
$this->campaignHelper->fireWebhook($config, $this->contact);
}
public function testFireWebhookWhenReturningNotFound(): void
{
$this->client->expects($this->once())
->method('get')
->willReturn(new Response(404));
$this->expectException(\OutOfRangeException::class);
$this->campaignHelper->fireWebhook($this->provideSampleConfig(), $this->contact);
}
/**
* @return array<string,mixed>
*/
private function provideSampleConfig(string $method = 'get', string $type = 'application/x-www-form-urlencoded'): array
{
$sample = [
'url' => 'https://mautic.org',
'method' => $method,
'timeout' => 10,
'additional_data' => [
'list' => [
[
'label' => 'test',
'value' => 'tee',
],
[
'label' => 'email',
'value' => '{contactfield=email}',
],
[
'label' => 'IP',
'value' => '{contactfield=ipAddress}',
],
],
],
'headers' => [
'list' => [
[
'label' => 'test',
'value' => 'tee',
],
[
'label' => 'company',
'value' => '{contactfield=company}',
],
],
],
];
if ('application/json' == $type) {
array_push($sample['headers']['list'],
[
'label' => 'content-type',
'value' => 'application/json',
]);
}
return $sample;
}
}
|