Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Utility / BytesTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Utility;
4
5 use Drupal\Component\Utility\Bytes;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Tests bytes size parsing helper methods.
10  *
11  * @group Utility
12  *
13  * @coversDefaultClass \Drupal\Component\Utility\Bytes
14  */
15 class BytesTest extends UnitTestCase {
16
17   /**
18    * Tests \Drupal\Component\Utility\Bytes::toInt().
19    *
20    * @param int $size
21    *   The value for the size argument for
22    *   \Drupal\Component\Utility\Bytes::toInt().
23    * @param int $expected_int
24    *   The expected return value from
25    *   \Drupal\Component\Utility\Bytes::toInt().
26    *
27    * @dataProvider providerTestToInt
28    * @covers ::toInt
29    */
30   public function testToInt($size, $expected_int) {
31     $this->assertEquals($expected_int, Bytes::toInt($size));
32   }
33
34   /**
35    * Provides data for testToInt.
36    *
37    * @return array
38    *   An array of arrays, each containing the argument for
39    *   \Drupal\Component\Utility\Bytes::toInt(): size, and the expected return
40    *   value.
41    */
42   public function providerTestToInt() {
43     return [
44       ['1', 1],
45       ['1 byte', 1],
46       ['1 KB'  , Bytes::KILOBYTE],
47       ['1 MB'  , pow(Bytes::KILOBYTE, 2)],
48       ['1 GB'  , pow(Bytes::KILOBYTE, 3)],
49       ['1 TB'  , pow(Bytes::KILOBYTE, 4)],
50       ['1 PB'  , pow(Bytes::KILOBYTE, 5)],
51       ['1 EB'  , pow(Bytes::KILOBYTE, 6)],
52       ['1 ZB'  , pow(Bytes::KILOBYTE, 7)],
53       ['1 YB'  , pow(Bytes::KILOBYTE, 8)],
54       ['23476892 bytes', 23476892],
55       ['76MRandomStringThatShouldBeIgnoredByParseSize.', 79691776], // 76 MB
56       ['76.24 Giggabyte', 81862076662], // 76.24 GB (with typo)
57     ];
58   }
59
60 }