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

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

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

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

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

  /**
   * 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', ''), FALSE],
      'scalar value' => [new Condition('name', 5), FALSE],
      'null value' => [new Condition('name', NULL), TRUE],
      'valid operator equal' => [new Condition('name', NULL), TRUE],
      'valid operator not equal' => [new Condition('name', NULL, '<>'), TRUE],
      'invalid operator greater than' => [
        new Condition('name', NULL, '>'),
        FALSE,
      ],
    ];
  }

  /**
   * Provides data for the parse method test.
   */
  public static function parseDataProvider(): array {
    return [
      'equal operator' => [new Condition('name', NULL), 'name NOT EXISTS'],
      'not equal operator' => [new Condition('name', NULL, '<>'), "name EXISTS"],
    ];
  }

}

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

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