class TestMessenger

Mock class to replace the messenger service in unit tests.

Hierarchy

Expanded class hierarchy of TestMessenger

1 file declares its use of TestMessenger
RulesIntegrationTestBase.php in tests/src/Unit/Integration/RulesIntegrationTestBase.php

File

tests/src/Unit/TestMessenger.php, line 10

Namespace

Drupal\Tests\rules\Unit
View source
class TestMessenger implements MessengerInterface {
    
    /**
     * Array of messages.
     *
     * @var array
     */
    protected $messages = NULL;
    
    /**
     * {@inheritdoc}
     */
    public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) {
        if (!empty($message)) {
            $this->messages[$type] = $this->messages[$type] ?? [];
            if ($repeat || !in_array($message, $this->messages[$type])) {
                $this->messages[$type][] = $message;
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function addStatus($message, $repeat = FALSE) {
        return $this->addMessage($message, static::TYPE_STATUS, $repeat);
    }
    
    /**
     * {@inheritdoc}
     */
    public function addError($message, $repeat = FALSE) {
        return $this->addMessage($message, static::TYPE_ERROR, $repeat);
    }
    
    /**
     * {@inheritdoc}
     */
    public function addWarning($message, $repeat = FALSE) {
        return $this->addMessage($message, static::TYPE_WARNING, $repeat);
    }
    
    /**
     * {@inheritdoc}
     */
    public function all() {
        return $this->messages;
    }
    
    /**
     * {@inheritdoc}
     */
    public function messagesByType($type) {
        if (!empty($type)) {
            return $this->messages[$type] ?? [];
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function deleteAll() {
        return $this->messages = NULL;
    }
    
    /**
     * {@inheritdoc}
     */
    public function deleteByType($type) {
        if (!empty($type) && isset($this->messages[$type])) {
            $this->messages[$type] = NULL;
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
MessengerInterface::TYPE_ERROR constant An error.
MessengerInterface::TYPE_STATUS constant A status message.
MessengerInterface::TYPE_WARNING constant A warning.
TestMessenger::$messages protected property Array of messages.
TestMessenger::addError public function Adds a new error message to the queue. Overrides MessengerInterface::addError
TestMessenger::addMessage public function Adds a new message to the queue. Overrides MessengerInterface::addMessage
TestMessenger::addStatus public function Adds a new status message to the queue. Overrides MessengerInterface::addStatus
TestMessenger::addWarning public function Adds a new warning message to the queue. Overrides MessengerInterface::addWarning
TestMessenger::all public function Gets all messages. Overrides MessengerInterface::all
TestMessenger::deleteAll public function Deletes all messages. Overrides MessengerInterface::deleteAll
TestMessenger::deleteByType public function Deletes all messages of a certain type. Overrides MessengerInterface::deleteByType
TestMessenger::messagesByType public function Gets all messages of a certain type. Overrides MessengerInterface::messagesByType