search_api_meilisearch-1.0.x-dev/tests/src/Unit/Parser/ScalarValueParserTest.php
tests/src/Unit/Parser/ScalarValueParserTest.php
<?php
namespace Drupal\Tests\search_api_meilisearch\Unit\Parser;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Query\Condition;
use Drupal\search_api\Query\ConditionInterface;
use Drupal\search_api_meilisearch\Parser\ScalarValueParser;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
/**
* Provides tests for the ScalarValueParser plugin.
*
* @group search_api_meilisearch
*/
#[Group('search_api_meilisearch')]
class ScalarValueParserTest extends UnitTestCase {
/**
* The plugin instance.
*
* @var \Drupal\search_api_meilisearch\Parser\ScalarValueParser
*/
protected ScalarValueParser $parser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->parser = new ScalarValueParser();
}
/**
* Tests the supports method of the plugin.
*
* @param \Drupal\search_api\Query\ConditionInterface $condition
* The condition to test.
* @param bool $expected
* The expected result.
*
* @dataProvider supportsDataProvider
*/
#[DataProvider('supportsDataProvider')]
public function testSupportsMethod(ConditionInterface $condition, bool $expected): void {
$result = $this->parser->supports($condition, new Index(['id' => 'test'], 'search_api_index'));
$this->assertEquals($expected, $result);
}
/**
* Tests the parse method of the plugin.
*
* @param \Drupal\search_api\Query\ConditionInterface $condition
* The condition to test.
* @param string $expected
* The expected result.
*
* @dataProvider parseDataProvider
*/
#[DataProvider('parseDataProvider')]
public function testParseMethod(ConditionInterface $condition, string $expected) {
$result = $this->parser->parse($condition, new Index(['id' => 'test'], 'search_api_index'));
$this->assertEquals($expected, $result);
}
/**
* Provides data for the supports method test.
*/
public static function supportsDataProvider(): array {
return [
'empty string' => [new Condition('name', ''), TRUE],
'scalar value' => [new Condition('name', 'test'), TRUE],
'non scalar value' => [new Condition('name', ['test']), FALSE],
'valid operator' => [new Condition('name', 0, '<'), TRUE],
'invalid operator IN' => [new Condition('name', 'test', 'IN'), FALSE],
'invalid operator NOT IN' => [
new Condition('name', 'test', 'NOT IN'),
FALSE,
],
'invalid operator BETWEEN' => [
new Condition('name', 'test', 'BETWEEN'),
FALSE,
],
'invalid operator NOT BETWEEN' => [
new Condition('name', 'test', 'NOT BETWEEN'),
FALSE,
],
];
}
/**
* Provides data for the parse method test.
*/
public static function parseDataProvider(): array {
return [
'string value' => [new Condition('name', 'test'), 'name = "test"'],
'multiword string value' => [
new Condition('name', 'test test test'),
'name = "test test test"',
],
'reserved word AND string value' => [
new Condition('name', 'and'),
'name = "and"',
],
'reserved word OR string value' => [
new Condition('name', 'or'),
'name = "or"',
],
'reserved word TO string value' => [
new Condition('name', 'to'),
'name = "to"',
],
'reserved word AND string value uppercase' => [
new Condition('name', 'AND'),
'name = "AND"',
],
'reserved word OR string value uppercase' => [
new Condition('name', 'OR'),
'name = "OR"',
],
'reserved word TO string value uppercase' => [
new Condition('name', 'TO'),
'name = "TO"',
],
'numeric value' => [new Condition('name', 1, '<'), 'name < 1'],
];
}
}
