Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add filter param scenario test case
- Loading branch information
Showing
6 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/Matiux/DDDStarterPack/Aggregate/Domain/Repository/Filter/Test/FilterApplierScenario.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,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DDDStarterPack\Aggregate\Domain\Repository\Filter\Test; | ||
|
||
use DDDStarterPack\Aggregate\Domain\Repository\Filter\FilterBuilder; | ||
use DDDStarterPack\Aggregate\Domain\Repository\Test\DoctrineUtil; | ||
use Doctrine\ORM\Query\Parameter; | ||
use Doctrine\ORM\QueryBuilder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @psalm-suppress PropertyNotSetInConstructor | ||
*/ | ||
class FilterApplierScenario | ||
{ | ||
private array $filters; | ||
|
||
public function __construct( | ||
private TestCase $testCase, | ||
private FilterBuilder $filterParamBuilder, | ||
private QueryBuilder $queryBuilder, | ||
) { | ||
} | ||
|
||
public function whenFiltersApplied(array $filters): self | ||
{ | ||
$this->filters = $filters; | ||
|
||
$filterParams = $this->filterParamBuilder->build($filters); | ||
$filterParams->applyToTarget($this->queryBuilder); | ||
|
||
return $this; | ||
} | ||
|
||
public function thenQueryBuilt(string $expectedQuery): self | ||
{ | ||
/** @var string $actual */ | ||
$actual = $this->queryBuilder->getQuery()->getDQL(); | ||
|
||
DoctrineUtil::assertDQLEquals($expectedQuery, $actual); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @psalm-suppress MixedArgument | ||
* | ||
* Verify that the values of the requested filters are equal to the values bind into the query. | ||
*/ | ||
public function andFilterValuesMatch(): self | ||
{ | ||
$this->testCase::assertEquals( | ||
array_values($this->filters), | ||
array_map( | ||
fn (string $val) => trim($val, '%'), | ||
array_map( | ||
fn (Parameter $val) => (string) $val->getValue(), | ||
$this->queryBuilder->getParameters()->toArray(), | ||
), | ||
), | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
public function queryBuilder(): QueryBuilder | ||
{ | ||
return $this->queryBuilder; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../DDDStarterPack/Aggregate/Domain/Repository/Filter/Test/FilterApplierScenarioTestCase.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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DDDStarterPack\Aggregate\Domain\Repository\Filter\Test; | ||
|
||
use DDDStarterPack\Aggregate\Domain\Repository\Filter\FilterBuilder; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\QueryBuilder; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
abstract class FilterApplierScenarioTestCase extends KernelTestCase | ||
{ | ||
protected FilterApplierScenario $scenario; | ||
protected EntityManagerInterface $em; | ||
|
||
protected function setUp(): void | ||
{ | ||
/** | ||
* @psalm-suppress PropertyTypeCoercion | ||
*/ | ||
$this->em = self::getContainer()->get(EntityManagerInterface::class); | ||
$this->createScenario(); | ||
} | ||
|
||
private function createScenario(): void | ||
{ | ||
$this->scenario = new FilterApplierScenario( | ||
$this, | ||
$this->createFilterParamBuilder(), | ||
$this->createQueryBuilder(), | ||
); | ||
} | ||
|
||
abstract protected function createFilterParamBuilder(): FilterBuilder; | ||
|
||
abstract protected function createQueryBuilder(): QueryBuilder; | ||
} |
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
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 |
---|---|---|
|
@@ -70,4 +70,4 @@ protected function getSingleTransformerNs(): string | |
{ | ||
return PersonDataTransformer::class; | ||
} | ||
} | ||
} |