View source
<?php
namespace Drupal\Tests\purge\Unit\Counter;
use Drupal\purge\Counter\Counter;
use Drupal\purge\Plugin\Purge\Purger\Exception\BadBehaviorException;
use Drupal\Tests\UnitTestCase;
class CounterTest extends UnitTestCase {
public function testDisableDecrement() : void {
$counter = new Counter();
$counter
->disableDecrement();
$this
->expectException(\LogicException::class);
$this
->expectExceptionMessage('No ::decrement() permission on this object.');
$counter
->decrement();
}
public function testDisableIncrement() : void {
$counter = new Counter();
$counter
->disableIncrement();
$this
->expectException(\LogicException::class);
$this
->expectExceptionMessage('No ::increment() permission on this object.');
$counter
->increment();
}
public function testDisableSet() : void {
$counter = new Counter();
$counter
->disableSet();
$this
->expectException(\LogicException::class);
$this
->expectExceptionMessage('No ::set() permission on this object.');
$counter
->set(5);
}
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()));
}
public function providerTestGet() : array {
return [
[
0,
],
[
5,
],
[
1.3,
],
[
8.9,
],
];
}
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()));
}
public function providerTestGetInteger() : array {
return [
[
0,
],
[
5,
],
[
1.3,
],
[
8.9,
],
];
}
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);
}
public function providerTestSetNotFloatOrInt() : array {
return [
[
FALSE,
],
[
"0",
],
[
NULL,
],
];
}
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);
}
public function testSet($value) : void {
$counter = new Counter();
$counter
->set($value);
$this
->assertEquals($value, $counter
->get());
}
public function providerTestSet() : array {
return [
[
0,
],
[
5,
],
[
1.3,
],
[
8.9,
],
];
}
public function testDecrement($start, $subtract, $result) : void {
$counter = new Counter($start);
$counter
->decrement($subtract);
$this
->assertEquals($result, $counter
->get());
}
public function providerTestDecrement() : array {
return [
[
4.0,
0.2,
3.8,
],
[
2,
1,
1,
],
[
1,
1,
0,
],
];
}
public function testDecrementInvalidValue($value) : void {
$counter = new Counter(10);
$this
->expectException(BadBehaviorException::class);
$this
->expectExceptionMessage('Given $amount is zero or negative.');
$counter
->decrement($value);
}
public function providerTestDecrementInvalidValue() : array {
return [
[
0,
],
[
0.0,
],
[
-1,
],
];
}
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);
}
public function providerTestDecrementNotFloatOrInt() : array {
return [
[
FALSE,
],
[
"0",
],
[
NULL,
],
];
}
public function testIncrement($start, $add, $result) : void {
$counter = new Counter($start);
$counter
->increment($add);
$this
->assertEquals($result, $counter
->get());
}
public function providerTestIncrement() : array {
return [
[
4.0,
0.2,
4.2,
],
[
0.1,
1,
1.1,
],
[
2,
1,
3,
],
];
}
public function testIncrementInvalidValue($value) : void {
$counter = new Counter(10);
$this
->expectException(BadBehaviorException::class);
$this
->expectExceptionMessage('Given $amount is zero or negative.');
$counter
->increment($value);
}
public function providerTestIncrementInvalidValue() : array {
return [
[
0,
],
[
0.0,
],
[
-1,
],
];
}
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);
}
public function providerTestIncrementNotFloatOrInt() : array {
return [
[
FALSE,
],
[
"0",
],
[
NULL,
],
];
}
public function testSetWriteCallback($value_start, $call, $value_end) : void {
$counter = new Counter($value_start);
$passed_value = NULL;
$callback = function ($_value) use (&$passed_value) {
$passed_value = $_value;
};
$counter
->setWriteCallback($callback);
$method = array_shift($call);
call_user_func_array([
$counter,
$method,
], $call);
$this
->assertEquals($passed_value, $value_end);
}
public function providerTestSetWriteCallback() : array {
return [
[
0,
[
'set',
5,
],
5,
],
[
1.8,
[
'increment',
2.3,
],
4.1,
],
[
1.6,
[
'decrement',
0.3,
],
1.3,
],
];
}
}