Skip to content

Commit

Permalink
issue #4 - unit tests cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed Sep 6, 2022
1 parent b17d91a commit e70f636
Showing 1 changed file with 60 additions and 30 deletions.
90 changes: 60 additions & 30 deletions tests/BasicQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

namespace MakinaCorpus\Lucene\Tests;

use DateTime;
use MakinaCorpus\Lucene\AbstractQuery;
use MakinaCorpus\Lucene\Query;
use PHPUnit\Framework\TestCase;
use DateTime;

/**
* @coversDefaultClass \MakinaCorpus\Lucene\Query
* @coversDefaultClass \MakinaCorpus\Lucene\AbstractQuery
* @coversDefaultClass \MakinaCorpus\Lucene\Query
* @coversDefaultClass \MakinaCorpus\Lucene\AbstractQuery
*/
class BasicQueryTest extends TestCase
{
public function testSomeQuery()
{
$query = new Query();

$now = new DateTime("@1467205820");
// Avoid runtime timezone conversion given false negative.
$from = new DateTime("1983-03-22", new \DateTimeZone("UTC"));
$now = new DateTime("@1467205820", new \DateTimeZone("UTC"));

$query
->setOperator(Query::OP_AND)
Expand All @@ -28,47 +30,75 @@ public function testSomeQuery()
$query
->createDateRange('my_date_field')
->setInclusive()
->setRange('1983-03-22', $now);
->setRange($from, $now);

$query
->matchTerm('some_field', 'some value', null, 0.8)
->matchTerm('other_field', 'some_other_value', 1.3);

// @codingStandardsIgnoreStart
$this->assertSame(
self::assertSame(
'((foo OR bar) AND my_date_field:["1983\-03\-22T00\:00\:00\+0000" TO "2016\-06\-29T13\:10\:20\+0000"] AND some_field:"some value"~0.8 AND other_field:some_other_value^1.3)',
trim((string)$query)
);
// @codingStandardsIgnoreEnd
}

public function dataSpecialCharacterEscaping()
{
return [
[
'Use the "~" symbol to add boost to your fields!',
'Use the \"\~\" symbol to add boost to your fields\!',
],
[
'Some date formats are: \"(30/12/1993), {30-12-1993} & [30\\12\\1993]\"',
'Some date formats are\: \\\\\"\(30\/12\/1993\), \{30\-12\-1993\} \& \[30\\\12\\\1993\]\\\\\"',
],
[
'one + one = two',
'one \+ one \= two',
],
[
'if (something || (something_else && another_something))',
'if \(something \|\| \(something_else \&\& another_something\)\)',
],
[
'Here is a cat: ^*>.<*^!!',
'Here is a cat\: \^\*\>.\<\*\^\!\!',
],
[
'Couldn\'t you come up with better test Strings? ... NO, sorry!',
'Couldn\'t you come up with better test Strings\? ... NO, sorry\!',
],
];
}

/**
* @covers \MakinaCorpus\Lucene\AbstractQuery::escapeToken
* @dataProvider dataSpecialCharacterEscaping
*/
public function testSpecialCharacterEscaping()
public function testSpecialCharacterEscaping(string $raw, string $expected)
{
// @codingStandardsIgnoreStart
$test_strings = [
'Use the "~" symbol to add boost to your fields!' => 'Use the \"\~\" symbol to add boost to your fields\!',
'Some date formats are: \"(30/12/1993), {30-12-1993} & [30\\12\\1993]\"' => 'Some date formats are\: \\\\\"\(30\/12\/1993\), \{30\-12\-1993\} \& \[30\\\12\\\1993\]\\\\\"',
'one + one = two' => 'one \+ one \= two',
'if (something || (something_else && another_something))' => 'if \(something \|\| \(something_else \&\& another_something\)\)',
'Here is a cat: ^*>.<*^!!' => 'Here is a cat\: \^\*\>.\<\*\^\!\!',
'Couldn\'t you come up with better test Strings? ... NO, sorry!' => 'Couldn\'t you come up with better test Strings\? ... NO, sorry\!',
];
// @codingStandardsIgnoreEnd
self::assertSame('"' . $expected . '"', AbstractQuery::escapeToken($raw));
}

// Assert strings with characters that should be escaped.
foreach ($test_strings as $input => $expected) {
$this->assertEquals("\"$expected\"", AbstractQuery::escapeToken($input));
}
// Assert single word without characters that should be escaped.
$this->assertEquals('Word', AbstractQuery::escapeToken('Word'));
// Assert phrase without characters that should be escaped.
$this->assertEquals('"Random search phrase."', AbstractQuery::escapeToken('Random search phrase.'));
/** Assert single word without characters that should be escaped, using @param $force = true. */
$this->assertEquals('"Word"', AbstractQuery::escapeToken('Word', TRUE));
// Assert string without spaces, with characters that should be escaped.
$this->assertEquals('"\!\*Test.php, \*.php"', AbstractQuery::escapeToken('!*Test.php, *.php'));
public function testEscapeSingleWordNoQuotes()
{
self::assertSame('Word', AbstractQuery::escapeToken('Word'));
}

public function testEscapePhraseAddQuotes()
{
self::assertSame('"Random search phrase."', AbstractQuery::escapeToken('Random search phrase.'));
}

public function testEscapeSingleWordAddQuoteWithForceParam()
{
self::assertSame('"Word"', AbstractQuery::escapeToken('Word', true));
}

public function testEscapeWithSpecialCharsAddQuotes()
{
self::assertSame('"\!\*Test.php, \*.php"', AbstractQuery::escapeToken('!*Test.php, *.php'));
}
}

0 comments on commit e70f636

Please sign in to comment.