Spaces:
No application file
No application file
File size: 8,114 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 |
<?php
namespace Mautic\CampaignBundle\Tests\Entity;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder as DbalQueryBuilder;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder as OrmQueryBuilder;
use Mautic\CampaignBundle\Entity\ContactLimiterTrait;
use Mautic\CampaignBundle\Executioner\ContactFinder\Limiter\ContactLimiter;
use Mautic\CoreBundle\Test\Doctrine\MockedConnectionTrait;
class ContactLimiterTraitTest extends \PHPUnit\Framework\TestCase
{
use ContactLimiterTrait;
use MockedConnectionTrait;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|Connection
*/
private \PHPUnit\Framework\MockObject\MockObject $connection;
/**
* @var \PHPUnit\Framework\MockObject\MockObject|EntityManagerInterface
*/
private \PHPUnit\Framework\MockObject\MockObject $entityManager;
protected function setUp(): void
{
$this->connection = $this->getMockedConnection();
$expr = new ExpressionBuilder($this->connection);
$this->connection->method('getExpressionBuilder')
->willReturn($expr);
$this->entityManager = $this->createMock(EntityManagerInterface::class);
$this->entityManager->method('getExpressionBuilder')
->willReturn(new Expr());
}
public function testSpecificContactId(): void
{
$contactLimiter = new ContactLimiter(50, 1);
$qb = new DbalQueryBuilder($this->connection);
$this->updateQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE l.lead_id = :contactId LIMIT 50', $qb->getSQL());
$this->assertEquals(['contactId' => 1], $qb->getParameters());
$qb = new OrmQueryBuilder($this->entityManager);
$this->updateOrmQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE IDENTITY(l.lead) = :contact', $qb->getDQL());
$this->assertEquals(1, $qb->getParameter('contact')->getValue());
$this->assertEquals(50, $qb->getMaxResults());
}
public function testListOfContacts(): void
{
$contactLimiter = new ContactLimiter(50, null, null, null, [1, 2, 3]);
$qb = new DbalQueryBuilder($this->connection);
$this->updateQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE l.lead_id IN (:contactIds) LIMIT 50', $qb->getSQL());
$this->assertEquals(['contactIds' => [1, 2, 3]], $qb->getParameters());
$qb = new OrmQueryBuilder($this->entityManager);
$this->updateOrmQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE IDENTITY(l.lead) IN(:contactIds)', $qb->getDQL());
$this->assertEquals([1, 2, 3], $qb->getParameter('contactIds')->getValue());
$this->assertEquals(50, $qb->getMaxResults());
}
public function testMinContactId(): void
{
$contactLimiter = new ContactLimiter(50, null, 4, null);
$qb = new DbalQueryBuilder($this->connection);
$this->updateQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE l.lead_id >= :minContactId LIMIT 50', $qb->getSQL());
$this->assertEquals(['minContactId' => 4], $qb->getParameters());
$qb = new OrmQueryBuilder($this->entityManager);
$this->updateOrmQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE IDENTITY(l.lead) >= :minContactId', $qb->getDQL());
$this->assertEquals(4, $qb->getParameter('minContactId')->getValue());
$this->assertEquals(50, $qb->getMaxResults());
}
public function testBatchMinContactId(): void
{
$contactLimiter = new ContactLimiter(50, null, 4, null);
$qb = new DbalQueryBuilder($this->connection);
$contactLimiter->setBatchMinContactId(10);
$this->updateQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE l.lead_id >= :minContactId LIMIT 50', $qb->getSQL());
$this->assertEquals(['minContactId' => 10], $qb->getParameters());
$qb = new OrmQueryBuilder($this->entityManager);
$this->updateOrmQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE IDENTITY(l.lead) >= :minContactId', $qb->getDQL());
$this->assertEquals(10, $qb->getParameter('minContactId')->getValue());
$this->assertEquals(50, $qb->getMaxResults());
}
public function testMaxContactId(): void
{
$contactLimiter = new ContactLimiter(50, null, null, 10);
$qb = new DbalQueryBuilder($this->connection);
$this->updateQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE l.lead_id <= :maxContactId LIMIT 50', $qb->getSQL());
$this->assertEquals(['maxContactId' => 10], $qb->getParameters());
$qb = new OrmQueryBuilder($this->entityManager);
$this->updateOrmQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE IDENTITY(l.lead) <= :maxContactId', $qb->getDQL());
$this->assertEquals(10, $qb->getParameter('maxContactId')->getValue());
$this->assertEquals(50, $qb->getMaxResults());
}
public function testMinAndMaxContactId(): void
{
$contactLimiter = new ContactLimiter(50, null, 1, 10);
$qb = new DbalQueryBuilder($this->connection);
$this->updateQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE l.lead_id BETWEEN :minContactId AND :maxContactId LIMIT 50', $qb->getSQL());
$this->assertEquals(['minContactId' => 1, 'maxContactId' => 10], $qb->getParameters());
$qb = new OrmQueryBuilder($this->entityManager);
$this->updateOrmQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE IDENTITY(l.lead) BETWEEN :minContactId AND :maxContactId', $qb->getDQL());
$this->assertEquals(1, $qb->getParameter('minContactId')->getValue());
$this->assertEquals(10, $qb->getParameter('maxContactId')->getValue());
$this->assertEquals(50, $qb->getMaxResults());
}
public function testThreads(): void
{
$contactLimiter = new ContactLimiter(50, null, null, null, [], 1, 5);
$qb = new DbalQueryBuilder($this->connection);
$this->updateQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE MOD((l.lead_id + :threadShift), :maxThreads) = 0 LIMIT 50', $qb->getSQL());
$this->assertEquals(['threadShift' => 0, 'maxThreads' => 5], $qb->getParameters());
$qb = new OrmQueryBuilder($this->entityManager);
$this->updateOrmQueryFromContactLimiter('l', $qb, $contactLimiter);
$this->assertEquals('SELECT WHERE MOD((IDENTITY(l.lead) + :threadShift), :maxThreads) = 0', $qb->getDQL());
$this->assertEquals(0, $qb->getParameter('threadShift')->getValue());
$this->assertEquals(5, $qb->getParameter('maxThreads')->getValue());
$this->assertEquals(50, $qb->getMaxResults());
}
public function testMaxResultsIgnoredForCountQueries(): void
{
$contactLimiter = new ContactLimiter(50, 1);
$qb = new DbalQueryBuilder($this->connection);
$this->updateQueryFromContactLimiter('l', $qb, $contactLimiter, true);
$this->assertEquals('SELECT WHERE l.lead_id = :contactId', $qb->getSQL());
$this->assertEquals(['contactId' => 1], $qb->getParameters());
$qb = new OrmQueryBuilder($this->entityManager);
$this->updateOrmQueryFromContactLimiter('l', $qb, $contactLimiter, true);
$this->assertEquals('SELECT WHERE IDENTITY(l.lead) = :contact', $qb->getDQL());
$this->assertEquals(1, $qb->getParameter('contact')->getValue());
$this->assertEquals(null, $qb->getMaxResults());
}
}
|