You are here

class LogsHttpLoggerTest in Logs HTTP 8

Tests the Logs Http logger service.

@group logs_http @coversDefaultClass \Drupal\logs_http\Logger\LogsHttpLogger

Hierarchy

Expanded class hierarchy of LogsHttpLoggerTest

File

tests/src/Unit/LogsHttpLoggerTest.php, line 17

Namespace

Drupal\Tests\logs_http\Unit
View source
class LogsHttpLoggerTest extends UnitTestCase {

  /**
   * The config object.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $config;

  /**
   * The log message parser service.
   *
   * @var \Drupal\Core\Logger\LogMessageParserInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $logMessageParser;

  /**
   * The severity levels array.
   *
   * @var array
   */
  protected $severityLevels;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->config = $this
      ->prophesize(ConfigFactoryInterface::class);
    $this->logMessageParser = $this
      ->prophesize(LogMessageParserInterface::class);
    $this->severityLevels = RfcLogLevel::getLevels();
    $this->config
      ->get('logs_http.settings')
      ->willReturn($this->config
      ->reveal());
  }

  /**
   * Tests isEnabled method.
   *
   * @covers ::isEnabled
   * @dataProvider isEnabledProvider
   */
  public function testIsEnabled($enabled, $url, $expected) {
    $this->config
      ->get('enabled')
      ->willReturn($enabled);
    $this->config
      ->get('url')
      ->willReturn($url);
    $logger = new LogsHttpLogger($this->config
      ->reveal(), $this->logMessageParser
      ->reveal());
    $result = $logger
      ->isEnabled();
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Provides test data to test isEnabled.
   *
   * In order to isEnabled method to return TRUE, the 'enabled' configuration
   * must be TRUE and the url must not be empty.
   *
   * @return array
   *   Array with:
   *   - "enabled" boolean value.
   *   - "url" string value.
   *   - The expected result.
   */
  public function isEnabledProvider() {
    return [
      [
        FALSE,
        '',
        FALSE,
      ],
      [
        FALSE,
        'https://example.com',
        FALSE,
      ],
      [
        TRUE,
        '',
        FALSE,
      ],
      [
        TRUE,
        'https://example.com',
        TRUE,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LogsHttpLoggerTest::$config protected property The config object.
LogsHttpLoggerTest::$logMessageParser protected property The log message parser service.
LogsHttpLoggerTest::$severityLevels protected property The severity levels array.
LogsHttpLoggerTest::isEnabledProvider public function Provides test data to test isEnabled.
LogsHttpLoggerTest::setUp public function Overrides UnitTestCase::setUp
LogsHttpLoggerTest::testIsEnabled public function Tests isEnabled method.
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.