function MessengerTest::testAddNoDuplicates

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Messenger/MessengerTest.php \Drupal\KernelTests\Core\Messenger\MessengerTest::testAddNoDuplicates()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Messenger/MessengerTest.php \Drupal\KernelTests\Core\Messenger\MessengerTest::testAddNoDuplicates()
  3. 10 core/tests/Drupal/KernelTests/Core/Messenger/MessengerTest.php \Drupal\KernelTests\Core\Messenger\MessengerTest::testAddNoDuplicates()

Tests we don't add duplicates.

@covers ::all @covers ::addStatus @covers ::addWarning @covers ::addError @covers ::deleteByType @covers ::deleteAll

File

core/tests/Drupal/KernelTests/Core/Messenger/MessengerTest.php, line 68

Class

MessengerTest
@group Messenger @coversDefaultClass \Drupal\Core\Messenger\Messenger

Namespace

Drupal\KernelTests\Core\Messenger

Code

public function testAddNoDuplicates() : void {
    $this->messenger
        ->addStatus('Non Duplicated status message');
    $this->messenger
        ->addStatus('Non Duplicated status message');
    $this->assertCount(1, $this->messenger
        ->messagesByType(MessengerInterface::TYPE_STATUS));
    $this->messenger
        ->addWarning('Non Duplicated warning message');
    $this->messenger
        ->addWarning('Non Duplicated warning message');
    $this->assertCount(1, $this->messenger
        ->messagesByType(MessengerInterface::TYPE_WARNING));
    $this->messenger
        ->addError('Non Duplicated error message');
    $this->messenger
        ->addError('Non Duplicated error message');
    $messages = $this->messenger
        ->messagesByType(MessengerInterface::TYPE_ERROR);
    $this->assertCount(1, $messages);
    // Check getting all messages.
    $messages = $this->messenger
        ->all();
    $this->assertCount(3, $messages);
    $this->assertArrayHasKey(MessengerInterface::TYPE_STATUS, $messages);
    $this->assertArrayHasKey(MessengerInterface::TYPE_WARNING, $messages);
    $this->assertArrayHasKey(MessengerInterface::TYPE_ERROR, $messages);
    // Check deletion.
    $this->messenger
        ->deleteAll();
    $this->assertCount(0, $this->messenger
        ->messagesByType(MessengerInterface::TYPE_STATUS));
    $this->assertCount(0, $this->messenger
        ->messagesByType(MessengerInterface::TYPE_WARNING));
    $this->assertCount(0, $this->messenger
        ->messagesByType(MessengerInterface::TYPE_ERROR));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.