ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DataSizeTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once("./vendor/composer/vendor/autoload.php");
22
24use PHPUnit\Framework\TestCase;
25
31class DataSizeTest extends TestCase
32{
33 public static function provideDataSizes(): array
34 {
35 return [
36 [1000, '1000 B'],
37 [1001, '1 KB'],
38 [1023, '1.02 KB'],
39 [1024, '1.02 KB'],
40 [1025, '1.03 KB'],
41 [10000, '10 KB'],
42 [11000, '11 KB'],
43 [28_566_695, '28.57 MB'],
44 [48_521_625, '48.52 MB'],
45 [58_777_412_654, '58.78 GB'],
46 [46_546_544_654_545, '46.55 TB'],
47 [125_862_151_563_255_622, '125862.15 TB'],
48 ];
49 }
50
51 #[\PHPUnit\Framework\Attributes\DataProvider('provideDataSizes')]
52 public function testDifferentDataSizes(int $bytes, string $expected_representation): void
53 {
54 $datasize = new DataSize($bytes, DataSize::Byte);
55
56 $this->assertEquals($expected_representation, $datasize->__toString());
57 }
58
59 #[\PHPUnit\Framework\Attributes\DataProvider('tDataProvider')]
60 public function test_normal($a, $b, $expected, $expected_in_bytes): void
61 {
62 $ds = new DataSize($a, $b);
63 $this->assertEquals($a / $b, $ds->getSize());
64 $this->assertEquals($b, $ds->getUnit());
65 $this->assertEquals($expected, $ds->__toString());
66 if ($expected_in_bytes) {
67 $this->assertEquals($expected_in_bytes, (int) $ds->inBytes());
68 }
69 }
70
71 public function test_division_by_zero(): void
72 {
73 try {
74 $ds = new DataSize(4533, 0);
75 $this->assertFalse("This should not happen");
76 } catch (Exception | DivisionByZeroError $e) {
77 $this->assertTrue(true);
78 }
79 }
80
81 public static function tDataProvider(): array
82 {
83 return [
84 [122, 1000, "122 B", 122],
85 [-122, 1000, "-122 B", -122],
86 [122, 1000000, "122 B", 122],
87 [-122, 1000000, "-122 B", -122],
88 [122, 1000000000, "122 B", 122],
89 [-122, 1000000000, "-122 B", -122],
90 [122, 1000000000000, "122 B", null], // There is a float rounding error here
91 [-122, 1000000000000, "-122 B", null], // There is a float rounding error here
92 [122, 1024, "122 B", 122],
93 [-122, 1024, "-122 B", -122],
94 [122, 1048576, "122 B", 122],
95 [-122, 1048576, "-122 B", -122],
96 [122, 1073741824, "122 B", 122],
97 [-122, 1073741824, "-122 B", -122],
98 [122, 1099511627776, "122 B", 122],
99 [-122, 1099511627776, "-122 B", -122],
100 [10 * DataSize::KiB, DataSize::KiB, "10.24 KB", 10 * DataSize::KiB],
101 ];
102 }
103}
Testing the DataSize object.
static provideDataSizes()
test_normal($a, $b, $expected, $expected_in_bytes)
testDifferentDataSizes(int $bytes, string $expected_representation)
static tDataProvider()
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:31
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples