Skip to content

Commit 6e6157a

Browse files
committed
add phpunit, & tests for NumericFacet, fixed bug
1 parent f90ad87 commit 6e6157a

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/composer.lock
44

55
/.php_cs.cache
6+
/.phpunit.cache/

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: analyze fix-code
1+
.PHONY: analyze fix-code test
22

33
analyze: | vendor
44
$(COMPOSER) install --optimize-autoloader --no-suggest --prefer-dist
@@ -19,6 +19,9 @@ fix-code: | vendor
1919
$(COMPOSER) exec -v php-cs-fixer -- fix
2020
@#$(COMPOSER) exec -v psalm -- --alter --issues=all src
2121

22+
test: | vendor
23+
$(COMPOSER) exec -v phpunit tests
24+
2225
vendor:
2326
ifneq (prod,${BUILD_MODE})
2427
$(COMPOSER) install --optimize-autoloader

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,19 @@
2929
"rskuipers/php-assumptions": "^0.8.0",
3030
"sebastian/phpcpd": "^6.0",
3131
"sensiolabs/security-checker": "^6.0",
32-
"vimeo/psalm": "^4.2"
32+
"vimeo/psalm": "^4.2",
33+
"phpunit/phpunit": "^9.5"
3334
},
3435
"autoload": {
3536
"psr-4": {
3637
"MacFJA\\RedisSearch\\": "src/"
3738
}
3839
},
40+
"autoload-dev": {
41+
"psr-4": {
42+
"Tests\\MacFJA\\RedisSearch\\": "tests/"
43+
}
44+
},
3945
"support": {
4046
"issues": "https://github.com/MacFJA/php-redisearch/issues",
4147
"source": "https://github.com/MacFJA/php-redisearch"

src/Search/QueryBuilder/NumericFacet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function render(): string
8787
$min = ($this->isMinInclusive ? '' : '(').$this->min;
8888
}
8989
if (is_numeric($this->max)) {
90-
$min = ($this->isMaxInclusive ? '' : '(').$this->max;
90+
$max = ($this->isMaxInclusive ? '' : '(').$this->max;
9191
}
9292

9393
return sprintf('@%s:[%s %s]', $this->fieldName, $min, $max);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Tests\MacFJA\RedisSearch\Search\QueryBuilder;
4+
5+
use MacFJA\RedisSearch\Search\QueryBuilder\NumericFacet;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* @coversDefaultClass \MacFJA\RedisSearch\Search\QueryBuilder\NumericFacet
10+
*
11+
* @see: https://oss.redislabs.com/redisearch/Query_Syntax/#mapping_common_sql_predicates_to_redisearch
12+
* WHERE num BETWEEN 10 AND 20 @num:[10 20]
13+
* WHERE num >= 10 @num:[10 +inf]
14+
* WHERE num > 10 @num:[(10 +inf]
15+
* WHERE num < 10 @num:[-inf (10]
16+
* WHERE num <= 10 @num:[-inf 10]
17+
* WHERE num < 10 OR num > 20 @num:[-inf (10] | @num:[(20 +inf]
18+
*/
19+
class NumericFacetTest extends TestCase
20+
{
21+
/**
22+
* @covers ::greaterThan
23+
*/
24+
public function testGreaterThan()
25+
{
26+
$facet = NumericFacet::greaterThan('num', 10);
27+
$this->assertSame('@num:[(10 +inf]', $facet->render());
28+
}
29+
30+
/**
31+
* @covers ::greaterThanOrEquals
32+
*/
33+
public function testGreaterThanOrEquals()
34+
{
35+
$facet = NumericFacet::greaterThanOrEquals('num', 10);
36+
$this->assertSame('@num:[10 +inf]', $facet->render());
37+
}
38+
39+
/**
40+
* @covers ::lessThan
41+
*/
42+
public function testLessThan()
43+
{
44+
$facet = NumericFacet::lessThan('num', 10);
45+
$this->assertSame('@num:[-inf (10]', $facet->render());
46+
}
47+
48+
/**
49+
* @covers ::lessThanOrEquals
50+
*/
51+
public function testLessThanOrEquals()
52+
{
53+
$facet = NumericFacet::lessThanOrEquals('num', 10);
54+
$this->assertSame('@num:[-inf 10]', $facet->render());
55+
}
56+
57+
/**
58+
* @covers ::equalsTo
59+
*/
60+
public function testEqualsTo()
61+
{
62+
$facet = NumericFacet::equalsTo('num', 10);
63+
$this->assertSame('@num:[10 10]', $facet->render());
64+
}
65+
}

0 commit comments

Comments
 (0)