-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid simple text replacements in SQL if possible
MAILPOET-4986
- Loading branch information
Showing
4 changed files
with
83 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
mailpoet/tests/integration/Segments/DynamicSegments/Filters/FilterHelperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace MailPoet\Segments\DynamicSegments\Filters; | ||
|
||
use MailPoet\Entities\SubscriberEntity; | ||
use MailPoetVendor\Doctrine\DBAL\Query\QueryBuilder; | ||
|
||
class FilterHelperTest extends \MailPoetTest { | ||
/** @var FilterHelper */ | ||
private $filterHelper; | ||
|
||
/** @var string */ | ||
private $subscribersTable; | ||
|
||
public function _before() { | ||
parent::_before(); | ||
$this->filterHelper = $this->diContainer->get(FilterHelper::class); | ||
$this->subscribersTable = $this->entityManager | ||
->getClassMetadata(SubscriberEntity::class) | ||
->getTableName(); | ||
} | ||
|
||
public function testItCanReturnSQLThatDoesNotIncludeParams(): void { | ||
$queryBuilder = $this->getSubscribersQueryBuilder(); | ||
$defaultResult = $queryBuilder->getSQL(); | ||
expect($defaultResult)->equals("SELECT id FROM $this->subscribersTable"); | ||
expect($this->filterHelper->getInterpolatedSQL($queryBuilder))->equals($defaultResult); | ||
} | ||
|
||
public function testItCanReturnInterpolatedSQL(): void { | ||
$queryBuilder = $this->getSubscribersQueryBuilder(); | ||
$queryBuilder->where("$this->subscribersTable.created_at < :date"); | ||
$queryBuilder->setParameter('date', '2023-03-09'); | ||
expect($this->filterHelper->getInterpolatedSQL($queryBuilder))->equals("SELECT id FROM $this->subscribersTable WHERE $this->subscribersTable.created_at < '2023-03-09'"); | ||
} | ||
|
||
public function testItProperlyInterpolatesArrayValues(): void { | ||
$queryBuilder = $this->getSubscribersQueryBuilder(); | ||
$queryBuilder->where("$this->subscribersTable.status IN (:statuses)"); | ||
$queryBuilder->setParameter('statuses', ['subscribed', 'inactive']); | ||
expect($this->filterHelper->getInterpolatedSQL($queryBuilder))->equals("SELECT id FROM $this->subscribersTable WHERE $this->subscribersTable.status IN ('subscribed','inactive')"); | ||
} | ||
|
||
private function getSubscribersQueryBuilder(): QueryBuilder { | ||
return $this->entityManager->getConnection()->createQueryBuilder()->select('id')->from($this->subscribersTable); | ||
} | ||
} |