382ae29da804311650cb85984c8266320aa0e10f
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Batch / PercentagesTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Batch;
4
5 use Drupal\Core\Batch\Percentage;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Core\Batch\Percentage
10  * @group Batch
11  *
12  * Tests the Batch helper object to make sure that the rounding works properly
13  * in all cases.
14  */
15 class PercentagesTest extends UnitTestCase {
16   protected $testCases = [];
17
18   /**
19    * @dataProvider providerTestPercentages
20    * @covers ::format
21    */
22   public function testPercentages($total, $current, $expected_result) {
23     $actual_result = Percentage::format($total, $current);
24     $this->assertEquals($actual_result, $expected_result, sprintf('The expected the batch api percentage at the state %s/%s is %s%% and got %s%%.', $current, $total, $expected_result, $actual_result));
25   }
26
27   /**
28    * Provide data for batch unit tests.
29    *
30    * @return array
31    *   An array of data used by the test.
32    */
33   public function providerTestPercentages() {
34     // Set up an array of test cases.
35     return [
36       // array(total, current, expected).
37       // 1/2 is 50%.
38       [2, 1, '50'],
39       // Though we should never encounter a case where the current set is set
40       // 0, if we did, we should get 0%.
41       [3, 0, '0'],
42       // 1/3 is closer to 33% than to 34%.
43       [3, 1, '33'],
44       // 2/3 is closer to 67% than to 66%.
45       [3, 2, '67'],
46       // 1/199 should round up to 1%.
47       [199, 1, '1'],
48       // 198/199 should round down to 99%.
49       [199, 198, '99'],
50       // 199/200 would have rounded up to 100%, which would give the false
51       // impression of being finished, so we add another digit and should get
52       // 99.5%.
53       [200, 199, '99.5'],
54       // The same logic holds for 1/200: we should get 0.5%.
55       [200, 1, '0.5'],
56       // Numbers that come out evenly, such as 50/200, should be forced to have
57       // extra digits for consistency.
58       [200, 50, '25.0'],
59       // Regardless of number of digits we're using, 100% should always just be
60       // 100%.
61       [200, 200, '100'],
62       // 1998/1999 should similarly round down to 99.9%.
63       [1999, 1998, '99.9'],
64       // 1999/2000 should add another digit and go to 99.95%.
65       [2000, 1999, '99.95'],
66       // 19999/20000 should add yet another digit and go to 99.995%.
67       [20000, 19999, '99.995'],
68       // The next five test cases simulate a batch with a single operation
69       // ('total' equals 1) that takes several steps to complete. Within the
70       // operation, we imagine that there are 501 items to process, and 100 are
71       // completed during each step. The percentages we get back should be
72       // rounded the usual way for the first few passes (i.e., 20%, 40%, etc.),
73       // but for the last pass through, when 500 out of 501 items have been
74       // processed, we do not want to round up to 100%, since that would
75       // erroneously indicate that the processing is complete.
76       ['total' => 1, 'current' => 100 / 501, '20'],
77       ['total' => 1, 'current' => 200 / 501, '40'],
78       ['total' => 1, 'current' => 300 / 501, '60'],
79       ['total' => 1, 'current' => 400 / 501, '80'],
80       ['total' => 1, 'current' => 500 / 501, '99.8'],
81     ];
82   }
83
84 }