Skip to content

Commit

Permalink
Fix bug in boolean filter builder
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jan 15, 2025
1 parent f205cfd commit e0ae3e5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
21 changes: 19 additions & 2 deletions src/Meilisearch/Filter/BooleanFilterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,28 @@ public function build(array $facets, array $facetsValues): array

foreach ($facets as $facet) {
if ($facet->type === 'bool' && isset($facetsValues[$facet->name])) {
$value = ($facetsValues[$facet->name] === '1') ? 'true' : 'false';
$filters = [$facet->name . ' = ' . $value];
$value = self::isTruthy($facetsValues[$facet->name]) ? 'true' : 'false';
$filters[] = sprintf('%s = %s', $facet->name, $value);
}
}

return $filters;
}

private static function isTruthy(mixed $value): bool
{
if (is_string($value)) {
return '1' === $value || 'true' === $value;
}

if (is_bool($value)) {
return $value;
}

if (is_int($value)) {
return 1 === $value;
}

return false;
}
}
26 changes: 13 additions & 13 deletions tests/Unit/Meilisearch/Builder/CompositeFilterBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@

use PHPUnit\Framework\TestCase;
use Setono\SyliusMeilisearchPlugin\Document\Metadata\Facet;
use Setono\SyliusMeilisearchPlugin\Meilisearch\Filter\ArrayFilterBuilder;
use Setono\SyliusMeilisearchPlugin\Meilisearch\Filter\BooleanFilterBuilder;
use Setono\SyliusMeilisearchPlugin\Meilisearch\Filter\CompositeFilterBuilder;
use Setono\SyliusMeilisearchPlugin\Meilisearch\Filter\FilterBuilderInterface;
use Setono\SyliusMeilisearchPlugin\Meilisearch\Filter\FloatFilterBuilder;

final class CompositeFilterBuilderTest extends TestCase
{
public function test_it_returns_filters(): void
{
$brandFilterBuilder = $this->createMock(FilterBuilderInterface::class);
$brandFilterBuilder->method('build')->willReturn(['(brand = "brand1")']);

$sizeFilterBuilder = $this->createMock(FilterBuilderInterface::class);
$sizeFilterBuilder->method('build')->willReturn(['(size = "size1" OR size = "size2")']);

$compositeFilterBuilder = new CompositeFilterBuilder();
$compositeFilterBuilder->add($brandFilterBuilder);
$compositeFilterBuilder->add($sizeFilterBuilder);
$compositeFilterBuilder->add(new ArrayFilterBuilder());
$compositeFilterBuilder->add(new BooleanFilterBuilder());
$compositeFilterBuilder->add(new FloatFilterBuilder());

$onSaleFacet = new Facet('onSale', 'bool');
$brandFacet = new Facet('brand', 'string');
$sizeFacet = new Facet('size', 'string');
$ecoFriendlyFacet = new Facet('ecoFriendly', 'bool');
$brandFacet = new Facet('brand', 'array');
$sizeFacet = new Facet('size', 'array');

$filters = $compositeFilterBuilder->build(
['onSale' => $onSaleFacet, 'brand' => $brandFacet, 'size' => $sizeFacet],
['onSale' => true, 'brand' => ['brand1'], 'size' => ['size1', 'size2']],
['onSale' => $onSaleFacet, 'ecoFriendly' => $ecoFriendlyFacet, 'brand' => $brandFacet, 'size' => $sizeFacet],
['onSale' => true, 'ecoFriendly' => true, 'brand' => ['brand1'], 'size' => ['size1', 'size2']],
);

$this->assertSame([
'(brand = "brand1")',
'(size = "size1" OR size = "size2")',
'onSale = true',
'ecoFriendly = true',
], $filters);
}
}

0 comments on commit e0ae3e5

Please sign in to comment.