You are here

class LegacyMessengerTest in Service Container 8

Same name and namespace in other branches
  1. 7.2 tests/src/Messenger/LegacyMessengerTest.php \Drupal\Tests\service_container\Messenger\LegacyMessengerTest
  2. 7 tests/src/Messenger/LegacyMessengerTest.php \Drupal\Tests\service_container\Messenger\LegacyMessengerTest

@coversDefaultClass \Drupal\service_container\Messenger\LegacyMessenger

Hierarchy

  • class \Drupal\Tests\service_container\Messenger\LegacyMessengerTest extends \Drupal\Tests\service_container\Messenger\PHPUnit_Framework_TestCase

Expanded class hierarchy of LegacyMessengerTest

File

tests/src/Messenger/LegacyMessengerTest.php, line 15
Contains \Drupal\Tests\service_container\Messenger\LegacyMessengerTest.

Namespace

Drupal\Tests\service_container\Messenger
View source
class LegacyMessengerTest extends \PHPUnit_Framework_TestCase {

  /**
   * The Drupal7 service.
   *
   * @var \Drupal\service_container\Legacy\Drupal7
   */
  protected $drupal7;

  /**
   * The Messenger service.
   *
   * @var \Drupal\service_container\Messenger\LegacyMessenger
   */
  protected $messenger_service;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->drupal7 = \Mockery::mock('\\Drupal\\service_container\\Legacy\\Drupal7');
    $this->messenger_service = new LegacyMessenger($this->drupal7);
  }

  /**
   * @covers ::__construct()
   */
  public function test_construct() {
    $this
      ->assertInstanceOf('\\Drupal\\service_container\\Messenger\\MessengerInterface', $this->messenger_service);
    $this
      ->assertInstanceOf('\\Drupal\\service_container\\Legacy\\Drupal7', $this->drupal7);
  }

  /**
   * @covers ::addMessage()
   * @dataProvider addMessageDataProvider
   */
  public function test_addMessage($type, $message, $repeat) {
    $this->drupal7
      ->shouldReceive('drupal_set_message')
      ->once()
      ->with($message, $type, $repeat);
    $this
      ->assertSame($this->messenger_service, $this->messenger_service
      ->addMessage($message, $type, $repeat));
  }

  /**
   * @covers ::getMessages()
   * @dataProvider getMessagesDataProvider
   */
  public function test_getMessages($type, $expected) {
    $this->drupal7
      ->shouldReceive('drupal_get_messages')
      ->once()
      ->with(NULL, FALSE)
      ->andReturn($expected);
    $this
      ->assertEquals($expected, $this->messenger_service
      ->getMessages());
  }

  /**
   * @covers ::getMessagesByType()
   * @dataProvider getMessagesDataProvider
   */
  public function test_getMessagesByType($type, $expected) {
    $this->drupal7
      ->shouldReceive('drupal_get_messages')
      ->once()
      ->with($type, FALSE)
      ->andReturn($expected);
    $result = isset($expected[$type]) ? $expected[$type] : array();
    $this
      ->assertEquals($result, $this->messenger_service
      ->getMessagesByType($type));
  }

  /**
   * Data Provider for addMessage().
   */
  public function addMessageDataProvider() {
    return array(
      array(
        'status',
        'Hello Status!',
        FALSE,
      ),
      array(
        'status',
        'Hello Status!',
        TRUE,
      ),
      array(
        'warning',
        'Hello World!',
        FALSE,
      ),
      array(
        'warning',
        'Hello World!',
        TRUE,
      ),
      array(
        'error',
        'Hello Error!',
        FALSE,
      ),
      array(
        'error',
        'Hello Error!',
        TRUE,
      ),
    );
  }

  /**
   * Data Provider for getMessages() and getMessagesByType().
   */
  public function getMessagesDataProvider() {
    return array(
      array(
        'status',
        array(),
      ),
      array(
        'status',
        array(
          'status' => array(
            'Hello Status!',
          ),
        ),
      ),
      array(
        'warning',
        array(
          'warning' => array(
            'Hello World!',
          ),
        ),
      ),
      array(
        'error',
        array(
          'error' => array(
            'Hello Error!',
          ),
        ),
      ),
    );
  }

  /**
   * @covers ::deleteMessages()
   */
  public function test_deleteMessages() {
    $this->drupal7
      ->shouldReceive('drupal_get_messages')
      ->once()
      ->with(NULL, TRUE);
    $this
      ->assertSame($this->messenger_service, $this->messenger_service
      ->deleteMessages());
  }

  /**
   * @covers ::deleteMessagesByType()
   */
  public function test_deleteMessagesByType() {
    $this->drupal7
      ->shouldReceive('drupal_get_messages')
      ->once()
      ->with('warning', TRUE);
    $this
      ->assertSame($this->messenger_service, $this->messenger_service
      ->deleteMessagesByType('warning'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LegacyMessengerTest::$drupal7 protected property The Drupal7 service.
LegacyMessengerTest::$messenger_service protected property The Messenger service.
LegacyMessengerTest::addMessageDataProvider public function Data Provider for addMessage().
LegacyMessengerTest::getMessagesDataProvider public function Data Provider for getMessages() and getMessagesByType().
LegacyMessengerTest::setUp protected function
LegacyMessengerTest::test_addMessage public function @covers ::addMessage() @dataProvider addMessageDataProvider
LegacyMessengerTest::test_construct public function @covers ::__construct()
LegacyMessengerTest::test_deleteMessages public function @covers ::deleteMessages()
LegacyMessengerTest::test_deleteMessagesByType public function @covers ::deleteMessagesByType()
LegacyMessengerTest::test_getMessages public function @covers ::getMessages() @dataProvider getMessagesDataProvider
LegacyMessengerTest::test_getMessagesByType public function @covers ::getMessagesByType() @dataProvider getMessagesDataProvider