reviewer-1.2.x-dev/modules/reviewer_test_kit/tests/src/Kernel/ConfigTaskBaseTest.php

modules/reviewer_test_kit/tests/src/Kernel/ConfigTaskBaseTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\reviewer_test_kit\Kernel;

use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\reviewer\Reviewer\Action;
use Drupal\reviewer_test_kit\Plugin\reviewer\Task\ConfigTaskBase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

/**
 * Test that the config task base checks and fixes simple configuration.
 */
#[Group('reviewer')]
#[CoversClass(ConfigTaskBase::class)]
final class ConfigTaskBaseTest extends ReviewerTestKitKernelTestBase {

  protected ConfigFactoryInterface $configFactory;

  /**
   * {@inheritdoc}
   *
   * @throws \Exception
   */
  protected function setUp(): void {
    parent::setUp();

    $this->configFactory = $this->container->get('config.factory');
  }

  /**
   * Test the check and fix method.
   *
   * @throws \Exception
   * @throws \PHPUnit\Framework\ExpectationFailedException
   * @throws \PHPUnit\Framework\UnknownClassOrInterfaceException
   */
  public function testCheckAndFix(): void {
    $this
      ->getConfig()
      ->setData([
        'key_one' => 'correct_value_one',
        'key_two' => 'incorrect_value_two',
        'key_three' => [
          'correct_value_three_a',
          'incorrect_value_three_b',
        ],
      ])
      ->save();

    $result = $this->runOneTaskReviewAndGetResult(Action::Check, 'test_config_review');
    self::assertSame('Configuration for "reviewer_test_kit_test_config.settings" is incorrect: key_two, key_three.1.', $result->getMessage());

    $result = $this->runOneTaskReviewAndGetResult(Action::Fix, 'test_config_review');
    self::assertSame('Fixed all "reviewer_test_kit_test_config.settings" configuration.', $result->getMessage());

    self::assertSame(
      [
        'key_one' => 'correct_value_one',
        'key_two' => 'correct_value_two',
        'key_three' => [
          'correct_value_three_a',
          'correct_value_three_b',
        ],
      ],
      $this->getConfig()->getRawData(),
    );
  }

  /**
   * Test that the correct exceptions are thrown when properties are missing.
   *
   * @throws \Exception
   * @throws \PHPUnit\Framework\ExpectationFailedException
   * @throws \PHPUnit\Framework\UnknownClassOrInterfaceException
   */
  public function testExceptions(): void {
    $expected_exceptions = [
      'Property Drupal\reviewer_test_kit_test\Plugin\reviewer\Task\TestNoExpectedValuesTask::expectedValues cannot be empty.',
      'Property Drupal\reviewer_test_kit_test\Plugin\reviewer\Task\TestNoConfigNameTask::configName cannot be empty.',
    ];

    // Check that the check method throws exceptions.
    $review = $this->runOneTaskReview(Action::Check, 'test_config_task_exceptions_review');
    $results = $review->getResults()->getIndividualResults();
    // Check that there are errors.
    self::assertNotCount(0, $results);
    // Check that each exception is reported correctly.
    foreach (array_values($results) as $index => $result) {
      if (isset($expected_exceptions[$index])) {
        self::assertSame($expected_exceptions[$index], $result->getMessage());
      }
    }

    // Add the exception for config that does not exist which is thrown when
    // fixing only.
    $expected_exceptions[] = 'Task Drupal\reviewer_test_kit_test\Plugin\reviewer\Task\TestNewConfigTask cannot fix configuration "new" because it does not exist.';
    // Check that the fix method throws exceptions.
    $review = $this->runOneTaskReview(Action::Fix, 'test_config_task_exceptions_review');
    $results = $review->getResults()->getIndividualResults();
    // Check that there are errors.
    self::assertNotCount(0, $results);
    // Check that each exception is reported correctly.
    foreach (array_values($results) as $index => $result) {
      if (isset($expected_exceptions[$index])) {
        self::assertSame($expected_exceptions[$index], $result->getMessage());
      }
    }
  }

  /**
   * Get the test config.
   */
  private function getConfig(): Config {
    return $this->configFactory->getEditable('reviewer_test_kit_test_config.settings');
  }

}

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

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