Classi per la configurazione upload
Cristiano Magro
2017-12-22 e01f6bdd9010a2fb5a52bd87196a24baa01d7c92
Introduco metodo per convertire stringa in bytes

+ tests
4 files added
1 files modified
89 ■■■■■ changed files
README.md 6 ●●●●● patch | view | raw | blame | history
src/UploadFile/SetupUpload.class.php 21 ●●●●● patch | view | raw | blame | history
tests/SetupUploadTest.php 47 ●●●●● patch | view | raw | blame | history
tests/bootstrap.php 7 ●●●●● patch | view | raw | blame | history
tests/phpunit.xml 8 ●●●●● patch | view | raw | blame | history
README.md
New file
@@ -0,0 +1,6 @@
Classi per gestire la configurazione dell'upload dei file.
#test
$cd tests
$ phpunit.phar -c phpunit.xml
src/UploadFile/SetupUpload.class.php
@@ -164,4 +164,25 @@
        return json_encode($dati);
    }
    /**
     * Converte una stringa nei corrispondenti bytes.
     *
     * 1 Mb = 1024^2
     *
     * @param string $strSize Stringa da convertire
     * @return integer
     */
    public static function str2Bytes($strSize) {
        $strUnit = strtolower($strSize);
        $strUnit = preg_replace('/[^a-z]/', '', $strUnit);
        $value = intval(preg_replace('/[^0-9]/', '', $strSize));
        $Units = array('b' => 0, 'kb' => 1, 'mb' => 2, 'gb' => 3, 'tb' => 4);
        $Exponent = isset($Units[$strUnit]) ? $Units[$strUnit] : 0;
        return ($value * pow(1024, $Exponent));
    }
}
tests/SetupUploadTest.php
New file
@@ -0,0 +1,47 @@
<?php
require 'bootstrap.php';
/**
 * Description of FbResponseTest
 *
 * @author Cristiano Magro
 */
class SetupUploadTest extends PHPUnit_Framework_TestCase {
    public static function setUpBeforeClass() {
    }
    public function test_str2Bytes() {
        $this->assertEquals('1', SetupUpload::str2Bytes('1'));
        $this->assertEquals('1', SetupUpload::str2Bytes('1b'));
        $this->assertEquals('1', SetupUpload::str2Bytes('1 b'));
        $this->assertEquals('1024', SetupUpload::str2Bytes('1 kb'));
        $this->assertEquals('1024', SetupUpload::str2Bytes('1 Kb'));
        $this->assertEquals('1024', SetupUpload::str2Bytes('1 KB'));
        $this->assertEquals('1024', SetupUpload::str2Bytes('1 kb'));
        $this->assertEquals('2048', SetupUpload::str2Bytes('2 Kb'));
        $this->assertEquals('1024', SetupUpload::str2Bytes('1 KB'));
        $this->assertEquals('2048', SetupUpload::str2Bytes('2           Kb'));
        $this->assertEquals('1048576', SetupUpload::str2Bytes('1 mB'));
        $this->assertEquals('0', SetupUpload::str2Bytes('0 B'));
        $this->assertEquals('0', SetupUpload::str2Bytes('0 kB'));
        $this->assertEquals('0', SetupUpload::str2Bytes('0 mB'));
        $this->assertEquals('0', SetupUpload::str2Bytes('0 GB'));
        $this->assertEquals('0', SetupUpload::str2Bytes('0 TB'));
        $this->assertEquals('0', SetupUpload::str2Bytes('0 XB'));
    }
}
tests/bootstrap.php
New file
@@ -0,0 +1,7 @@
<?php
require_once __DIR__ . '/../vendor/autoload.php';
$baseDir = str_replace('/tests', '', __DIR__);
define('APPLICATION_PATH', $baseDir);
tests/phpunit.xml
New file
@@ -0,0 +1,8 @@
<phpunit bootstrap="bootstrap.php"
         stopOnFailure="true">
    <testsuites>
        <testsuite name="FileUpload">
            <directory>.</directory>
        </testsuite>
    </testsuites>
</phpunit>