Spaces:
No application file
No application file
File size: 7,509 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
<?php
namespace Mautic\CampaignBundle\Tests\Executioner;
use Doctrine\Common\Collections\ArrayCollection;
use Mautic\CampaignBundle\Entity\Campaign;
use Mautic\CampaignBundle\Entity\Event;
use Mautic\CampaignBundle\Executioner\ContactFinder\InactiveContactFinder;
use Mautic\CampaignBundle\Executioner\ContactFinder\Limiter\ContactLimiter;
use Mautic\CampaignBundle\Executioner\EventExecutioner;
use Mautic\CampaignBundle\Executioner\Helper\InactiveHelper;
use Mautic\CampaignBundle\Executioner\InactiveExecutioner;
use Mautic\CampaignBundle\Executioner\Scheduler\EventScheduler;
use Mautic\CoreBundle\Translation\Translator;
use Mautic\LeadBundle\Entity\Lead;
use Psr\Log\NullLogger;
use Symfony\Component\Console\Output\BufferedOutput;
class InactiveExecutionerTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \PHPUnit\Framework\MockObject\MockObject|InactiveContactFinder
*/
private \PHPUnit\Framework\MockObject\MockObject $inactiveContactFinder;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|Translator
*/
private \PHPUnit\Framework\MockObject\MockObject $translator;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|EventScheduler
*/
private \PHPUnit\Framework\MockObject\MockObject $eventScheduler;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|InactiveHelper
*/
private \PHPUnit\Framework\MockObject\MockObject $inactiveHelper;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|EventExecutioner
*/
private \PHPUnit\Framework\MockObject\MockObject $eventExecutioner;
protected function setUp(): void
{
$this->inactiveContactFinder = $this->getMockBuilder(InactiveContactFinder::class)
->disableOriginalConstructor()
->getMock();
$this->translator = $this->getMockBuilder(Translator::class)
->disableOriginalConstructor()
->getMock();
$this->eventScheduler = $this->getMockBuilder(EventScheduler::class)
->disableOriginalConstructor()
->getMock();
$this->inactiveHelper = $this->getMockBuilder(InactiveHelper::class)
->disableOriginalConstructor()
->getMock();
$this->eventExecutioner = $this->getMockBuilder(EventExecutioner::class)
->disableOriginalConstructor()
->getMock();
}
public function testNoContactsFoundResultsInNothingExecuted(): void
{
$campaign = $this->getMockBuilder(Campaign::class)
->getMock();
$campaign->expects($this->once())
->method('getEventsByType')
->willReturn(new ArrayCollection());
$this->inactiveContactFinder->expects($this->never())
->method('getContactCount');
$limiter = new ContactLimiter(0, 0, 0, 0);
$counter = $this->getExecutioner()->execute($campaign, $limiter, new BufferedOutput());
$this->assertEquals(0, $counter->getEvaluated());
}
public function testNoEventsFoundResultsInNothingExecuted(): void
{
$campaign = $this->getMockBuilder(Campaign::class)
->getMock();
$campaign->expects($this->once())
->method('getEventsByType')
->willReturn(new ArrayCollection([new Event()]));
$this->inactiveContactFinder->expects($this->once())
->method('getContactCount')
->willReturn(0);
$limiter = new ContactLimiter(0, 0, 0, 0);
$counter = $this->getExecutioner()->execute($campaign, $limiter, new BufferedOutput());
$this->assertEquals(0, $counter->getTotalEvaluated());
}
public function testNextBatchOfContactsAreExecuted(): void
{
$decision = new Event();
$campaign = $this->getMockBuilder(Campaign::class)
->getMock();
$campaign->expects($this->once())
->method('getEventsByType')
->willReturn(new ArrayCollection([$decision]));
$limiter = new ContactLimiter(0, 0, 0, 0);
$this->inactiveContactFinder->expects($this->once())
->method('getContactCount')
->willReturn(2);
$this->inactiveContactFinder->expects($this->exactly(3))
->method('getContacts')
->with(null, $decision, $limiter)
->willReturnOnConsecutiveCalls(
new ArrayCollection([3 => new Lead()]),
new ArrayCollection([10 => new Lead()]),
new ArrayCollection([])
);
$this->inactiveHelper->expects($this->exactly(2))
->method('getEarliestInactiveDateTime')
->willReturn(new \DateTime());
$this->eventScheduler->expects($this->exactly(2))
->method('getSortedExecutionDates')
->willReturn([]);
$this->getExecutioner()->execute($campaign, $limiter, new BufferedOutput());
}
public function testValidationExecutesNothingIfCampaignUnpublished(): void
{
$campaign = $this->getMockBuilder(Campaign::class)
->getMock();
$campaign->expects($this->once())
->method('isPublished')
->willReturn(false);
$event = new Event();
$event->setCampaign($campaign);
$this->inactiveHelper->expects($this->once())
->method('getCollectionByDecisionId')
->with(1)
->willReturn(new ArrayCollection([$event]));
$this->inactiveContactFinder->expects($this->never())
->method('getContacts');
$limiter = new ContactLimiter(0, 0, 0, 0);
$counter = $this->getExecutioner()->validate(1, $limiter, new BufferedOutput());
$this->assertEquals(0, $counter->getTotalEvaluated());
}
public function testValidationEvaluatesFoundEvents(): void
{
$campaign = $this->getMockBuilder(Campaign::class)
->getMock();
$campaign->expects($this->once())
->method('isPublished')
->willReturn(true);
$decision = new Event();
$decision->setCampaign($campaign);
$limiter = new ContactLimiter(0, 0, 0, 0);
$this->inactiveHelper->expects($this->once())
->method('getCollectionByDecisionId')
->with(1)
->willReturn(new ArrayCollection([$decision]));
$this->inactiveContactFinder->expects($this->once())
->method('getContactCount')
->willReturn(2);
$this->inactiveContactFinder->expects($this->exactly(3))
->method('getContacts')
->with(null, $decision, $limiter)
->willReturnOnConsecutiveCalls(
new ArrayCollection([3 => new Lead()]),
new ArrayCollection([10 => new Lead()]),
new ArrayCollection([])
);
$this->inactiveHelper->expects($this->exactly(2))
->method('getEarliestInactiveDateTime')
->willReturn(new \DateTime());
$this->eventScheduler->expects($this->exactly(2))
->method('getSortedExecutionDates')
->willReturn([]);
$this->getExecutioner()->validate(1, $limiter, new BufferedOutput());
}
private function getExecutioner()
{
return new InactiveExecutioner(
$this->inactiveContactFinder,
new NullLogger(),
$this->translator,
$this->eventScheduler,
$this->inactiveHelper,
$this->eventExecutioner
);
}
}
|