class CounterTest in Purge 8.3
@coversDefaultClass \Drupal\purge\Counter\Counter
@group purge
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\purge\Unit\Counter\CounterTest
Expanded class hierarchy of CounterTest
File
- tests/
src/ Unit/ Counter/ CounterTest.php, line 14
Namespace
Drupal\Tests\purge\Unit\CounterView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CounterTest:: |
public | function | Provides test data for testDecrement(). | |
CounterTest:: |
public | function | Provides test data for testDecrementInvalidValue(). | |
CounterTest:: |
public | function | Provides test data for testDecrementNotFloatOrInt(). | |
CounterTest:: |
public | function | Provides test data for testGet(). | |
CounterTest:: |
public | function | Provides test data for testGetInteger(). | |
CounterTest:: |
public | function | Provides test data for testIncrement(). | |
CounterTest:: |
public | function | Provides test data for testIncrementInvalidValue(). | |
CounterTest:: |
public | function | Provides test data for testIncrementNotFloatOrInt(). | |
CounterTest:: |
public | function | Provides test data for testSet(). | |
CounterTest:: |
public | function | Provides test data for testSetNotFloatOrInt(). | |
CounterTest:: |
public | function | Provides test data for testSetWriteCallback(). | |
CounterTest:: |
public | function | @covers ::decrement | |
CounterTest:: |
public | function | @covers ::decrement @dataProvider providerTestDecrementInvalidValue() | |
CounterTest:: |
public | function | @covers ::decrement @dataProvider providerTestDecrementNotFloatOrInt() | |
CounterTest:: |
public | function | @covers ::disableDecrement | |
CounterTest:: |
public | function | @covers ::disableIncrement | |
CounterTest:: |
public | function | @covers ::disableSet | |
CounterTest:: |
public | function | @covers ::get | |
CounterTest:: |
public | function | @covers ::getInteger | |
CounterTest:: |
public | function | @covers ::increment | |
CounterTest:: |
public | function | @covers ::increment @dataProvider providerTestIncrementInvalidValue() | |
CounterTest:: |
public | function | @covers ::increment @dataProvider providerTestIncrementNotFloatOrInt() | |
CounterTest:: |
public | function | @covers ::set | |
CounterTest:: |
public | function | @covers ::disableSet | |
CounterTest:: |
public | function | @covers ::disableSet @dataProvider providerTestSetNotFloatOrInt() | |
CounterTest:: |
public | function | @covers ::setWriteCallback | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |