search_api_meilisearch-1.0.x-dev/tests/src/Unit/Parser/NotInOperatorParserTest.php
tests/src/Unit/Parser/NotInOperatorParserTest.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\NotInOperatorParser;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
/**
* Provides tests for the InOperatorParser plugin.
*
* @group search_api_meilisearch
*/
#[Group('search_api_meilisearch')]
class NotInOperatorParserTest extends UnitTestCase {
/**
* The plugin instance.
*
* @var \Drupal\search_api_meilisearch\Parser\NotInOperatorParser
*/
protected NotInOperatorParser $parser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->parser = new NotInOperatorParser();
}
/**
* 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 [
'valid operator and value' => [
new Condition('name', ['test', 2], 'NOT IN'),
TRUE,
],
'non array value' => [
new Condition('name', 1, 'NOT IN'),
FALSE,
],
'empty array value' => [
new Condition('name', [], 'NOT IN'),
FALSE,
],
'invalid operator equal' => [new Condition('name', [1, 3]), FALSE],
'invalid operator not in' => [
new Condition('name', [1, 3], 'IN'),
FALSE,
],
];
}
/**
* Provides data for the parse method test.
*/
public static function parseDataProvider(): array {
return [
'single word values' => [
new Condition('name', ['test', 'test1'], 'NOT IN'),
'(name != "test" AND name != "test1")',
],
'multi word values' => [
new Condition('name', ['test test', 'test1 test1'], 'IN'),
'(name != "test test" AND name != "test1 test1")',
],
'reserved word values' => [
new Condition('name', ['and', 'or', 'to'], 'IN'),
'(name != "and" AND name != "or" AND name != "to")',
],
'reserved word values uppercase' => [
new Condition('name', ['AND', 'OR', 'TO'], 'IN'),
'(name != "AND" AND name != "OR" AND name != "TO")',
],
];
}
}
