Skip to content

Commit

Permalink
Fix not using mapped operator when method is comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
dave-redfern committed Jan 29, 2023
1 parent 1114dd2 commit a6eb5dc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
==========

2023-01-29 - 5.2.3
------------------

* Fix bug in API Expression to DBAL helper not using correct operator mapping when using comparison

2023-01-29 - 5.2.2
------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ private function buildExpression(APIExpression $where, QueryBuilder $qb): Compos
}

if ('comparison' === $method) {
$parts[] = $qb->expr()->comparison($this->mapField($part->field), $part->operator, ':' . array_keys($values)[0]);
$parts[] = $qb->expr()->comparison(
$this->mapField($part->field),
$this->mapFieldOperator($part->field, $part->operator),
':' . array_keys($values)[0]
);
} else {
$parts[] = $qb->expr()->$method($this->mapField($part->field), ':' . array_keys($values)[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
use PHPUnit\Framework\TestCase;
use Somnambulist\Bundles\ApiBundle\Request\Filters\ApplyApiExpressionsToDBALQueryBuilder;
use Somnambulist\Bundles\ApiBundle\Request\Filters\Decoders\CompoundNestedArrayFilterDecoder;
use Somnambulist\Bundles\ApiBundle\Request\Filters\Decoders\SimpleApiFilterDecoder;
use Somnambulist\Bundles\ApiBundle\Tests\Support\Stubs\Forms\SearchFormRequest;
use Somnambulist\Bundles\FormRequestBundle\Http\FormRequest;
use Somnambulist\Components\ApiClient\Client\Query\Encoders\CompoundNestedArrayEncoder;
use Somnambulist\Components\ApiClient\Client\Query\Encoders\SimpleEncoder;
use Somnambulist\Components\ApiClient\Client\Query\QueryBuilder;
use Symfony\Component\HttpFoundation\Request;

Expand Down Expand Up @@ -170,4 +172,33 @@ public function testOperatorMappingOfILike()

$this->assertEquals('SELECT WHERE table.name ILIKE :table_name_0', $qb->getSQL());
}

public function testOperatorMappingOfILikeSimpleDecoder()
{
$qb = new QueryBuilder();
$qb->where($qb->expr()->eq('this', 'that'));

$queryString = http_build_query((new SimpleEncoder())->encode($qb));

$GET = [];
parse_str($queryString, $GET);

$formRequest = new SearchFormRequest(new Request($GET));
FormRequest::appendValidationData($formRequest, $GET);
$parser = new SimpleApiFilterDecoder();
$result = $parser->decode($formRequest);

$qb = new \Doctrine\DBAL\Query\QueryBuilder(DriverManager::getConnection(['url' => 'sqlite:///:in-memory:']));

(new ApplyApiExpressionsToDBALQueryBuilder(
[
'this' => 'table.name',
],
[
'this' => 'ILIKE',
]
))->apply($result, $qb);

$this->assertEquals('SELECT WHERE table.name ILIKE :table_name_0', $qb->getSQL());
}
}

0 comments on commit a6eb5dc

Please sign in to comment.