Skip to content

Commit

Permalink
Add a few unit tests for the Filter trait
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Sep 28, 2018
1 parent cf5b30f commit d9c7fa9
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
82 changes: 82 additions & 0 deletions tests/Executor/FilterTraitTest.php
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));
}
}
21 changes: 21 additions & 0 deletions tests/Stub/ExecutorStub.php
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;
}
}

0 comments on commit d9c7fa9

Please sign in to comment.