Spaces:
No application file
No application file
File size: 3,315 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 |
<?php
namespace Mautic\EmailBundle\Tests\Helper;
use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\EmailBundle\Entity\Email;
use Mautic\EmailBundle\Helper\PointEventHelper;
use Mautic\EmailBundle\Model\EmailModel;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\LeadModel;
class PointEventHelperTest extends \PHPUnit\Framework\TestCase
{
public function testSendEmail(): void
{
$helper = new PointEventHelper();
$lead = new Lead();
$lead->setFields([
'core' => [
'email' => [
'value' => '[email protected]',
],
],
]);
$event = [
'id' => 1,
'properties' => [
'email' => 1,
],
];
$result = $helper->sendEmail($event, $lead, $this->getMockMauticFactory());
$this->assertEquals(true, $result);
$result = $helper->sendEmail($event, $lead, $this->getMockMauticFactory(false));
$this->assertEquals(false, $result);
$result = $helper->sendEmail($event, $lead, $this->getMockMauticFactory(true, false));
$this->assertEquals(false, $result);
$result = $helper->sendEmail($event, new Lead(), $this->getMockMauticFactory(true, false));
$this->assertEquals(false, $result);
}
/**
* @param bool $published
* @param bool $success
*
* @return \PHPUnit\Framework\MockObject\MockObject
*/
private function getMockMauticFactory($published = true, $success = true)
{
$mock = $this->getMockBuilder(MauticFactory::class)
->disableOriginalConstructor()
->onlyMethods(['getModel'])
->getMock();
$mock->expects($this->any())
->method('getModel')
->willReturnCallback(function ($model) use ($published, $success) {
switch ($model) {
case 'email':
return $this->getMockEmail($published, $success);
case 'lead':
return $this->getMockLead();
}
});
return $mock;
}
/**
* @return \PHPUnit\Framework\MockObject\MockObject
*/
private function getMockLead()
{
$mock = $this->getMockBuilder(LeadModel::class)
->disableOriginalConstructor()
->getMock();
return $mock;
}
/**
* @param bool $published
* @param bool $success
*
* @return \PHPUnit\Framework\MockObject\MockObject
*/
private function getMockEmail($published = true, $success = true)
{
$sendEmail = $success ? true : ['error' => 1];
$mock = $this->getMockBuilder(EmailModel::class)
->disableOriginalConstructor()
->onlyMethods(['getEntity', 'sendEmail'])
->getMock();
$mock->expects($this->any())
->method('getEntity')
->willReturnCallback(function ($id) use ($published) {
$email = new Email();
$email->setIsPublished($published);
return $email;
});
$mock->expects($this->any())
->method('sendEmail')
->willReturn($sendEmail);
return $mock;
}
}
|