You are here

class ValidationResultTest in Automatic Updates 8.2

@coversDefaultClass \Drupal\automatic_updates\Validation\ValidationResult

@group automatic_updates

Hierarchy

Expanded class hierarchy of ValidationResultTest

File

tests/src/Unit/ValidationResultTest.php, line 15

Namespace

Drupal\Tests\automatic_updates\Unit
View source
class ValidationResultTest extends UnitTestCase {

  /**
   * @covers ::createWarning
   *
   * @dataProvider providerValidConstructorArguments
   */
  public function testCreateWarningResult(array $messages, ?string $summary) : void {
    $summary = $summary ? t($summary) : NULL;
    $result = ValidationResult::createWarning($messages, $summary);
    $this
      ->assertResultValid($result, $messages, $summary, SystemManager::REQUIREMENT_WARNING);
  }

  /**
   * @covers ::createError
   *
   * @dataProvider providerValidConstructorArguments
   */
  public function testCreateErrorResult(array $messages, ?string $summary) : void {
    $summary = $summary ? t($summary) : NULL;
    $result = ValidationResult::createError($messages, $summary);
    $this
      ->assertResultValid($result, $messages, $summary, SystemManager::REQUIREMENT_ERROR);
  }

  /**
   * @covers ::createWarning
   */
  public function testCreateWarningResultException() : void {
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('If more than one message is provided, a summary is required.');
    ValidationResult::createWarning([
      'Something is wrong',
      'Something else is also wrong',
    ], NULL);
  }

  /**
   * @covers ::createError
   */
  public function testCreateErrorResultException() : void {
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('If more than one message is provided, a summary is required.');
    ValidationResult::createError([
      'Something is wrong',
      'Something else is also wrong',
    ], NULL);
  }

  /**
   * Data provider for testCreateWarningResult().
   *
   * @return mixed[]
   *   Test cases for testCreateWarningResult().
   */
  public function providerValidConstructorArguments() : array {
    return [
      '1 message no summary' => [
        'messages' => [
          'Something is wrong',
        ],
        'summary' => NULL,
      ],
      '2 messages has summary' => [
        'messages' => [
          'Something is wrong',
          'Something else is also wrong',
        ],
        'summary' => 'This sums it up.',
      ],
    ];
  }

  /**
   * Asserts a check result is valid.
   *
   * @param \Drupal\automatic_updates\Validation\ValidationResult $result
   *   The validation result to check.
   * @param array $expected_messages
   *   The expected messages.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $summary
   *   The expected summary or NULL if not summary is expected.
   * @param int $severity
   *   The severity.
   */
  protected function assertResultValid(ValidationResult $result, array $expected_messages, ?TranslatableMarkup $summary, int $severity) : void {
    $this
      ->assertSame($expected_messages, $result
      ->getMessages());
    if ($summary === NULL) {
      $this
        ->assertNull($result
        ->getSummary());
    }
    else {
      $this
        ->assertSame($summary
        ->getUntranslatedString(), $result
        ->getSummary()
        ->getUntranslatedString());
    }
    $this
      ->assertSame($severity, $result
      ->getSeverity());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340
ValidationResultTest::assertResultValid protected function Asserts a check result is valid.
ValidationResultTest::providerValidConstructorArguments public function Data provider for testCreateWarningResult().
ValidationResultTest::testCreateErrorResult public function @covers ::createError
ValidationResultTest::testCreateErrorResultException public function @covers ::createError
ValidationResultTest::testCreateWarningResult public function @covers ::createWarning
ValidationResultTest::testCreateWarningResultException public function @covers ::createWarning