You are here

public function MaintenanceModeTest::testSubscriber in Nagios Monitoring 8

File

tests/src/Kernel/MaintenanceModeTest.php, line 37

Class

MaintenanceModeTest
Tests the functionality to monitor cron.

Namespace

Drupal\Tests\nagios\Kernel

Code

public function testSubscriber() {
  $subscriber = new MaintenanceModeSubscriber();
  $get_response_event = $this
    ->prophesize(GetResponseEvent::class);
  $request = $this
    ->prophesize(Request::class);

  /** @noinspection PhpUndefinedMethodInspection */
  $request
    ->getPathInfo()
    ->willReturn('/nagios');

  /** @noinspection PhpUndefinedMethodInspection */
  $get_response_event
    ->getRequest()
    ->willReturn($request
    ->reveal())
    ->shouldBeCalled();
  $content = '';
  $set_response_content = function ($args) use (&$content) {

    /** @var \Symfony\Component\HttpFoundation\Response $response */
    $response = $args[0];
    $content = $response
      ->getContent();
  };

  /** @noinspection PhpUndefinedMethodInspection */
  $get_response_event
    ->setResponse(Argument::any())
    ->will($set_response_content)
    ->shouldBeCalled();
  $config = \Drupal::configFactory()
    ->getEditable('nagios.settings');
  foreach ([
    'cron',
    'maintenance',
    'watchdog',
  ] as $check) {
    $config
      ->set('nagios.function.' . $check, FALSE);
  }
  $config
    ->set('nagios.statuspage.enabled', TRUE);
  $config
    ->save();
  $_SERVER['HTTP_USER_AGENT'] = $config
    ->get('nagios.ua');

  /** @noinspection PhpParamsInspection */
  $subscriber
    ->onKernelRequestMaintenance($get_response_event
    ->reveal());
  self::assertSame("\nnagios=OK,  | \n", $content);
}