| 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 |
|