search_api_meilisearch-1.0.x-dev/tests/src/Unit/Parser/InOperatorParserTest.php

tests/src/Unit/Parser/InOperatorParserTest.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\InOperatorParser;
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 InOperatorParserTest extends UnitTestCase {

  /**
   * The plugin instance.
   *
   * @var \Drupal\search_api_meilisearch\Parser\InOperatorParser
   */
  protected InOperatorParser $parser;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->parser = new InOperatorParser();
  }

  /**
   * 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], 'IN'),
        TRUE,
      ],
      'non array value' => [
        new Condition('name', 1, 'IN'),
        FALSE,
      ],
      'empty array value' => [
        new Condition('name', [], 'IN'),
        FALSE,
      ],
      'invalid operator equal' => [new Condition('name', [1, 3]), FALSE],
      'invalid operator not in' => [
        new Condition('name', [1, 3], 'NOT IN'),
        FALSE,
      ],
    ];
  }

  /**
   * Provides data for the parse method test.
   */
  public static function parseDataProvider(): array {
    return [
      'single word values' => [
        new Condition('name', ['test', 'test1'], 'IN'),
        '(name = "test" OR name = "test1")',
      ],
      'multi word values' => [
        new Condition('name', ['test test', 'test1   test1'], 'IN'),
        '(name = "test test" OR name = "test1   test1")',
      ],
      'reserved word values' => [
        new Condition('name', ['and', 'or', 'to'], 'IN'),
        '(name = "and" OR name = "or" OR name = "to")',
      ],
      'reserved word values uppercase' => [
        new Condition('name', ['AND', 'OR', 'TO'], 'IN'),
        '(name = "AND" OR name = "OR" OR name = "TO")',
      ],
    ];
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc