Skip to content

Commit 84cf02f

Browse files
committed
Add UT for Escaper
1 parent 9c48cc5 commit 84cf02f

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed

tests/Query/EscaperTest.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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;
23+
24+
use function count;
25+
use MacFJA\RediSearch\Query\Escaper;
26+
27+
/**
28+
* @covers \MacFJA\RediSearch\Query\Escaper
29+
*
30+
* @internal
31+
*/
32+
class EscaperTest extends \PHPUnit\Framework\TestCase
33+
{
34+
private const TEST_TO_FIXTURE = [
35+
'testEscapeFieldName' => 'field name',
36+
'testEscapeWord' => 'word',
37+
'testEscapeExactMatch' => 'exact match',
38+
'testEscapeFuzzy' => 'fuzzy',
39+
'testEscapeNegation' => 'negation',
40+
'testEscapeOptional' => 'optional',
41+
'testEscapeNothing' => 'input',
42+
];
43+
/**
44+
* @var array<array<string>>
45+
*/
46+
private $dataset = [];
47+
48+
/**
49+
* @dataProvider dataProvider
50+
*/
51+
public function testEscapeNothing(string $input, string $escaped): void
52+
{
53+
static::assertSame($escaped, Escaper::escapeNothing($input));
54+
}
55+
56+
/**
57+
* @dataProvider dataProvider
58+
*/
59+
public function testEscapeFieldName(string $input, string $escaped): void
60+
{
61+
static::assertSame($escaped, Escaper::escapeFieldName($input));
62+
}
63+
64+
/**
65+
* @dataProvider dataProvider
66+
*/
67+
public function testEscapeWord(string $input, string $escaped): void
68+
{
69+
static::assertSame($escaped, Escaper::escapeWord($input));
70+
}
71+
72+
/**
73+
* @dataProvider dataProvider
74+
*/
75+
public function testEscapeExactMatch(string $input, string $escaped): void
76+
{
77+
static::assertSame($escaped, Escaper::escapeExactMatch($input));
78+
}
79+
80+
/**
81+
* @dataProvider dataProvider
82+
*/
83+
public function testEscapeFuzzy(string $input, string $escaped): void
84+
{
85+
static::assertSame($escaped, Escaper::escapeFuzzy($input));
86+
}
87+
88+
/**
89+
* @dataProvider dataProvider
90+
*/
91+
public function testEscapeNegation(string $input, string $escaped): void
92+
{
93+
static::assertSame($escaped, Escaper::escapeNegation($input));
94+
}
95+
96+
/**
97+
* @dataProvider dataProvider
98+
*/
99+
public function testEscapeOptional(string $input, string $escaped): void
100+
{
101+
static::assertSame($escaped, Escaper::escapeOptional($input));
102+
}
103+
104+
/**
105+
* @return array<array<string>>
106+
*/
107+
public function dataProvider(string $testName): array
108+
{
109+
$this->buildDataset();
110+
111+
return array_map(static function ($row) use ($testName) {
112+
return [$row['input'], $row[self::TEST_TO_FIXTURE[$testName]]];
113+
}, $this->dataset);
114+
}
115+
116+
private function buildDataset(): void
117+
{
118+
if (count($this->dataset) > 0) {
119+
return;
120+
}
121+
122+
$datasetPath = __DIR__.'/../fixtures/Query/EscaperFixture.csv';
123+
124+
if (!file_exists($datasetPath)) {
125+
static::markTestSkipped('Unable to find fixture data');
126+
}
127+
$handle = fopen($datasetPath, 'rb');
128+
129+
if (false === $handle) {
130+
static::markTestSkipped('Unable to load fixture data');
131+
}
132+
133+
$header = fgetcsv($handle) ?: [];
134+
fgetcsv($handle); // Description
135+
while (($row = fgetcsv($handle, 0, ',', '"', '"')) !== false) {
136+
$this->dataset[] = array_combine($header, $row ?: []) ?: [];
137+
}
138+
fclose($handle);
139+
140+
if (0 === count($this->dataset)) {
141+
static::markTestSkipped('Unable to load fixture data');
142+
}
143+
}
144+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"input","field name","fuzzy","exact match","optional","negation","word"
2+
"","'@' + tranformed + ':'","'%' + tranformed + '%'","'""' + tranformed + '""'","'~' + transformed","'-' + transformed","transformed"
3+
"foobar","foobar","foobar","foobar","foobar","foobar","foobar"
4+
"""foobar","\""foobar","\""foobar","\""foobar","\""foobar","\""foobar","\""foobar"
5+
"foobar""","foobar\""","foobar\""","foobar\""","foobar\""","foobar\""","foobar\"""
6+
"""foobar""","\""foobar\""","\""foobar\""","\""foobar\""","\""foobar\""","\""foobar\""","\""foobar\"""
7+
"@foobar","\@foobar","\@foobar","\@foobar","\@foobar","\@foobar","\@foobar"
8+
"foobar:","foobar\:","foobar\:","foobar\:","foobar\:","foobar\:","foobar\:"
9+
"@foobar:","\@foobar\:","\@foobar\:","\@foobar\:","\@foobar\:","\@foobar\:","\@foobar\:"
10+
"-foobar","\-foobar","\-foobar","\-foobar","\-foobar","\-foobar","\-foobar"
11+
"foo@bar","foo\@bar","foo\@bar","foo\@bar","foo\@bar","foo\@bar","foo\@bar"
12+
"foo-bar","foo\-bar","foo\-bar","foo\-bar","foo\-bar","foo\-bar","foo\-bar"
13+
"foo.bar","foo\.bar","foo\.bar","foo\.bar","foo\.bar","foo\.bar","foo\.bar"
14+
"foobar-","foobar\-","foobar\-","foobar\-","foobar\-","foobar\-","foobar\-"
15+
"foo,bar","foo\,bar","foo\,bar","foo\,bar","foo\,bar","foo\,bar","foo\,bar"
16+
"\@foobar","\@foobar","\@foobar","\@foobar","\@foobar","\@foobar","\@foobar"
17+
"foo""bar","foo\""bar","foo\""bar","foo\""bar","foo\""bar","foo\""bar","foo\""bar"
18+
"%foobar","\%foobar","\%foobar","\%foobar","\%foobar","\%foobar","\%foobar"
19+
"foo%bar","foo\%bar","foo\%bar","foo\%bar","foo\%bar","foo\%bar","foo\%bar"
20+
"%foobar%","\%foobar\%","\%foobar\%","\%foobar\%","\%foobar\%","\%foobar\%","\%foobar\%"
21+
"\%foobar","\%foobar","\%foobar","\%foobar","\%foobar","\%foobar","\%foobar"
22+
"\\@foobar","\\\@foobar","\\\@foobar","\\\@foobar","\\\@foobar","\\\@foobar","\\\@foobar"
23+
"foo=bar","foo\=bar","foo\=bar","foo\=bar","foo\=bar","foo\=bar","foo\=bar"
24+
"~foobar","\~foobar","\~foobar","\~foobar","\~foobar","\~foobar","\~foobar"
25+
"foobar*","foobar\*","foobar\*","foobar\*","foobar\*","foobar\*","foobar\*"
26+
"foo|bar","foo\|bar","foo\|bar","foo\|bar","foo\|bar","foo\|bar","foo\|bar"
27+
"foo(bar","foo\(bar","foo\(bar","foo\(bar","foo\(bar","foo\(bar","foo\(bar"
28+
"foo{bar","foo\{bar","foo\{bar","foo\{bar","foo\{bar","foo\{bar","foo\{bar"
29+
"foo[bar","foo\[bar","foo\[bar","foo\[bar","foo\[bar","foo\[bar","foo\[bar"
30+
"foo$bar","foo\$bar","foo\$bar","foo\$bar","foo\$bar","foo\$bar","foo\$bar"
31+
"-29","\-29","-29","-29","-29","\-29","-29"
32+
"29","29","29","29","29","\29","29"

0 commit comments

Comments
 (0)