You are here

class NewRelicLoggerTest in New Relic 8

Same name and namespace in other branches
  1. 2.x tests/src/Unit/Logger/NewRelicLoggerTest.php \Drupal\Tests\new_relic_rpm\Unit\Logger\NewRelicLoggerTest
  2. 2.0.x tests/src/Unit/Logger/NewRelicLoggerTest.php \Drupal\Tests\new_relic_rpm\Unit\Logger\NewRelicLoggerTest

@coversDefaultClass \Drupal\new_relic_rpm\Logger\NewRelicLogger @group new_relic_rpm

Hierarchy

Expanded class hierarchy of NewRelicLoggerTest

File

tests/src/Unit/Logger/NewRelicLoggerTest.php, line 16

Namespace

Drupal\Tests\new_relic_rpm\Unit\Logger
View source
class NewRelicLoggerTest extends UnitTestCase {

  /**
   * The default request context for tests.
   *
   * @var array
   */
  private static $defaultContext = [
    'channel' => 'mytype',
    'ip' => '127.0.0.1',
    'request_uri' => '/foo',
    'referer' => '/bar',
    'uid' => 1,
  ];

  /**
   * Get a preconfigured logger.
   *
   * @param \Drupal\new_relic_rpm\ExtensionAdapter\NewRelicAdapterInterface $adapter
   *   The adapter to use.
   * @param array $levels
   *   The log levels to report.
   *
   * @return \Drupal\new_relic_rpm\Logger\NewRelicLogger
   *   A new logger instance.
   */
  private function getLogger(NewRelicAdapterInterface $adapter, array $levels = []) {
    $parser = new LogMessageParser();
    $config = $this
      ->getConfigFactoryStub([
      'new_relic_rpm.settings' => [
        'watchdog_severities' => $levels,
      ],
    ]);
    return new NewRelicLogger($parser, $adapter, $config);
  }

  /**
   * Test that log levels requested are logged.
   */
  public function testLogsSelectedLevelMessages() {
    $adapter = $this
      ->prophesize(NewRelicAdapterInterface::class);
    $adapter
      ->logError(Argument::type('string'))
      ->shouldBeCalled();
    $logger = $this
      ->getLogger($adapter
      ->reveal(), [
      RfcLogLevel::CRITICAL,
    ]);
    $logger
      ->log(RfcLogLevel::CRITICAL, 'Test', self::$defaultContext);
  }

  /**
   * Test that log levels not requested are ignored.
   */
  public function testIgnoresUnselectedLevelMessages() {
    $adapter = $this
      ->prophesize(NewRelicAdapterInterface::class);
    $adapter
      ->logError()
      ->shouldNotBeCalled();
    $logger = $this
      ->getLogger($adapter
      ->reveal());
    $logger
      ->log(RfcLogLevel::CRITICAL, 'Test', self::$defaultContext);
  }

  /**
   * Data source for tests.
   */
  public function getMessageTests() {
    return [
      [
        'My Log Message |',
      ],
      [
        'Severity: (2) Critical |',
      ],
      [
        'Type: mytype |',
      ],
      [
        'Request URI: /foo |',
      ],
      [
        'Referrer URI: /bar |',
      ],
      [
        'User: 1',
      ],
      [
        'IP Address: 127.0.0.1',
      ],
    ];
  }

  /**
   * Test that we log a message.
   *
   * @dataProvider getMessageTests
   */
  public function testCreatesMessage($expectedPart) {
    $adapter = $this
      ->prophesize(NewRelicAdapterInterface::class);
    $adapter
      ->logError(Argument::containingString($expectedPart))
      ->shouldBeCalled();
    $logger = $this
      ->getLogger($adapter
      ->reveal(), [
      RfcLogLevel::CRITICAL,
    ]);
    $logger
      ->log(RfcLogLevel::CRITICAL, 'My Log Message', self::$defaultContext);
  }

  /**
   * Test that an unknown log level is handled.
   */
  public function testHandlesUnknownLevel() {
    $adapter = $this
      ->prophesize(NewRelicAdapterInterface::class);
    $adapter
      ->logError(Argument::containingString('Severity: (8) Unknown'))
      ->shouldBeCalled();
    $logger = $this
      ->getLogger($adapter
      ->reveal(), [
      8,
    ]);
    $logger
      ->log(8, 'My Log Message', self::$defaultContext);
  }

  /**
   * Test that exceptions are being passed to the adapter.
   */
  public function testExceptionPassing() {
    $exception = new \Exception('Some exception to be logged.');
    $adapter = $this
      ->prophesize(NewRelicAdapterInterface::class);
    $adapter
      ->logException($exception)
      ->shouldBeCalled();
    $logger = $this
      ->getLogger($adapter
      ->reveal(), [
      RfcLogLevel::ERROR,
    ]);
    $logger
      ->log(RfcLogLevel::ERROR, 'My Log Message', self::$defaultContext + [
      'exception' => $exception,
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NewRelicLoggerTest::$defaultContext private static property The default request context for tests.
NewRelicLoggerTest::getLogger private function Get a preconfigured logger.
NewRelicLoggerTest::getMessageTests public function Data source for tests.
NewRelicLoggerTest::testCreatesMessage public function Test that we log a message.
NewRelicLoggerTest::testExceptionPassing public function Test that exceptions are being passed to the adapter.
NewRelicLoggerTest::testHandlesUnknownLevel public function Test that an unknown log level is handled.
NewRelicLoggerTest::testIgnoresUnselectedLevelMessages public function Test that log levels not requested are ignored.
NewRelicLoggerTest::testLogsSelectedLevelMessages public function Test that log levels requested are logged.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340