You are here

class StateTest in CloudFlare 8

Tests functionality of CloudFlareState object.

@group cloudflare

@covers \Drupal\cloudflare\State

Hierarchy

Expanded class hierarchy of StateTest

File

tests/src/Unit/StateTest.php, line 20

Namespace

Drupal\Tests\cloudflare\Unit
View source
class StateTest extends UnitTestCase {

  /**
   * Tests tag count tracking functionality.
   */
  public function testTagPurgeDailyCountIncrements() {
    $timestamp_stub = $this
      ->getMockBuilder('Drupal\\cloudflare\\Timestamp')
      ->disableOriginalConstructor()
      ->getMock();

    // Configure the stub.
    $timestamp_stub
      ->method('now')
      ->will($this
      ->onConsecutiveCalls(new \DateTime('2010-02-01 00:00:00'), new \DateTime('2010-02-01 00:01:00'), new \DateTime('2010-02-01 00:02:00')));
    $drupal_state_service = new CoreState(new KeyValueMemoryFactory(), new MemoryBackend('test'), new NullLockBackend());
    $cloudflare_state = new CloudFlareState($drupal_state_service, $timestamp_stub);
    $initial_count = $cloudflare_state
      ->getTagDailyCount();
    $this
      ->assertEquals(0, $initial_count, 'Tested state with empty counts');
    $cloudflare_state
      ->incrementTagPurgeDailyCount();
    $count = $cloudflare_state
      ->getTagDailyCount();
    $this
      ->assertEquals(1, $count, 'Tested state with first increment of day');
    $cloudflare_state
      ->incrementTagPurgeDailyCount();
    $count = $cloudflare_state
      ->getTagDailyCount();
    $this
      ->assertEquals(2, $count, 'Tested state with first increment of day');
    $cloudflare_state
      ->incrementTagPurgeDailyCount();
    $count = $cloudflare_state
      ->getTagDailyCount();
    $this
      ->assertEquals(3, $count, 'Tested state with first increment of day');
  }

  /**
   * Tests tag count boundary functionality.
   */
  public function testTagPurgeBoundaryIncrements() {
    $timestamp_stub = $this
      ->getMockBuilder('Drupal\\cloudflare\\Timestamp')
      ->disableOriginalConstructor()
      ->getMock();

    // Configure the stub.
    $timestamp_stub
      ->method('now')
      ->will($this
      ->onConsecutiveCalls(new DateTime('2010-02-01 00:00:00'), new DateTime('2010-02-02 00:01:00'), new DateTime('2010-02-03 00:02:00')));
    $drupal_state_service = new CoreState(new KeyValueMemoryFactory(), new MemoryBackend('test'), new NullLockBackend());
    $cloudflare_state = new CloudFlareState($drupal_state_service, $timestamp_stub);
    $initial_count = $cloudflare_state
      ->getTagDailyCount();
    $this
      ->assertEquals(0, $initial_count, 'Tested state with empty counts');
    $cloudflare_state
      ->incrementTagPurgeDailyCount();
    $count = $cloudflare_state
      ->getTagDailyCount();
    $this
      ->assertEquals(1, $count, 'Tested state with first increment of day');
    $cloudflare_state
      ->incrementTagPurgeDailyCount();
    $count = $cloudflare_state
      ->getTagDailyCount();
    $this
      ->assertEquals(1, $count, 'Tested state with first increment of day');
    $cloudflare_state
      ->incrementTagPurgeDailyCount();
    $count = $cloudflare_state
      ->getTagDailyCount();
    $this
      ->assertEquals(1, $count, 'Tested state with first increment of day');
  }

  /**
   * Tests tag count tracking functionality.
   */
  public function testApiRateLimitCountIncrements() {
    $timestamp_stub = $this
      ->getMockBuilder('Drupal\\cloudflare\\Timestamp')
      ->disableOriginalConstructor()
      ->getMock();

    // Configure the stub.
    $timestamp_stub
      ->method('now')
      ->will($this
      ->onConsecutiveCalls(new DateTime('2010-02-01 00:00:00'), new DateTime('2010-02-01 00:01:00'), new DateTime('2010-02-01 00:02:00')));
    $drupal_state_service = new CoreState(new KeyValueMemoryFactory(), new MemoryBackend('test'), new NullLockBackend());
    $cloudflare_state = new CloudFlareState($drupal_state_service, $timestamp_stub);
    $initial_count = $cloudflare_state
      ->getTagDailyCount();
    $this
      ->assertEquals(0, $initial_count, 'Tested state with empty counts');
    $cloudflare_state
      ->incrementApiRateCount();
    $count = $cloudflare_state
      ->getApiRateCount();
    $this
      ->assertEquals(1, $count, 'Tested state with first increment of day');
    $cloudflare_state
      ->incrementApiRateCount();
    $count = $cloudflare_state
      ->getApiRateCount();
    $this
      ->assertEquals(2, $count, 'Tested state with first increment of day');
    $cloudflare_state
      ->incrementApiRateCount();
    $count = $cloudflare_state
      ->getApiRateCount();
    $this
      ->assertEquals(3, $count, 'Tested state with first increment of day');
  }

  /**
   * Tests tag count boundary functionality.
   */
  public function testApiRateLimitBoundaryIncrements() {
    $timestamp_stub = $this
      ->getMockBuilder('Drupal\\cloudflare\\Timestamp')
      ->disableOriginalConstructor()
      ->getMock();

    // Configure the stub.
    $timestamp_stub
      ->method('now')
      ->will($this
      ->onConsecutiveCalls(new DateTime('2010-02-01 00:00:00'), new DateTime('2010-02-02 00:01:00'), new DateTime('2010-02-03 00:02:00'), new DateTime('2010-02-03 00:10:00'), new DateTime('2010-02-03 00:15:00')));
    $drupal_state_service = new CoreState(new KeyValueMemoryFactory(), new MemoryBackend('test'), new NullLockBackend());
    $cloudflare_state = new CloudFlareState($drupal_state_service, $timestamp_stub);
    $initial_count = $cloudflare_state
      ->getApiRateCount();
    $this
      ->assertEquals(0, $initial_count, 'Tested state with empty counts');
    $cloudflare_state
      ->incrementApiRateCount();
    $count = $cloudflare_state
      ->getApiRateCount();
    $this
      ->assertEquals(1, $count, 'Tested state with first increment of day');
    $cloudflare_state
      ->incrementApiRateCount();
    $count = $cloudflare_state
      ->getApiRateCount();
    $this
      ->assertEquals(1, $count, 'Tested state with first increment of day');
    $cloudflare_state
      ->incrementApiRateCount();
    $count = $cloudflare_state
      ->getApiRateCount();
    $this
      ->assertEquals(1, $count, 'Tested state with first increment of day');
    $cloudflare_state
      ->incrementApiRateCount();
    $count = $cloudflare_state
      ->getApiRateCount();
    $this
      ->assertEquals(1, $count, 'Tested state with first increment of day');
    $cloudflare_state
      ->incrementApiRateCount();
    $count = $cloudflare_state
      ->getApiRateCount();
    $this
      ->assertEquals(1, $count, 'Tested state with first increment of day');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
StateTest::testApiRateLimitBoundaryIncrements public function Tests tag count boundary functionality.
StateTest::testApiRateLimitCountIncrements public function Tests tag count tracking functionality.
StateTest::testTagPurgeBoundaryIncrements public function Tests tag count boundary functionality.
StateTest::testTagPurgeDailyCountIncrements public function Tests tag count tracking functionality.
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.
UnitTestCase::setUp protected function 340