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

tests/src/Unit/Parser/BetweenOperatorParserTest.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\BetweenOperatorParser;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;

/**
 * Provides tests for the BetweenOperatorParser plugin.
 *
 * @group search_api_meilisearch
 */
#[Group('search_api_meilisearch')]
class BetweenOperatorParserTest extends UnitTestCase {

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

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

  /**
   * 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', [1, '2'], 'BETWEEN'),
        TRUE,
      ],
      'invalid array length value' => [
        new Condition('name', [1], 'BETWEEN'),
        FALSE,
      ],
      'non array value' => [
        new Condition('name', 1, 'BETWEEN'),
        FALSE,
      ],
      'non numeric array value' => [
        new Condition('name', ['test', 1], 'BETWEEN'),
        FALSE,
      ],
      'invalid operator equal' => [new Condition('name', [1, 3]), FALSE],
      'invalid operator not between' => [
        new Condition('name', [1, 3], 'NOT BETWEEN'),
        FALSE,
      ],
    ];
  }

  /**
   * Provides data for the parse method test.
   */
  public static function parseDataProvider(): array {
    return [
      'operator' => [new Condition('name', [1, 3], 'BETWEEN'), 'name 1 TO 3'],
    ];
  }

}

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

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