You are here

class ApiRateLimitCheckTest in CloudFlare 8

Tests that purge_requirements() passes on our diagnostic checks.

@group cloudflarepurger

@covers \Drupal\cloudflarepurger\Plugin\Purge\DiagnosticCheck\ApiRateLimitCheck

Hierarchy

Expanded class hierarchy of ApiRateLimitCheckTest

File

modules/cloudflarepurger/tests/src/Unit/ApiRateLimitCheckTest.php, line 16

Namespace

Drupal\Tests\cloudflarepurger\Unit
View source
class ApiRateLimitCheckTest extends DiagnosticCheckTestBase {

  /**
   * Tests that ApiRateLimitCheck Responds as expected with test purge rates.
   *
   * @param int $api_rate
   *   The currentAPI rate to test.
   * @param int $expected_severity
   *   The expected diagnostic severity.
   *
   * @dataProvider apiRateLimitProvider
   */
  public function testApiRateLimitCheck($api_rate, $expected_severity) {
    $this->drupalState
      ->set(State::API_RATE_COUNT, $api_rate);
    $this->drupalState
      ->set(State::API_RATE_COUNT_START, new \DateTime());
    $api_rate_limit_check = new ApiRateLimitCheck([], '23123', 'this is a definition', $this->cloudflareState, $this->composerDependencyStub);
    $actual_severity = $api_rate_limit_check
      ->run();
    $this
      ->assertEquals($expected_severity, $actual_severity);
  }

  /**
   * Data provider for validating ApiRateLimitCheck.
   *
   * @return array[]
   *   Returns per data set an array with:
   *     - count of daily tag purge requests
   *     - expected status returned by diagnostic check
   */
  public function apiRateLimitProvider() {
    return [
      [
        NULL,
        DiagnosticCheckInterface::SEVERITY_OK,
      ],
      [
        0,
        DiagnosticCheckInterface::SEVERITY_OK,
      ],
      [
        1,
        DiagnosticCheckInterface::SEVERITY_OK,
      ],
      [
        500,
        DiagnosticCheckInterface::SEVERITY_OK,
      ],
      [
        899,
        DiagnosticCheckInterface::SEVERITY_OK,
      ],
      [
        900,
        DiagnosticCheckInterface::SEVERITY_WARNING,
      ],
      [
        901,
        DiagnosticCheckInterface::SEVERITY_WARNING,
      ],
      [
        1199,
        DiagnosticCheckInterface::SEVERITY_WARNING,
      ],
      [
        1200,
        DiagnosticCheckInterface::SEVERITY_ERROR,
      ],
      [
        1220,
        DiagnosticCheckInterface::SEVERITY_ERROR,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ApiRateLimitCheckTest::apiRateLimitProvider public function Data provider for validating ApiRateLimitCheck.
ApiRateLimitCheckTest::testApiRateLimitCheck public function Tests that ApiRateLimitCheck Responds as expected with test purge rates.
DiagnosticCheckTestBase::$cloudflareState protected property Tracks rate limits associated with CloudFlare Api.
DiagnosticCheckTestBase::$composerDependencyStub protected property Provides check for composer dependencies.
DiagnosticCheckTestBase::$container protected property The dependency injection container.
DiagnosticCheckTestBase::$drupalState protected property Tracks Drupal states.
DiagnosticCheckTestBase::$timestampStub protected property Provides timestamps.
DiagnosticCheckTestBase::setUp public function Overrides UnitTestCase::setUp 1
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.