function PercentagesUnitTest::testBatchPercentages

Tests the _batch_api_percentage() function.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Batch/PercentagesUnitTest.php, line 86
Definition of Drupal\system\Tests\Batch\PercentagesUnitTest.

Class

PercentagesUnitTest
Unit tests of progress percentage rounding.

Namespace

Drupal\system\Tests\Batch

Code

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(format_string('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(format_string('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,
      )));
    }
  }
}