3 namespace Drupal\KernelTests\Core\Common;
5 use Drupal\Component\Utility\Bytes;
6 use Drupal\KernelTests\KernelTestBase;
9 * Parse a predefined amount of bytes and compare the output with the expected
14 class SizeTest extends KernelTestBase {
15 protected $exactTestCases;
16 protected $roundedTestCases;
18 protected function setUp() {
20 $kb = Bytes::KILOBYTE;
21 $this->exactTestCases = [
25 '1 GB' => $kb * $kb * $kb,
26 '1 TB' => $kb * $kb * $kb * $kb,
27 '1 PB' => $kb * $kb * $kb * $kb * $kb,
28 '1 EB' => $kb * $kb * $kb * $kb * $kb * $kb,
29 '1 ZB' => $kb * $kb * $kb * $kb * $kb * $kb * $kb,
30 '1 YB' => $kb * $kb * $kb * $kb * $kb * $kb * $kb * $kb,
32 $this->roundedTestCases = [
34 '1 MB' => ($kb * $kb) - 1, // rounded to 1 MB (not 1000 or 1024 kilobyte!)
35 round(3623651 / ($this->exactTestCases['1 MB']), 2) . ' MB' => 3623651, // megabytes
36 round(67234178751368124 / ($this->exactTestCases['1 PB']), 2) . ' PB' => 67234178751368124, // petabytes
37 round(235346823821125814962843827 / ($this->exactTestCases['1 YB']), 2) . ' YB' => 235346823821125814962843827, // yottabytes
42 * Checks that format_size() returns the expected string.
44 public function testCommonFormatSize() {
45 foreach ([$this->exactTestCases, $this->roundedTestCases] as $test_cases) {
46 foreach ($test_cases as $expected => $input) {
48 ($result = format_size($input, NULL)),
50 $expected . ' == ' . $result . ' (' . $input . ' bytes)'
57 * Cross-tests Bytes::toInt() and format_size().
59 public function testCommonParseSizeFormatSize() {
60 foreach ($this->exactTestCases as $size) {
63 ($parsed_size = Bytes::toInt($string = format_size($size, NULL))),
64 $size . ' == ' . $parsed_size . ' (' . $string . ')'