class BatchPercentagesUnitTestCase in Drupal 7
Tests the function _batch_api_percentage() to make sure that the rounding works properly in all cases.
Hierarchy
- class \DrupalTestCase
- class \DrupalUnitTestCase
- class \BatchPercentagesUnitTestCase
- class \DrupalUnitTestCase
Expanded class hierarchy of BatchPercentagesUnitTestCase
File
- modules/
simpletest/ tests/ batch.test, line 319 - Tests for the Batch API.
View source
class BatchPercentagesUnitTestCase extends DrupalUnitTestCase {
protected $testCases = array();
public static function getInfo() {
return array(
'name' => 'Batch percentages',
'description' => 'Unit tests of progress percentage rounding.',
'group' => 'Batch API',
);
}
function setUp() {
// Set up an array of test cases, where the expected values are the keys,
// and the values are arrays with the keys 'total' and 'current',
// corresponding with the function parameters of _batch_api_percentage().
$this->testCases = array(
// 1/2 is 50%.
'50' => array(
'total' => 2,
'current' => 1,
),
// Though we should never encounter a case where the current set is set
// 0, if we did, we should get 0%.
'0' => array(
'total' => 3,
'current' => 0,
),
// 1/3 is closer to 33% than to 34%.
'33' => array(
'total' => 3,
'current' => 1,
),
// 2/3 is closer to 67% than to 66%.
'67' => array(
'total' => 3,
'current' => 2,
),
// 1/199 should round up to 1%.
'1' => array(
'total' => 199,
'current' => 1,
),
// 198/199 should round down to 99%.
'99' => array(
'total' => 199,
'current' => 198,
),
// 199/200 would have rounded up to 100%, which would give the false
// impression of being finished, so we add another digit and should get
// 99.5%.
'99.5' => array(
'total' => 200,
'current' => 199,
),
// The same logic holds for 1/200: we should get 0.5%.
'0.5' => array(
'total' => 200,
'current' => 1,
),
// Numbers that come out evenly, such as 50/200, should be forced to have
// extra digits for consistancy.
'25.0' => array(
'total' => 200,
'current' => 50,
),
// Regardless of number of digits we're using, 100% should always just be
// 100%.
'100' => array(
'total' => 200,
'current' => 200,
),
// 1998/1999 should similarly round down to 99.9%.
'99.9' => array(
'total' => 1999,
'current' => 1998,
),
// 1999/2000 should add another digit and go to 99.95%.
'99.95' => array(
'total' => 2000,
'current' => 1999,
),
// 19999/20000 should add yet another digit and go to 99.995%.
'99.995' => array(
'total' => 20000,
'current' => 19999,
),
// The next five test cases simulate a batch with a single operation
// ('total' equals 1) that takes several steps to complete. Within the
// operation, we imagine that there are 501 items to process, and 100 are
// completed during each step. The percentages we get back should be
// rounded the usual way for the first few passes (i.e., 20%, 40%, etc.),
// but for the last pass through, when 500 out of 501 items have been
// processed, we do not want to round up to 100%, since that would
// erroneously indicate that the processing is complete.
'20' => array(
'total' => 1,
'current' => 100 / 501,
),
'40' => array(
'total' => 1,
'current' => 200 / 501,
),
'60' => array(
'total' => 1,
'current' => 300 / 501,
),
'80' => array(
'total' => 1,
'current' => 400 / 501,
),
'99.8' => array(
'total' => 1,
'current' => 500 / 501,
),
);
require_once DRUPAL_ROOT . '/includes/batch.inc';
parent::setUp();
}
/**
* Test the _batch_api_percentage() function.
*/
function testBatchPercentages() {
foreach ($this->testCases as $expected_result => $arguments) {
// PHP sometimes casts numeric strings that are array keys to integers,
// cast them back here.
$expected_result = (string) $expected_result;
$total = $arguments['total'];
$current = $arguments['current'];
$actual_result = _batch_api_percentage($total, $current);
if ($actual_result === $expected_result) {
$this
->pass(t('Expected the batch api percentage at the state @numerator/@denominator to be @expected%, and got @actual%.', array(
'@numerator' => $current,
'@denominator' => $total,
'@expected' => $expected_result,
'@actual' => $actual_result,
)));
}
else {
$this
->fail(t('Expected the batch api percentage at the state @numerator/@denominator to be @expected%, but got @actual%.', array(
'@numerator' => $current,
'@denominator' => $total,
'@expected' => $expected_result,
'@actual' => $actual_result,
)));
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BatchPercentagesUnitTestCase:: |
protected | property | ||
BatchPercentagesUnitTestCase:: |
public static | function | ||
BatchPercentagesUnitTestCase:: |
function |
Sets up unit test environment. Overrides DrupalUnitTestCase:: |
||
BatchPercentagesUnitTestCase:: |
function | Test the _batch_api_percentage() function. | ||
DrupalTestCase:: |
protected | property | Assertions thrown in that test case. | |
DrupalTestCase:: |
protected | property | The database prefix of this test run. | |
DrupalTestCase:: |
protected | property | The original file directory, before it was changed for testing purposes. | |
DrupalTestCase:: |
public | property | Current results of this test case. | |
DrupalTestCase:: |
protected | property | Flag to indicate whether the test has been set up. | |
DrupalTestCase:: |
protected | property | ||
DrupalTestCase:: |
protected | property | ||
DrupalTestCase:: |
protected | property | This class is skipped when looking for the source of an assertion. | |
DrupalTestCase:: |
protected | property | The test run ID. | |
DrupalTestCase:: |
protected | property | Time limit for the test. | |
DrupalTestCase:: |
public | property | Whether to cache the installation part of the setUp() method. | |
DrupalTestCase:: |
public | property | Whether to cache the modules installation part of the setUp() method. | |
DrupalTestCase:: |
protected | property | URL to the verbose output file directory. | |
DrupalTestCase:: |
protected | function | Internal helper: stores the assert. | |
DrupalTestCase:: |
protected | function | Check to see if two values are equal. | |
DrupalTestCase:: |
protected | function | Check to see if a value is false (an empty string, 0, NULL, or FALSE). | |
DrupalTestCase:: |
protected | function | Check to see if two values are identical. | |
DrupalTestCase:: |
protected | function | Check to see if two values are not equal. | |
DrupalTestCase:: |
protected | function | Check to see if two values are not identical. | |
DrupalTestCase:: |
protected | function | Check to see if a value is not NULL. | |
DrupalTestCase:: |
protected | function | Check to see if a value is NULL. | |
DrupalTestCase:: |
protected | function | Check to see if a value is not false (not an empty string, 0, NULL, or FALSE). | |
DrupalTestCase:: |
public static | function | Delete an assertion record by message ID. | |
DrupalTestCase:: |
protected | function | Fire an error assertion. | 1 |
DrupalTestCase:: |
public | function | Handle errors during test runs. | 1 |
DrupalTestCase:: |
protected | function | Handle exceptions. | |
DrupalTestCase:: |
protected | function | Fire an assertion that is always negative. | |
DrupalTestCase:: |
public static | function | Converts a list of possible parameters into a stack of permutations. | |
DrupalTestCase:: |
protected | function | Cycles through backtrace until the first non-assertion method is found. | |
DrupalTestCase:: |
public static | function | Returns the database connection to the site running Simpletest. | |
DrupalTestCase:: |
public static | function | Store an assertion from outside the testing context. | |
DrupalTestCase:: |
protected | function | Fire an assertion that is always positive. | |
DrupalTestCase:: |
public static | function | Generates a random string containing letters and numbers. | |
DrupalTestCase:: |
public static | function | Generates a random string of ASCII characters of codes 32 to 126. | |
DrupalTestCase:: |
public | function | Run all tests in this class. | |
DrupalTestCase:: |
protected | function | Logs a verbose message in a text file. | |
DrupalUnitTestCase:: |
protected | function | 1 | |
DrupalUnitTestCase:: |
function |
Constructor for DrupalUnitTestCase. Overrides DrupalTestCase:: |