Skip to content

Commit 9c48cc5

Browse files
committed
Add test on completeness of fields and terms in facets (+ UT)
1 parent b3d9e06 commit 9c48cc5

File tree

6 files changed

+141
-0
lines changed

6 files changed

+141
-0
lines changed

src/Query/Builder/FieldFacet.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
namespace MacFJA\RediSearch\Query\Builder;
2323

24+
use function count;
25+
use MacFJA\RediSearch\Exception\NotEnoughFieldsException;
2426
use MacFJA\RediSearch\Query\Escaper;
2527

2628
class FieldFacet implements QueryElement
@@ -35,6 +37,9 @@ class FieldFacet implements QueryElement
3537
*/
3638
public function __construct(array $fields, QueryElement $element)
3739
{
40+
if (0 === count($fields)) {
41+
throw new NotEnoughFieldsException();
42+
}
3843
$this->fields = array_map([Escaper::class, 'escapeFieldName'], $fields);
3944
$this->element = $element;
4045
}

src/Query/Builder/GeoFacet.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
namespace MacFJA\RediSearch\Query\Builder;
2323

24+
use function count;
25+
use MacFJA\RediSearch\Exception\NotEnoughFieldsException;
2426
use MacFJA\RediSearch\Exception\UnknownUnitException;
2527
use MacFJA\RediSearch\Redis\Command\SearchCommand\GeoFilterOption;
2628
use Respect\Validation\Rules\In;
@@ -32,6 +34,9 @@ class GeoFacet extends FieldFacet
3234
*/
3335
public function __construct(array $fields, float $lon, float $lat, float $radius, string $unit)
3436
{
37+
if (0 === count($fields)) {
38+
throw new NotEnoughFieldsException();
39+
}
3540
if (!(new In(GeoFilterOption::UNITS))->validate($unit)) {
3641
throw new UnknownUnitException($unit);
3742
}

src/Query/Builder/NumericFacet.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace MacFJA\RediSearch\Query\Builder;
2323

2424
use function is_array;
25+
use MacFJA\RediSearch\Exception\NotEnoughFieldsException;
2526
use MacFJA\RediSearch\Redis\Command\SearchCommand\NumericRangeTrait;
2627

2728
class NumericFacet extends FieldFacet
@@ -33,9 +34,13 @@ class NumericFacet extends FieldFacet
3334
*/
3435
public function __construct($fields, ?float $min, ?float $max, bool $isMinInclusive = true, bool $isMaxInclusive = true)
3536
{
37+
if (empty($fields)) {
38+
throw new NotEnoughFieldsException();
39+
}
3640
if (!is_array($fields)) {
3741
$fields = [$fields];
3842
}
43+
3944
$bounds = self::renderBound($min, $max, $isMinInclusive, $isMaxInclusive);
4045
$group = new EncapsulationGroup('[', ']', AndGroup::fromStrings(...$bounds));
4146
parent::__construct($fields, $group);

src/Query/Builder/TagFacet.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,21 @@
2121

2222
namespace MacFJA\RediSearch\Query\Builder;
2323

24+
use function count;
25+
use MacFJA\RediSearch\Exception\NotEnoughFieldsException;
26+
use MacFJA\RediSearch\Exception\NotEnoughTermsException;
27+
2428
class TagFacet extends FieldFacet
2529
{
2630
public function __construct(array $fields, string ...$values)
2731
{
32+
if (0 === count($fields)) {
33+
throw new NotEnoughFieldsException();
34+
}
35+
if (0 === count($values)) {
36+
throw new NotEnoughTermsException();
37+
}
38+
2839
$orGroup = new OrGroup(array_map(static function (string $value): QueryElement {
2940
return new Word($value);
3041
}, $values));

src/Query/Builder/TextFacet.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
namespace MacFJA\RediSearch\Query\Builder;
2323

2424
use function count;
25+
use MacFJA\RediSearch\Exception\NotEnoughFieldsException;
26+
use MacFJA\RediSearch\Exception\NotEnoughTermsException;
2527

2628
class TextFacet extends FieldFacet
2729
{
@@ -30,6 +32,13 @@ class TextFacet extends FieldFacet
3032
*/
3133
public function __construct(array $fields, string ...$values)
3234
{
35+
if (0 === count($fields)) {
36+
throw new NotEnoughFieldsException();
37+
}
38+
if (0 === count($values)) {
39+
throw new NotEnoughTermsException();
40+
}
41+
3342
$orGroup = new OrGroup(array_map(static function (string $value): QueryElement {
3443
return new ExactMatch($value);
3544
}, $values));
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright MacFJA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
10+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
namespace MacFJA\RediSearch\tests\Query\Builder;
23+
24+
use MacFJA\RediSearch\Exception\NotEnoughFieldsException;
25+
use MacFJA\RediSearch\Exception\NotEnoughTermsException;
26+
use MacFJA\RediSearch\Query\Builder\FieldFacet;
27+
use MacFJA\RediSearch\Query\Builder\GeoFacet;
28+
use MacFJA\RediSearch\Query\Builder\NumericFacet;
29+
use MacFJA\RediSearch\Query\Builder\RawElement;
30+
use MacFJA\RediSearch\Query\Builder\TagFacet;
31+
use MacFJA\RediSearch\Query\Builder\TextFacet;
32+
use MacFJA\RediSearch\Redis\Command\SearchCommand\GeoFilterOption;
33+
use PHPUnit\Framework\TestCase;
34+
35+
/**
36+
* @covers \MacFJA\RediSearch\Exception\NotEnoughFieldsException
37+
* @covers \MacFJA\RediSearch\Exception\NotEnoughTermsException
38+
*
39+
* @covers \MacFJA\RediSearch\Query\Builder\FieldFacet
40+
* @covers \MacFJA\RediSearch\Query\Builder\GeoFacet
41+
* @covers \MacFJA\RediSearch\Query\Builder\NumericFacet
42+
* @covers \MacFJA\RediSearch\Query\Builder\TagFacet
43+
* @covers \MacFJA\RediSearch\Query\Builder\TextFacet
44+
*
45+
* @uses \MacFJA\RediSearch\Query\Builder\RawElement
46+
*
47+
* @internal
48+
*/
49+
class IncompleteFacetTest extends TestCase
50+
{
51+
public function testTagFacetMissingTerms(): void
52+
{
53+
$this->expectException(NotEnoughTermsException::class);
54+
$this->expectExceptionMessage('You must have at least one term');
55+
56+
new TagFacet(['text']);
57+
}
58+
59+
public function testTagFacetMissingFields(): void
60+
{
61+
$this->expectException(NotEnoughFieldsException::class);
62+
$this->expectExceptionMessage('You must have at least one field');
63+
64+
new TagFacet([], 'foo');
65+
}
66+
67+
public function testTextFacetMissingTerms(): void
68+
{
69+
$this->expectException(NotEnoughTermsException::class);
70+
$this->expectExceptionMessage('You must have at least one term');
71+
72+
new TextFacet(['text']);
73+
}
74+
75+
public function testTextFacetMissingFields(): void
76+
{
77+
$this->expectException(NotEnoughFieldsException::class);
78+
$this->expectExceptionMessage('You must have at least one field');
79+
80+
new TextFacet([], 'foo');
81+
}
82+
83+
public function testNumericFacetMissingFields(): void
84+
{
85+
$this->expectException(NotEnoughFieldsException::class);
86+
$this->expectExceptionMessage('You must have at least one field');
87+
88+
new NumericFacet([], 0, null);
89+
}
90+
91+
public function testGeoFacetMissingFields(): void
92+
{
93+
$this->expectException(NotEnoughFieldsException::class);
94+
$this->expectExceptionMessage('You must have at least one field');
95+
96+
new GeoFacet([], 0, 0, 5, GeoFilterOption::UNIT_METERS);
97+
}
98+
99+
public function testFieldFacetMissingFields(): void
100+
{
101+
$this->expectException(NotEnoughFieldsException::class);
102+
$this->expectExceptionMessage('You must have at least one field');
103+
104+
new FieldFacet([], new RawElement(''));
105+
}
106+
}

0 commit comments

Comments
 (0)