Adding unit testing

This commit is contained in:
Axel 2017-07-21 15:14:42 +02:00
parent aea1820254
commit 7b578f878d
4 changed files with 84 additions and 24 deletions

View file

@ -2,11 +2,16 @@
namespace app\Helpers; namespace app\Helpers;
use Exception;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
class Upload { class Upload {
public static function generateFilePath(string $token) { public static function generateFilePath(string $token) {
if (strlen($token) < 5) {
throw new Exception('Invalid token');
}
$path = 'files/'; $path = 'files/';
for ($i = 0; $i < 3; $i++) { for ($i = 0; $i < 3; $i++) {
$letter = substr($token, $i, 1); $letter = substr($token, $i, 1);
@ -33,7 +38,7 @@ class Upload {
return false; return false;
} }
public static function humanFilesize(float $size, $precision = 2) public static function humanFilesize($size, $precision = 2)
{ {
if ($size > 0) { if ($size > 0) {
$size = (int) $size; $size = (int) $size;
@ -152,10 +157,10 @@ class Upload {
// A-B format // A-B format
if (strpos($range, '-') !== false) { if (strpos($range, '-') !== false) {
list($lower, $upper) = explode('-', $range, 2); list($lower, $upper) = explode('-', $range, 2);
$lower_dec = (float)sprintf("%u",ip2long($lower)); $lower_dec = (float)sprintf("%u", ip2long($lower));
$upper_dec = (float)sprintf("%u",ip2long($upper)); $upper_dec = (float)sprintf("%u", ip2long($upper));
$ip_dec = (float)sprintf("%u",ip2long($ip)); $ip_dec = (float)sprintf("%u", ip2long($ip));
return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) ); return ( ($ip_dec >= $lower_dec) && ($ip_dec <= $upper_dec) );
} }
return false; return false;

View file

@ -144,8 +144,6 @@ class UploadController extends Controller
'error' => $e->getMessage() 'error' => $e->getMessage()
], 500); ], 500);
} }
} }
} }

View file

@ -9,15 +9,5 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase class ExampleTest extends TestCase
{ {
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$response = $this->get('/');
$response->assertStatus(200);
}
} }

View file

@ -2,19 +2,86 @@
namespace Tests\Unit; namespace Tests\Unit;
use Upload;
use Exception;
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase class ExampleTest extends TestCase
{ {
/**
* A basic test example. public function testPhpVersion()
*
* @return void
*/
public function testBasicTest()
{ {
$this->assertTrue(true); $this->assertTrue(version_compare(phpversion(), '5.6.4', '>='));
}
public function testOpenSSL() {
$this->assertTrue(extension_loaded('openssl'));
}
public function testPDO() {
$this->assertTrue(extension_loaded('pdo'));
}
public function testMbString() {
$this->assertTrue(extension_loaded('mbstring'));
}
public function testTokenizer() {
$this->assertTrue(extension_loaded('tokenizer'));
}
public function testXml() {
$this->assertTrue(extension_loaded('xml'));
}
public function testJson() {
$this->assertTrue(extension_loaded('json'));
}
public function testZip() {
$this->assertTrue(extension_loaded('zip'));
}
public function testCheckIpRange() {
$ips = [
true => [
'192.168.0.10' => '192.168.0.*',
'10.0.2.1' => '10.0.2.0-10.0.2.10',
'191.57.177.24' => '191.57.177.0/24',
'127.0.0.1' => '127.0.0.1'
],
false => [
'192.168.2.10' => '192.168.0.*',
'10.0.2.11' => '10.0.2.0-10.0.2.10',
'191.57.171.24' => '191.57.177.0/24',
'127.0.0.1' => '127.0.0.2'
]
];
foreach ($ips as $assert => $tests) {
foreach ($tests as $ip => $range) {
if ($assert == true) {
$this->assertTrue(Upload::isValidIp($ip, $range), 'Test should be OK but is not (IP: '.$ip.', range '.$range.')');
}
else {
$this->assertFalse(Upload::isValidIp($ip, $range), 'Test should not be OK but is it (IP: '.$ip.', range '.$range.')');
}
}
}
}
/**
* @expectedException Exception
*/
function testFilePath() {
upload::generateFilePath('abc');
}
function testUploadFilesize() {
$this->assertTrue(is_numeric(Upload::fileMaxSize()), 'Max filesize should be a numeric');
} }
} }