class NewRelicLoggerTest in New Relic 8
Same name and namespace in other branches
- 2.x tests/src/Unit/Logger/NewRelicLoggerTest.php \Drupal\Tests\new_relic_rpm\Unit\Logger\NewRelicLoggerTest
- 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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait- class \Drupal\Tests\new_relic_rpm\Unit\Logger\NewRelicLoggerTest
 
Expanded class hierarchy of NewRelicLoggerTest
File
- tests/src/ Unit/ Logger/ NewRelicLoggerTest.php, line 16 
Namespace
Drupal\Tests\new_relic_rpm\Unit\LoggerView 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
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| NewRelicLoggerTest:: | private static | property | The default request context for tests. | |
| NewRelicLoggerTest:: | private | function | Get a preconfigured logger. | |
| NewRelicLoggerTest:: | public | function | Data source for tests. | |
| NewRelicLoggerTest:: | public | function | Test that we log a message. | |
| NewRelicLoggerTest:: | public | function | Test that exceptions are being passed to the adapter. | |
| NewRelicLoggerTest:: | public | function | Test that an unknown log level is handled. | |
| NewRelicLoggerTest:: | public | function | Test that log levels not requested are ignored. | |
| NewRelicLoggerTest:: | public | function | Test that log levels requested are logged. | |
| PhpunitCompatibilityTrait:: | public | function | Returns a mock object for the specified class using the available method. | |
| PhpunitCompatibilityTrait:: | public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
| UnitTestCase:: | protected | property | The random generator. | |
| UnitTestCase:: | protected | property | The app root. | 1 | 
| UnitTestCase:: | protected | function | Asserts if two arrays are equal by sorting them first. | |
| UnitTestCase:: | protected | function | Mocks a block with a block plugin. | 1 | 
| UnitTestCase:: | protected | function | Returns a stub class resolver. | |
| UnitTestCase:: | public | function | Returns a stub config factory that behaves according to the passed array. | |
| UnitTestCase:: | public | function | Returns a stub config storage that returns the supplied configuration. | |
| UnitTestCase:: | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase:: | protected | function | Gets the random generator for the utility methods. | |
| UnitTestCase:: | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase:: | public | function | Generates a unique random string containing letters and numbers. | |
| UnitTestCase:: | protected | function | 340 | 
