Skip to content

Commit 0843757

Browse files
committed
Fix on facet + more UT
1 parent 47c9295 commit 0843757

File tree

8 files changed

+292
-0
lines changed

8 files changed

+292
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3030
- Aggregation SortBy allow 0 (not limit) as value for Max
3131
- Fix helper removing _"nullish"_ values (`array_filter` is too lax by default)
3232
- Fix order in fields creation not being correct
33+
- Don't allow empty facet in search query
3334

3435
### Deprecated
3536

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\Search\Exception;
23+
24+
class NotEnoughTermsException extends \MacFJA\RediSearch\Words\Exception\NotEnoughTermsException
25+
{
26+
}

src/Search/QueryBuilder/TagFacet.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
namespace MacFJA\RediSearch\Search\QueryBuilder;
2323

2424
use function array_map;
25+
use function count;
26+
use MacFJA\RediSearch\Helper\DataHelper;
2527
use MacFJA\RediSearch\Helper\EscapeHelper;
28+
use MacFJA\RediSearch\Search\Exception\NotEnoughTermsException;
2629
use function sprintf;
2730

2831
class TagFacet implements PartialQuery
@@ -35,6 +38,7 @@ class TagFacet implements PartialQuery
3538

3639
public function __construct(string $field, string ...$orValues)
3740
{
41+
DataHelper::assert(count($orValues) > 0, NotEnoughTermsException::class);
3842
$this->field = $field;
3943
$this->orValues = $orValues;
4044
}

src/Search/QueryBuilder/TextFacet.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
use function array_map;
2525
use function count;
26+
use MacFJA\RediSearch\Helper\DataHelper;
2627
use MacFJA\RediSearch\Helper\EscapeHelper;
28+
use MacFJA\RediSearch\Search\Exception\NotEnoughTermsException;
2729
use function reset;
2830
use function sprintf;
2931
use function strpos;
@@ -42,6 +44,7 @@ class TextFacet implements PartialQuery
4244

4345
public function __construct(string $field, string ...$orValues)
4446
{
47+
DataHelper::assert(count($orValues) > 0, NotEnoughTermsException::class);
4548
$this->field = $field;
4649
$this->orValues = $orValues;
4750
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 Tests\MacFJA\RediSearch\Search\QueryBuilder;
23+
24+
use MacFJA\RediSearch\Search\Exception\UnknownUnitException;
25+
use MacFJA\RediSearch\Search\GeoFilter;
26+
use MacFJA\RediSearch\Search\QueryBuilder\GeoFacet;
27+
use MacFJA\RediSearch\Search\QueryBuilder\PartialQuery;
28+
use function PHPUnit\Framework\assertFalse;
29+
use function PHPUnit\Framework\assertSame;
30+
use PHPUnit\Framework\TestCase;
31+
use Tests\MacFJA\RediSearch\support\Assertion;
32+
33+
/**
34+
* @coversDefaultClass \MacFJA\RediSearch\Search\QueryBuilder\GeoFacet
35+
* @covers ::__construct
36+
*
37+
* @uses \MacFJA\RediSearch\Helper\EscapeHelper
38+
* @uses \MacFJA\RediSearch\Helper\DataHelper
39+
* @uses \MacFJA\RediSearch\Search\QueryBuilder\OrGroup
40+
* @uses \MacFJA\RediSearch\Search\QueryBuilder\Word
41+
* @uses \MacFJA\RediSearch\Search\Exception\UnknownUnitException
42+
*/
43+
class GeoFacetTest extends TestCase
44+
{
45+
use Assertion;
46+
47+
/**
48+
* @covers ::render
49+
*/
50+
public function testNominal(): void
51+
{
52+
self::assertSameRender('@foobar:[-74.044502 40.689247 40.000000 km]', new GeoFacet('foobar', -74.044502, 40.689247, 40, GeoFilter::UNIT_KILOMETERS));
53+
}
54+
55+
/**
56+
* @covers \MacFJA\RediSearch\Search\Exception\UnknownUnitException
57+
*/
58+
public function testUnknownUnit(): void
59+
{
60+
$this->expectException(UnknownUnitException::class);
61+
$this->expectExceptionMessage('The unit "bar" is not valid.');
62+
63+
new GeoFacet('foo', 0, 0, 0, 'bar');
64+
}
65+
66+
/**
67+
* @covers ::includeSpace
68+
*/
69+
public function testIncludeSpace(): void
70+
{
71+
assertFalse((new GeoFacet('foo', 0, 0, 0, 'm'))->includeSpace());
72+
}
73+
74+
/**
75+
* @covers ::priority
76+
*/
77+
public function testPriority(): void
78+
{
79+
assertSame(PartialQuery::PRIORITY_NORMAL, (new GeoFacet('foo', 0, 0, 0, 'm'))->priority());
80+
}
81+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 Tests\MacFJA\RediSearch\Search\QueryBuilder;
23+
24+
use MacFJA\RediSearch\Search\Exception\NotEnoughTermsException;
25+
use MacFJA\RediSearch\Search\QueryBuilder\PartialQuery;
26+
use MacFJA\RediSearch\Search\QueryBuilder\TagFacet;
27+
use function PHPUnit\Framework\assertFalse;
28+
use function PHPUnit\Framework\assertSame;
29+
use PHPUnit\Framework\TestCase;
30+
use Tests\MacFJA\RediSearch\support\Assertion;
31+
32+
/**
33+
* @coversDefaultClass \MacFJA\RediSearch\Search\QueryBuilder\TagFacet
34+
* @covers ::__construct
35+
*
36+
* @uses \MacFJA\RediSearch\Helper\EscapeHelper
37+
* @uses \MacFJA\RediSearch\Helper\DataHelper
38+
* @uses \MacFJA\RediSearch\Search\QueryBuilder\OrGroup
39+
* @uses \MacFJA\RediSearch\Search\QueryBuilder\Word
40+
*/
41+
class TagFacetTest extends TestCase
42+
{
43+
use Assertion;
44+
45+
/**
46+
* @covers ::render
47+
*/
48+
public function testNominal(): void
49+
{
50+
self::assertSameRender('@foobar:{hello}', new TagFacet('foobar', 'hello'));
51+
self::assertSameRender('@foobar:{hello|world}', new TagFacet('foobar', 'hello', 'world'));
52+
self::assertSameRender('@foobar:{hello world}', new TagFacet('foobar', 'hello world'));
53+
self::assertSameRender('@foobar:{hello\|world}', new TagFacet('foobar', 'hello|world'));
54+
}
55+
56+
/**
57+
* @covers \MacFJA\RediSearch\Search\Exception\NotEnoughTermsException
58+
*
59+
* @uses \MacFJA\RediSearch\Words\Exception\NotEnoughTermsException
60+
*/
61+
public function testNotEnoughValues(): void
62+
{
63+
$this->expectException(NotEnoughTermsException::class);
64+
$this->expectExceptionMessage('You must have at least one term');
65+
66+
new TagFacet('foobar');
67+
}
68+
69+
/**
70+
* @covers ::includeSpace
71+
*/
72+
public function testIncludeSpace(): void
73+
{
74+
assertFalse((new TagFacet('foo', 'bar'))->includeSpace());
75+
}
76+
77+
/**
78+
* @covers ::priority
79+
*/
80+
public function testPriority(): void
81+
{
82+
assertSame(PartialQuery::PRIORITY_NORMAL, (new TagFacet('foo', 'bar'))->priority());
83+
}
84+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 Tests\MacFJA\RediSearch\Search\QueryBuilder;
23+
24+
use MacFJA\RediSearch\Search\Exception\NotEnoughTermsException;
25+
use MacFJA\RediSearch\Search\QueryBuilder\PartialQuery;
26+
use MacFJA\RediSearch\Search\QueryBuilder\TextFacet;
27+
use function PHPUnit\Framework\assertFalse;
28+
use function PHPUnit\Framework\assertSame;
29+
use PHPUnit\Framework\TestCase;
30+
use Tests\MacFJA\RediSearch\support\Assertion;
31+
32+
/**
33+
* @coversDefaultClass \MacFJA\RediSearch\Search\QueryBuilder\TextFacet
34+
* @covers ::__construct
35+
*
36+
* @uses \MacFJA\RediSearch\Helper\EscapeHelper
37+
* @uses \MacFJA\RediSearch\Helper\DataHelper
38+
* @uses \MacFJA\RediSearch\Search\QueryBuilder\OrGroup
39+
* @uses \MacFJA\RediSearch\Search\QueryBuilder\Word
40+
*/
41+
class TextFacetTest extends TestCase
42+
{
43+
use Assertion;
44+
45+
/**
46+
* @covers ::render
47+
*/
48+
public function testNominal(): void
49+
{
50+
self::assertSameRender('@foobar:hello', new TextFacet('foobar', 'hello'));
51+
self::assertSameRender('@foobar:(hello|world)', new TextFacet('foobar', 'hello', 'world'));
52+
self::assertSameRender('@foobar:(hello world)', new TextFacet('foobar', 'hello world'));
53+
self::assertSameRender('@foobar:hello\|world', new TextFacet('foobar', 'hello|world'));
54+
}
55+
56+
/**
57+
* @covers \MacFJA\RediSearch\Search\Exception\NotEnoughTermsException
58+
*
59+
* @uses \MacFJA\RediSearch\Words\Exception\NotEnoughTermsException
60+
*/
61+
public function testNotEnoughValues(): void
62+
{
63+
$this->expectException(NotEnoughTermsException::class);
64+
$this->expectExceptionMessage('You must have at least one term');
65+
66+
new TextFacet('foobar');
67+
}
68+
69+
/**
70+
* @covers ::includeSpace
71+
*/
72+
public function testIncludeSpace(): void
73+
{
74+
assertFalse((new TextFacet('foo', 'bar'))->includeSpace());
75+
}
76+
77+
/**
78+
* @covers ::priority
79+
*/
80+
public function testPriority(): void
81+
{
82+
assertSame(PartialQuery::PRIORITY_NORMAL, (new TextFacet('foo', 'bar'))->priority());
83+
}
84+
}

tests/support/Assertion.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use function implode;
2525
use function is_subclass_of;
2626
use MacFJA\RediSearch\PartialQuery;
27+
use MacFJA\RediSearch\Search\QueryBuilder\PartialQuery as RenderablePartialQuery;
2728
use PHPUnit\Framework\TestCase;
2829
use RuntimeException;
2930

@@ -36,4 +37,12 @@ private static function assertSameQuery(string $expected, PartialQuery $partialQ
3637
}
3738
self::assertSame($expected, implode(' ', $partialQuery->getQueryParts()));
3839
}
40+
41+
private static function assertSameRender(string $expected, RenderablePartialQuery $partialQuery): void
42+
{
43+
if (!is_subclass_of(self::class, TestCase::class)) {
44+
throw new RuntimeException();
45+
}
46+
self::assertSame($expected, $partialQuery->render());
47+
}
3948
}

0 commit comments

Comments
 (0)