PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.5
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.5
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / rosell-dk / webp-convert / tests / Convert / Helpers / PhpIniSizesTest.php

PhpIniSizesTest.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.5, at vendor/rosell-dk/webp-convert/tests/Convert/Helpers/PhpIniSizesTest.php

86 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WebPConvert\Tests\Convert\BaseConverters;
4
5 use WebPConvert\Convert\Helpers\PhpIniSizes;
6
7 use PHPUnit\Framework\TestCase;
8
9 class PhpIniSizesTest extends TestCase
10 {
11
12 public function testParseShortHandSize()
13 {
14 // Test without units
15 $this->assertEquals(0, PhpIniSizes::parseShortHandSize('0'));
16 $this->assertEquals(10, PhpIniSizes::parseShortHandSize('10'));
17
18 // Test "k" unit
19 $this->assertEquals(1024, PhpIniSizes::parseShortHandSize('1k'));
20
21 // Test capitial "K"
22 $this->assertEquals(1024, PhpIniSizes::parseShortHandSize('1K'));
23
24 // Test "M" unit
25 $this->assertEquals(1024 * 1024, PhpIniSizes::parseShortHandSize('1M'));
26
27 // Test "G" unit
28 $this->assertEquals(1024 * 1024 * 1024, PhpIniSizes::parseShortHandSize('1G'));
29
30
31 // Moving to terrabytes, we have to be careful.
32 // Terrabytes cannot be represented as integers on 32 bit systems.
33 // (on 32 bit systems, max integer value is 107.374.182.400, which can represent up to ~107G)
34
35 // Testing floating point numbers for equality is prone to errors.
36 //$this->assertInternalType('int', PhpIniSizes::parseShortHandSize('10'));
37 //$this->assertEquals(10.0, 10);
38
39
40 // So, ie "200G" can not be represented by an int.
41
42 // The computation goes:
43 // floor($size) * pow(1024, stripos('bkmgtpezy', $unit[0]));
44
45 // floor() always returns float, according to docs (but may also
46 // pow() returns int unless the number is too high, in that case it returns float.
47 // And the result? What do you get if you multiply an int and a float (which is in fact representating an integer),
48 // and the result is more than PHP_INT_MAX?
49 // In the docs, it states the following:
50 // "an operation which results in a number beyond the bounds of the integer type will return a float instead."
51 // [https://www.php.net/manual/en/language.types.integer.php]
52 // Se it seems we are good.
53 // But let's check!
54
55 $greatComputation = floor(100) * PHP_INT_MAX;
56 $this->assertGreaterThan(PHP_INT_MAX, $greatComputation);
57
58 $greaterComputation = floatval(200) * floatval(PHP_INT_MAX);
59 $this->assertGreaterThan($greatComputation, $greaterComputation);
60
61 // Test "T" unit
62 $this->assertGreaterThan(PhpIniSizes::parseShortHandSize('1G'), PhpIniSizes::parseShortHandSize('100T'));
63 $this->assertGreaterThan(1024 * 1024 * 1024 * 1024, PhpIniSizes::parseShortHandSize('1T') + 1);
64
65
66 // Test that decimals are trunked, as described here:
67 // https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
68 $this->assertEquals(1024, PhpIniSizes::parseShortHandSize('1.5k'));
69 $this->assertEquals(0, PhpIniSizes::parseShortHandSize('0.5M'));
70
71
72 // Test syntax violations, which must result in parse error.
73 $this->assertFalse(PhpIniSizes::parseShortHandSize('0.5MM'));
74 $this->assertFalse(PhpIniSizes::parseShortHandSize('//5'));
75 }
76
77 /* TODO...
78 public function testTestFilesizeRequirements()
79 {
80 $iniValue = ini_get('upload_max_filesize');
81
82 // could we call ini_set? instead of mocking ini_get ?
83 }
84 */
85 }
86