You are here

class CounterTest in Purge 8.3

@coversDefaultClass \Drupal\purge\Counter\Counter

@group purge

Hierarchy

Expanded class hierarchy of CounterTest

File

tests/src/Unit/Counter/CounterTest.php, line 14

Namespace

Drupal\Tests\purge\Unit\Counter
View source
class CounterTest extends UnitTestCase {

  /**
   * @covers ::disableDecrement
   */
  public function testDisableDecrement() : void {
    $counter = new Counter();
    $counter
      ->disableDecrement();
    $this
      ->expectException(\LogicException::class);
    $this
      ->expectExceptionMessage('No ::decrement() permission on this object.');
    $counter
      ->decrement();
  }

  /**
   * @covers ::disableIncrement
   */
  public function testDisableIncrement() : void {
    $counter = new Counter();
    $counter
      ->disableIncrement();
    $this
      ->expectException(\LogicException::class);
    $this
      ->expectExceptionMessage('No ::increment() permission on this object.');
    $counter
      ->increment();
  }

  /**
   * @covers ::disableSet
   */
  public function testDisableSet() : void {
    $counter = new Counter();
    $counter
      ->disableSet();
    $this
      ->expectException(\LogicException::class);
    $this
      ->expectExceptionMessage('No ::set() permission on this object.');
    $counter
      ->set(5);
  }

  /**
   * @covers ::get
   *
   * @dataProvider providerTestGet()
   */
  public function testGet($value) : void {
    $counter = new Counter($value);
    $this
      ->assertEquals($value, $counter
      ->get());
    $this
      ->assertTrue(is_float($counter
      ->get()));
    $this
      ->assertFalse(is_int($counter
      ->get()));
  }

  /**
   * Provides test data for testGet().
   */
  public function providerTestGet() : array {
    return [
      [
        0,
      ],
      [
        5,
      ],
      [
        1.3,
      ],
      [
        8.9,
      ],
    ];
  }

  /**
   * @covers ::getInteger
   *
   * @dataProvider providerTestGetInteger()
   */
  public function testGetInteger($value) : void {
    $counter = new Counter($value);
    $this
      ->assertEquals((int) $value, $counter
      ->getInteger());
    $this
      ->assertFalse(is_float($counter
      ->getInteger()));
    $this
      ->assertTrue(is_int($counter
      ->getInteger()));
  }

  /**
   * Provides test data for testGetInteger().
   */
  public function providerTestGetInteger() : array {
    return [
      [
        0,
      ],
      [
        5,
      ],
      [
        1.3,
      ],
      [
        8.9,
      ],
    ];
  }

  /**
   * @covers ::disableSet
   * @dataProvider providerTestSetNotFloatOrInt()
   */
  public function testSetNotFloatOrInt($value) : void {
    $counter = new Counter();
    $this
      ->expectException(BadBehaviorException::class);
    $this
      ->expectExceptionMessage('Given $value is not a integer or float.');
    $counter
      ->set($value);
  }

  /**
   * Provides test data for testSetNotFloatOrInt().
   */
  public function providerTestSetNotFloatOrInt() : array {
    return [
      [
        FALSE,
      ],
      [
        "0",
      ],
      [
        NULL,
      ],
    ];
  }

  /**
   * @covers ::disableSet
   */
  public function testSetNegative() : void {
    $counter = new Counter();
    $this
      ->expectException(BadBehaviorException::class);
    $this
      ->expectExceptionMessage('Given $value can only be zero or positive.');
    $counter
      ->set(-1.0E-6);
  }

  /**
   * @covers ::set
   *
   * @dataProvider providerTestSet()
   */
  public function testSet($value) : void {
    $counter = new Counter();
    $counter
      ->set($value);
    $this
      ->assertEquals($value, $counter
      ->get());
  }

  /**
   * Provides test data for testSet().
   */
  public function providerTestSet() : array {
    return [
      [
        0,
      ],
      [
        5,
      ],
      [
        1.3,
      ],
      [
        8.9,
      ],
    ];
  }

  /**
   * @covers ::decrement
   *
   * @dataProvider providerTestDecrement()
   */
  public function testDecrement($start, $subtract, $result) : void {
    $counter = new Counter($start);
    $counter
      ->decrement($subtract);
    $this
      ->assertEquals($result, $counter
      ->get());
  }

  /**
   * Provides test data for testDecrement().
   */
  public function providerTestDecrement() : array {
    return [
      [
        4.0,
        0.2,
        3.8,
      ],
      [
        2,
        1,
        1,
      ],
      [
        1,
        1,
        0,
      ],
    ];
  }

  /**
   * @covers ::decrement
   * @dataProvider providerTestDecrementInvalidValue()
   */
  public function testDecrementInvalidValue($value) : void {
    $counter = new Counter(10);
    $this
      ->expectException(BadBehaviorException::class);
    $this
      ->expectExceptionMessage('Given $amount is zero or negative.');
    $counter
      ->decrement($value);
  }

  /**
   * Provides test data for testDecrementInvalidValue().
   */
  public function providerTestDecrementInvalidValue() : array {
    return [
      [
        0,
      ],
      [
        0.0,
      ],
      [
        -1,
      ],
    ];
  }

  /**
   * @covers ::decrement
   * @dataProvider providerTestDecrementNotFloatOrInt()
   */
  public function testDecrementNotFloatOrInt($value) : void {
    $counter = new Counter(10);
    $this
      ->expectException(BadBehaviorException::class);
    $this
      ->expectExceptionMessage('Given $amount is not a integer or float.');
    $counter
      ->decrement($value);
  }

  /**
   * Provides test data for testDecrementNotFloatOrInt().
   */
  public function providerTestDecrementNotFloatOrInt() : array {
    return [
      [
        FALSE,
      ],
      [
        "0",
      ],
      [
        NULL,
      ],
    ];
  }

  /**
   * @covers ::increment
   *
   * @dataProvider providerTestIncrement()
   */
  public function testIncrement($start, $add, $result) : void {
    $counter = new Counter($start);
    $counter
      ->increment($add);
    $this
      ->assertEquals($result, $counter
      ->get());
  }

  /**
   * Provides test data for testIncrement().
   */
  public function providerTestIncrement() : array {
    return [
      [
        4.0,
        0.2,
        4.2,
      ],
      [
        0.1,
        1,
        1.1,
      ],
      [
        2,
        1,
        3,
      ],
    ];
  }

  /**
   * @covers ::increment
   * @dataProvider providerTestIncrementInvalidValue()
   */
  public function testIncrementInvalidValue($value) : void {
    $counter = new Counter(10);
    $this
      ->expectException(BadBehaviorException::class);
    $this
      ->expectExceptionMessage('Given $amount is zero or negative.');
    $counter
      ->increment($value);
  }

  /**
   * Provides test data for testIncrementInvalidValue().
   */
  public function providerTestIncrementInvalidValue() : array {
    return [
      [
        0,
      ],
      [
        0.0,
      ],
      [
        -1,
      ],
    ];
  }

  /**
   * @covers ::increment
   * @dataProvider providerTestIncrementNotFloatOrInt()
   */
  public function testIncrementNotFloatOrInt($value) : void {
    $counter = new Counter(10);
    $this
      ->expectException(BadBehaviorException::class);
    $this
      ->expectExceptionMessage('Given $amount is not a integer or float.');
    $counter
      ->increment($value);
  }

  /**
   * Provides test data for testIncrementNotFloatOrInt().
   */
  public function providerTestIncrementNotFloatOrInt() : array {
    return [
      [
        FALSE,
      ],
      [
        "0",
      ],
      [
        NULL,
      ],
    ];
  }

  /**
   * @covers ::setWriteCallback
   *
   * @dataProvider providerTestSetWriteCallback()
   */
  public function testSetWriteCallback($value_start, $call, $value_end) : void {
    $counter = new Counter($value_start);

    // Pass a callback that modifies the local $passed_value.
    $passed_value = NULL;
    $callback = function ($_value) use (&$passed_value) {
      $passed_value = $_value;
    };
    $counter
      ->setWriteCallback($callback);

    // Call the requested callback and verify that the results match.
    $method = array_shift($call);
    call_user_func_array([
      $counter,
      $method,
    ], $call);
    $this
      ->assertEquals($passed_value, $value_end);
  }

  /**
   * Provides test data for testSetWriteCallback().
   */
  public function providerTestSetWriteCallback() : array {
    return [
      [
        0,
        [
          'set',
          5,
        ],
        5,
      ],
      [
        1.8,
        [
          'increment',
          2.3,
        ],
        4.1,
      ],
      [
        1.6,
        [
          'decrement',
          0.3,
        ],
        1.3,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CounterTest::providerTestDecrement public function Provides test data for testDecrement().
CounterTest::providerTestDecrementInvalidValue public function Provides test data for testDecrementInvalidValue().
CounterTest::providerTestDecrementNotFloatOrInt public function Provides test data for testDecrementNotFloatOrInt().
CounterTest::providerTestGet public function Provides test data for testGet().
CounterTest::providerTestGetInteger public function Provides test data for testGetInteger().
CounterTest::providerTestIncrement public function Provides test data for testIncrement().
CounterTest::providerTestIncrementInvalidValue public function Provides test data for testIncrementInvalidValue().
CounterTest::providerTestIncrementNotFloatOrInt public function Provides test data for testIncrementNotFloatOrInt().
CounterTest::providerTestSet public function Provides test data for testSet().
CounterTest::providerTestSetNotFloatOrInt public function Provides test data for testSetNotFloatOrInt().
CounterTest::providerTestSetWriteCallback public function Provides test data for testSetWriteCallback().
CounterTest::testDecrement public function @covers ::decrement
CounterTest::testDecrementInvalidValue public function @covers ::decrement @dataProvider providerTestDecrementInvalidValue()
CounterTest::testDecrementNotFloatOrInt public function @covers ::decrement @dataProvider providerTestDecrementNotFloatOrInt()
CounterTest::testDisableDecrement public function @covers ::disableDecrement
CounterTest::testDisableIncrement public function @covers ::disableIncrement
CounterTest::testDisableSet public function @covers ::disableSet
CounterTest::testGet public function @covers ::get
CounterTest::testGetInteger public function @covers ::getInteger
CounterTest::testIncrement public function @covers ::increment
CounterTest::testIncrementInvalidValue public function @covers ::increment @dataProvider providerTestIncrementInvalidValue()
CounterTest::testIncrementNotFloatOrInt public function @covers ::increment @dataProvider providerTestIncrementNotFloatOrInt()
CounterTest::testSet public function @covers ::set
CounterTest::testSetNegative public function @covers ::disableSet
CounterTest::testSetNotFloatOrInt public function @covers ::disableSet @dataProvider providerTestSetNotFloatOrInt()
CounterTest::testSetWriteCallback public function @covers ::setWriteCallback
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.
UnitTestCase::setUp protected function 340