You are here

class LoggerChannelPartTest in Purge 8.3

@coversDefaultClass \Drupal\purge\Logger\LoggerChannelPart

@group purge

Hierarchy

Expanded class hierarchy of LoggerChannelPartTest

File

tests/src/Unit/Logger/LoggerChannelPartTest.php, line 14

Namespace

Drupal\Tests\purge\Unit\Logger
View source
class LoggerChannelPartTest extends UnitTestCase {

  /**
   * The mocked logger channel.
   *
   * @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Log\LoggerInterface
   */
  protected $loggerChannelPurge;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->loggerChannelPurge = $this
      ->createMock('\\Psr\\Log\\LoggerInterface');
  }

  /**
   * Helper to all severity methods.
   */
  private function helperForSeverityMethods($id, array $grants, $output, $severity) : void {
    $occurrence = is_null($output) ? $this
      ->never() : $this
      ->once();
    $this->loggerChannelPurge
      ->expects($occurrence)
      ->method('log')
      ->with($this
      ->stringContains($severity), $this
      ->stringContains('@purge_channel_part: @replaceme'), $this
      ->callback(function ($subject) use ($id, $output) {
      return $subject['@purge_channel_part'] === $id && $subject['@replaceme'] === $output;
    }));
    $part = new LoggerChannelPart($this->loggerChannelPurge, $id, $grants);
    $part
      ->{$severity}('@replaceme', [
      '@replaceme' => $output,
    ]);
  }

  /**
   * @covers ::__construct
   */
  public function testInstance() : void {
    $part = new LoggerChannelPart($this->loggerChannelPurge, 'id', []);
    $this
      ->assertInstanceOf('\\Psr\\Log\\LoggerInterface', $part);
  }

  /**
   * @covers ::getGrants
   *
   * @dataProvider providerTestGetGrants()
   */
  public function testGetGrants(array $grants) : void {
    $part = new LoggerChannelPart($this->loggerChannelPurge, 'id', $grants);
    $this
      ->assertEquals(count($grants), count($part
      ->getGrants()));
    $this
      ->assertEquals($grants, $part
      ->getGrants());
    foreach ($part
      ->getGrants() as $k => $v) {
      $this
        ->assertTrue(is_int($k));
      $this
        ->assertTrue(is_int($v));
    }
  }

  /**
   * Provides test data for testGetGrants().
   */
  public function providerTestGetGrants() : array {
    return [
      [
        [],
      ],
      [
        [
          RfcLogLevel::EMERGENCY,
        ],
      ],
      [
        [
          RfcLogLevel::ALERT,
        ],
      ],
      [
        [
          RfcLogLevel::CRITICAL,
        ],
      ],
      [
        [
          RfcLogLevel::ERROR,
        ],
      ],
      [
        [
          RfcLogLevel::WARNING,
        ],
      ],
      [
        [
          RfcLogLevel::NOTICE,
        ],
      ],
      [
        [
          RfcLogLevel::INFO,
        ],
      ],
      [
        [
          RfcLogLevel::INFO,
          RfcLogLevel::DEBUG,
        ],
      ],
      [
        [
          RfcLogLevel::DEBUG,
        ],
      ],
    ];
  }

  /**
   * @covers ::emergency
   *
   * @dataProvider providerTestEmergency()
   */
  public function testEmergency($id, array $grants, $output) : void {
    $this
      ->helperForSeverityMethods($id, $grants, $output, 'emergency');
  }

  /**
   * Provides test data for testEmergency().
   */
  public function providerTestEmergency() : array {
    return [
      [
        'good',
        [
          RfcLogLevel::EMERGENCY,
        ],
        'bazinga!',
      ],
      [
        'bad',
        [
          -1,
        ],
        NULL,
      ],
    ];
  }

  /**
   * @covers ::alert
   *
   * @dataProvider providerTestAlert()
   */
  public function testAlert($id, array $grants, $output) : void {
    $this
      ->helperForSeverityMethods($id, $grants, $output, 'alert');
  }

  /**
   * Provides test data for testAlert().
   */
  public function providerTestAlert() : array {
    return [
      [
        'good',
        [
          RfcLogLevel::ALERT,
        ],
        'bazinga!',
      ],
      [
        'bad',
        [
          -1,
        ],
        NULL,
      ],
    ];
  }

  /**
   * @covers ::critical
   *
   * @dataProvider providerTestCritical()
   */
  public function testCritical($id, array $grants, $output) : void {
    $this
      ->helperForSeverityMethods($id, $grants, $output, 'critical');
  }

  /**
   * Provides test data for testCritical().
   */
  public function providerTestCritical() : array {
    return [
      [
        'good',
        [
          RfcLogLevel::CRITICAL,
        ],
        'bazinga!',
      ],
      [
        'bad',
        [
          -1,
        ],
        NULL,
      ],
    ];
  }

  /**
   * @covers ::error
   *
   * @dataProvider providerTestError()
   */
  public function testError($id, array $grants, $output) : void {
    $this
      ->helperForSeverityMethods($id, $grants, $output, 'error');
  }

  /**
   * Provides test data for testError().
   */
  public function providerTestError() : array {
    return [
      [
        'good',
        [
          RfcLogLevel::ERROR,
        ],
        'bazinga!',
      ],
      [
        'bad',
        [
          -1,
        ],
        NULL,
      ],
    ];
  }

  /**
   * @covers ::warning
   *
   * @dataProvider providerTestWarning()
   */
  public function testWarning($id, array $grants, $output) : void {
    $this
      ->helperForSeverityMethods($id, $grants, $output, 'warning');
  }

  /**
   * Provides test data for testWarning().
   */
  public function providerTestWarning() : array {
    return [
      [
        'good',
        [
          RfcLogLevel::WARNING,
        ],
        'bazinga!',
      ],
      [
        'bad',
        [
          -1,
        ],
        NULL,
      ],
    ];
  }

  /**
   * @covers ::notice
   *
   * @dataProvider providerTestNotice()
   */
  public function testNotice($id, array $grants, $output) : void {
    $this
      ->helperForSeverityMethods($id, $grants, $output, 'notice');
  }

  /**
   * Provides test data for testNotice().
   */
  public function providerTestNotice() : array {
    return [
      [
        'good',
        [
          RfcLogLevel::NOTICE,
        ],
        'bazinga!',
      ],
      [
        'bad',
        [
          -1,
        ],
        NULL,
      ],
    ];
  }

  /**
   * @covers ::info
   *
   * @dataProvider providerTestInfo()
   */
  public function testInfo($id, array $grants, $output) : void {
    $this
      ->helperForSeverityMethods($id, $grants, $output, 'info');
  }

  /**
   * Provides test data for testInfo().
   */
  public function providerTestInfo() : array {
    return [
      [
        'good',
        [
          RfcLogLevel::INFO,
        ],
        'bazinga!',
      ],
      [
        'bad',
        [
          -1,
        ],
        NULL,
      ],
    ];
  }

  /**
   * @covers ::debug
   *
   * @dataProvider providerTestDebug()
   */
  public function testDebug($id, array $grants, $output) : void {
    $this
      ->helperForSeverityMethods($id, $grants, $output, 'debug');
  }

  /**
   * Provides test data for testDebug().
   */
  public function providerTestDebug() : array {
    return [
      [
        'good',
        [
          RfcLogLevel::DEBUG,
        ],
        'bazinga!',
      ],
      [
        'bad',
        [
          -1,
        ],
        NULL,
      ],
    ];
  }

  /**
   * @covers ::log
   *
   * @dataProvider providerTestLog()
   */
  public function testLog($id, $level, $message, $output) : void {
    $this->loggerChannelPurge
      ->expects($this
      ->once())
      ->method('log')
      ->with($this
      ->stringContains($level), $this
      ->stringContains('@purge_channel_part: ' . $message), $this
      ->callback(function ($subject) use ($id, $output) {
      return $subject['@purge_channel_part'] === $id && $subject['@replaceme'] === $output;
    }));
    $part = new LoggerChannelPart($this->loggerChannelPurge, $id);
    $part
      ->log($level, $message, [
      '@replaceme' => $output,
    ]);
  }

  /**
   * Provides test data for testLog().
   */
  public function providerTestLog() : array {
    return [
      [
        'id1',
        'level1',
        'message @placeholder',
        [
          '@placeholder' => 'foo',
        ],
      ],
      [
        'id2',
        'level2',
        'message @placeholder',
        [
          '@placeholder' => 'bar',
        ],
      ],
      [
        'id3',
        'level3',
        'message @placeholder',
        [
          '@placeholder' => 'baz',
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LoggerChannelPartTest::$loggerChannelPurge protected property The mocked logger channel.
LoggerChannelPartTest::helperForSeverityMethods private function Helper to all severity methods.
LoggerChannelPartTest::providerTestAlert public function Provides test data for testAlert().
LoggerChannelPartTest::providerTestCritical public function Provides test data for testCritical().
LoggerChannelPartTest::providerTestDebug public function Provides test data for testDebug().
LoggerChannelPartTest::providerTestEmergency public function Provides test data for testEmergency().
LoggerChannelPartTest::providerTestError public function Provides test data for testError().
LoggerChannelPartTest::providerTestGetGrants public function Provides test data for testGetGrants().
LoggerChannelPartTest::providerTestInfo public function Provides test data for testInfo().
LoggerChannelPartTest::providerTestLog public function Provides test data for testLog().
LoggerChannelPartTest::providerTestNotice public function Provides test data for testNotice().
LoggerChannelPartTest::providerTestWarning public function Provides test data for testWarning().
LoggerChannelPartTest::setUp protected function Overrides UnitTestCase::setUp
LoggerChannelPartTest::testAlert public function @covers ::alert
LoggerChannelPartTest::testCritical public function @covers ::critical
LoggerChannelPartTest::testDebug public function @covers ::debug
LoggerChannelPartTest::testEmergency public function @covers ::emergency
LoggerChannelPartTest::testError public function @covers ::error
LoggerChannelPartTest::testGetGrants public function @covers ::getGrants
LoggerChannelPartTest::testInfo public function @covers ::info
LoggerChannelPartTest::testInstance public function @covers ::__construct
LoggerChannelPartTest::testLog public function @covers ::log
LoggerChannelPartTest::testNotice public function @covers ::notice
LoggerChannelPartTest::testWarning public function @covers ::warning
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.