View source
<?php
namespace Drupal\Tests\automatic_updates\Unit;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\system\SystemManager;
use Drupal\Tests\UnitTestCase;
class ValidationResultTest extends UnitTestCase {
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);
}
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);
}
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);
}
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);
}
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.',
],
];
}
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());
}
}