You are here

class LoggerChannelTest in Service Container 7

Same name and namespace in other branches
  1. 7.2 tests/src/Logger/LoggerChannelTest.php \Drupal\Tests\service_container\Logger\LoggerChannelTest

@coversDefaultClass \Drupal\service_container\Logger\LoggerChannel

Hierarchy

  • class \Drupal\Tests\service_container\Logger\LoggerChannelTest extends \Drupal\Tests\service_container\Logger\PHPUnit_Framework_TestCase

Expanded class hierarchy of LoggerChannelTest

File

tests/src/Logger/LoggerChannelTest.php, line 16
Contains \Drupal\Tests\service_container\Logger\LoggerChannelTest.

Namespace

Drupal\Tests\service_container\Logger
View source
class LoggerChannelTest extends \PHPUnit_Framework_TestCase {

  /**
   * The tested logger channel.
   *
   * @var \Drupal\service_container\Logger\LoggerChannel
   */
  protected $loggerChannel;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->loggerChannel = new LoggerChannel('test');
  }

  /**
   * @covers ::__construct()
   */
  public function test_construct() {
    $this
      ->assertInstanceOf('\\Drupal\\service_container\\Logger\\LoggerChannel', $this->loggerChannel);
    $this
      ->assertAttributeEquals('test', 'channel', $this->loggerChannel);
  }

  /**
   * @covers ::log()
   * @covers ::addLogger()
   *
   * @dataProvider providerLogLevels
   */
  public function test_log_singleLogger($log_method = NULL, $log_level = LogLevel::INFO) {
    $logger = \Mockery::mock('Psr\\Log\\LoggerInterface');
    $logger
      ->shouldReceive('log')
      ->with($log_level, 'test-message', array(
      'key' => 'value',
      'channel' => 'test',
    ));
    $this->loggerChannel
      ->addLogger($logger);
    if (is_null($log_method)) {
      $this->loggerChannel
        ->log($log_level, 'test-message', array(
        'key' => 'value',
      ));
    }
    else {
      $this->loggerChannel
        ->{$log_method}('test-message', array(
        'key' => 'value',
      ));
    }
  }

  /**
   * @covers ::log()
   * @covers ::setLoggers()
   */
  public function test_setLoggers() {
    $logger1 = \Mockery::mock('Psr\\Log\\LoggerInterface');
    $logger2 = \Mockery::mock('Psr\\Log\\LoggerInterface');

    // Note: We use globally()/ordered() in order to ensure that $logger2 is
    // called first.
    $logger2
      ->shouldReceive('log')
      ->globally()
      ->ordered()
      ->with(LogLevel::INFO, 'test-message', array(
      'key' => 'value',
      'channel' => 'test',
    ));
    $logger1
      ->shouldReceive('log')
      ->globally()
      ->ordered()
      ->with(LogLevel::INFO, 'test-message', array(
      'key' => 'value',
      'channel' => 'test',
    ));
    $this->loggerChannel
      ->setLoggers(array(
      0 => array(
        $logger1,
      ),
      10 => array(
        $logger2,
      ),
    ));
    $this->loggerChannel
      ->log(LogLevel::INFO, 'test-message', array(
      'key' => 'value',
    ));
  }

  /**
   * @covers ::log()
   * @covers ::sortLoggers()
   * @covers ::addLogger()
   *
   * @covers ::emergency()
   * @covers ::alert()
   * @covers ::critical()
   * @covers ::error()
   * @covers ::warning()
   * @covers ::notice()
   * @covers ::info()
   * @covers ::debug()
   *
   * @dataProvider providerLogLevels
   */
  public function test_sortLogger($log_method = NULL, $log_level = LogLevel::INFO) {
    $logger1 = \Mockery::mock('Psr\\Log\\LoggerInterface');
    $logger2 = \Mockery::mock('Psr\\Log\\LoggerInterface');

    // Note: We use globally()/ordered() in order to ensure that $logger2 is
    // called first.
    $logger2
      ->shouldReceive('log')
      ->globally()
      ->ordered()
      ->with($log_level, 'test-message', array(
      'key' => 'value',
      'channel' => 'test',
    ));
    $logger1
      ->shouldReceive('log')
      ->globally()
      ->ordered()
      ->with($log_level, 'test-message', array(
      'key' => 'value',
      'channel' => 'test',
    ));
    $this->loggerChannel
      ->addLogger($logger1, 0);
    $this->loggerChannel
      ->addLogger($logger2, 10);
    if (is_null($log_method)) {
      $this->loggerChannel
        ->log($log_level, 'test-message', array(
        'key' => 'value',
      ));
    }
    else {
      $this->loggerChannel
        ->{$log_method}('test-message', array(
        'key' => 'value',
      ));
    }
  }
  public function providerLogLevels() {
    $data = array();
    $data[] = array(
      'emergency',
      LogLevel::EMERGENCY,
    );
    $data[] = array(
      'alert',
      LogLevel::ALERT,
    );
    $data[] = array(
      'critical',
      LogLevel::CRITICAL,
    );
    $data[] = array(
      'error',
      LogLevel::ERROR,
    );
    $data[] = array(
      'warning',
      LogLevel::WARNING,
    );
    $data[] = array(
      'notice',
      LogLevel::NOTICE,
    );
    $data[] = array(
      'info',
      LogLevel::INFO,
    );
    $data[] = array(
      'debug',
      LogLevel::DEBUG,
    );
    return $data;
  }

  /**
   * @covers ::setRequestStack()
   */
  public function test_setRequestStack() {
    $request_stack = \Mockery::mock('alias:Symfony\\Component\\HttpFoundation\\RequestStack');
    $this->loggerChannel
      ->setRequestStack($request_stack);
    $this
      ->assertAttributeEquals($request_stack, 'requestStack', $this->loggerChannel);
  }

  /**
   * @covers ::setCurrentUser()
   */
  public function test_setCurrentUser() {
    $current_user = \Mockery::mock('alias:Drupal\\Core\\Session\\AccountInterface');
    $this->loggerChannel
      ->setCurrentUser($current_user);
    $this
      ->assertAttributeEquals($current_user, 'currentUser', $this->loggerChannel);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LoggerChannelTest::$loggerChannel protected property The tested logger channel.
LoggerChannelTest::providerLogLevels public function
LoggerChannelTest::setUp protected function
LoggerChannelTest::test_construct public function @covers ::__construct()
LoggerChannelTest::test_log_singleLogger public function @covers ::log() @covers ::addLogger()
LoggerChannelTest::test_setCurrentUser public function @covers ::setCurrentUser()
LoggerChannelTest::test_setLoggers public function @covers ::log() @covers ::setLoggers()
LoggerChannelTest::test_setRequestStack public function @covers ::setRequestStack()
LoggerChannelTest::test_sortLogger public function @covers ::log() @covers ::sortLoggers() @covers ::addLogger()