Spaces:
No application file
No application file
File size: 5,735 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 |
<?php
namespace Mautic\LeadBundle\Tests\Entity;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\LeadBundle\Entity\Lead;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class LeadRepositoryFunctionalTest extends MauticMysqlTestCase
{
private Lead $lead;
protected function setUp(): void
{
parent::setUp();
$this->lead = $this->createLead();
}
public function testPointsAreAdded(): void
{
$model = static::getContainer()->get('mautic.lead.model.lead');
$this->lead->adjustPoints(100);
$model->saveEntity($this->lead);
$this->assertEquals(200, $this->lead->getPoints());
$changes = $this->lead->getChanges(true);
$this->assertEquals(200, $changes['points'][1]);
}
public function testPointsAreSubtracted(): void
{
$model = static::getContainer()->get('mautic.lead.model.lead');
$this->lead->adjustPoints(100, Lead::POINTS_SUBTRACT);
$model->saveEntity($this->lead);
$this->assertEquals(0, $this->lead->getPoints());
$changes = $this->lead->getChanges(true);
$this->assertEquals(0, $changes['points'][1]);
}
public function testPointsAreMultiplied(): void
{
$model = static::getContainer()->get('mautic.lead.model.lead');
$this->lead->adjustPoints(2, Lead::POINTS_MULTIPLY);
$model->saveEntity($this->lead);
$this->assertEquals(200, $this->lead->getPoints());
$changes = $this->lead->getChanges(true);
$this->assertEquals(200, $changes['points'][1]);
}
public function testPointsAreDivided(): void
{
$model = static::getContainer()->get('mautic.lead.model.lead');
$this->lead->adjustPoints(2, Lead::POINTS_DIVIDE);
$model->saveEntity($this->lead);
$this->assertEquals(50, $this->lead->getPoints());
$changes = $this->lead->getChanges(true);
$this->assertEquals(50, $changes['points'][1]);
}
public function testMixedOperatorPointsAreCalculated(): void
{
$model = static::getContainer()->get('mautic.lead.model.lead');
$this->lead->adjustPoints(100, Lead::POINTS_SUBTRACT);
$this->lead->adjustPoints(120, Lead::POINTS_ADD);
$this->lead->adjustPoints(2, Lead::POINTS_MULTIPLY);
$this->lead->adjustPoints(4, Lead::POINTS_DIVIDE);
$model->saveEntity($this->lead);
$this->assertEquals(60, $this->lead->getPoints());
$changes = $this->lead->getChanges(true);
$this->assertEquals(60, $changes['points'][1]);
}
public function testMixedModelAndRepositorySavesDoNotDoublePoints(): void
{
$model = static::getContainer()->get('mautic.lead.model.lead');
$this->lead->adjustPoints(120, Lead::POINTS_ADD);
$model->saveEntity($this->lead);
// Changes should be stored with points
$changes = $this->lead->getChanges(true);
$this->assertEquals(220, $changes['points'][1]);
// Points should now not be in changes
$model->saveEntity($this->lead);
$changes = $this->lead->getChanges(true);
$this->assertFalse(isset($changes['points']));
// Points should remain the same
$model->saveEntity($this->lead);
$this->em->getRepository(Lead::class)->saveEntity($this->lead);
$this->assertEquals(220, $this->lead->getPoints());
}
/**
* @param string[]|string $emails
*
* @dataProvider dataForTestAjaxGetLeadsByFieldValue
*/
public function testAjaxGetLeadsByFieldValue($emails, bool $createFlag, int $expectedCount): void
{
$this->createLeads($emails, $createFlag);
$payload = [
'action' => 'lead:getLeadIdsByFieldValue',
'field' => 'email',
'value' => $emails,
];
$this->client->request(Request::METHOD_GET, '/s/ajax', $payload, [], $this->createAjaxHeaders());
$this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
$contentArray = json_decode($this->client->getResponse()->getContent(), true);
$this->assertCount($expectedCount, $contentArray['items']);
}
/**
* @return array<string, array<int, int|string|bool|string[]>>
*/
public function dataForTestAjaxGetLeadsByFieldValue(): iterable
{
yield 'Email passed as string with associated contact' => [
'[email protected]', // Email
true,
1, // Count
];
yield 'Email passed as string without associated contact' => [
'[email protected]', // Email
false,
0, // Count
];
yield 'Email passed as array with associated contacts' => [
['[email protected]', '[email protected]'], // Email
true,
2, // Count
];
yield 'Email passed as array without associated contacts' => [
['[email protected]', '[email protected]'], // Email
false,
0, // Count
];
}
/**
* @param string[]|string $emails
*/
private function createLeads($emails, bool $flag): void
{
if (!$flag) {
return;
}
if (!is_array($emails)) {
$emails = [$emails];
}
foreach ($emails as $email) {
$this->createLead($email);
}
}
private function createLead(string $email = ''): Lead
{
$lead = new Lead();
$lead->setPoints(100);
if ($email) {
$lead->setEmail($email);
}
$this->em->persist($lead);
$this->em->flush();
return $lead;
}
}
|