-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a few unit tests for the Filter trait
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\RulerZ\Executor; | ||
|
||
use Doctrine\ORM\AbstractQuery as Query; | ||
use Doctrine\ORM\QueryBuilder; | ||
use PHPUnit\Framework\TestCase; | ||
use RulerZ\Context\ExecutionContext; | ||
use Tests\RulerZ\Stub\ExecutorStub; | ||
|
||
class FilterTraitTest extends TestCase | ||
{ | ||
/** @var ExecutorStub */ | ||
private $executor; | ||
|
||
public function setUp() | ||
{ | ||
$this->executor = new ExecutorStub(); | ||
} | ||
|
||
public function testItCanApplyAFilterOnATarget() | ||
{ | ||
$sql = 'some SQL generated by RulerZ'; | ||
|
||
$queryBuilder = $this->createMock(QueryBuilder::class); | ||
|
||
$queryBuilder->expects($this->once())->method('setParameter')->with('foo', 'bar', $type = null); | ||
$queryBuilder->expects($this->once())->method('andWhere')->with($sql); | ||
|
||
ExecutorStub::$executeReturn = $sql; | ||
|
||
$filteretTarget = $this->executor->applyFilter($queryBuilder, $parameters = ['foo' => 'bar'], $operators = [], new ExecutionContext()); | ||
|
||
$this->assertSame($queryBuilder, $filteretTarget, 'The trait is called and it returns the result generated by the executor'); | ||
} | ||
|
||
public function testItCanApplyDetectedJoins() | ||
{ | ||
$sql = 'some SQL generated by RulerZ'; | ||
|
||
$queryBuilder = $this->createMock(QueryBuilder::class); | ||
|
||
$queryBuilder->expects($this->once())->method('leftJoin')->with('root_alias.join_column', 'join_alias'); | ||
$queryBuilder->expects($this->once())->method('andWhere')->with($sql); | ||
|
||
ExecutorStub::$executeReturn = $sql; | ||
$this->executor->detectedJoins = [ | ||
[ | ||
'root' => 'root_alias', | ||
'column' => 'join_column', | ||
'as' => 'join_alias', | ||
], | ||
]; | ||
|
||
$filteretTarget = $this->executor->applyFilter($queryBuilder, $parameters = [], $operators = [], new ExecutionContext()); | ||
|
||
$this->assertSame($queryBuilder, $filteretTarget, 'The trait is called and it returns the result generated by the executor'); | ||
} | ||
|
||
public function testItCanExecuteTheRequestAndReturnTheResultsAsATraversableObject() | ||
{ | ||
$sql = 'some SQL generated by RulerZ'; | ||
$results = ['result']; | ||
|
||
$queryBuilder = $this->createMock(QueryBuilder::class); | ||
$query = $this->createMock(Query::class); | ||
|
||
$queryBuilder->expects($this->once())->method('andWhere')->with($sql); | ||
|
||
$queryBuilder->method('getQuery')->willReturn($query); | ||
$query->method('getResult')->willReturn($results); | ||
|
||
ExecutorStub::$executeReturn = $sql; | ||
|
||
$returnedResults = $this->executor->filter($queryBuilder, $parameters = [], $operators = [], new ExecutionContext()); | ||
|
||
$this->assertInstanceOf(\Traversable::class, $returnedResults); | ||
$this->assertEquals($results, iterator_to_array($returnedResults)); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\RulerZ\Stub; | ||
|
||
use RulerZ\DoctrineORM\Executor\FilterTrait; | ||
|
||
class ExecutorStub | ||
{ | ||
public static $executeReturn; | ||
|
||
public $detectedJoins = []; | ||
|
||
use FilterTrait; | ||
|
||
public function execute($target, array $operators, array $parameters) | ||
{ | ||
return self::$executeReturn; | ||
} | ||
} |