Add phpunit tests folder, fix in Math method floorp when precision was larger then number length
This commit is contained in:
@@ -85,9 +85,7 @@ return [
|
||||
// to parse, but not analyze
|
||||
"exclude_analysis_directory_list" => [
|
||||
'www/vendor',
|
||||
// 'www/lib/FileUpload',
|
||||
'www/lib/pChart',
|
||||
'www/lib/pChart2.1.4',
|
||||
'www/tests',
|
||||
'www/lib/Smarty',
|
||||
'www/lib/smarty-3.1.30',
|
||||
'www/templates_c',
|
||||
|
||||
3
4dev/checking/phan.sh
Normal file
3
4dev/checking/phan.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
base="/storage/var/www/html/developers/clemens/core_data/php_libraries/trunk/";
|
||||
# must be run in ${base}www/
|
||||
phan --progress-bar -C --analyze-twice
|
||||
3
4dev/checking/phpstan.sh
Normal file
3
4dev/checking/phpstan.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
base="/storage/var/www/html/developers/clemens/core_data/php_libraries/trunk/";
|
||||
# must be run in ${base}www/
|
||||
phpstan
|
||||
4
4dev/checking/phpunit.sh
Executable file
4
4dev/checking/phpunit.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
base="/storage/var/www/html/developers/clemens/core_data/php_libraries/trunk/";
|
||||
# -c phpunit.xml
|
||||
# --testdox
|
||||
${base}www/vendor/bin/phpunit -c ${base}phpunit.xml ${base}4dev/tests/
|
||||
@@ -1,21 +1,27 @@
|
||||
Install composer:
|
||||
curl -sS https://getcomposer.org/installer | /usr/local/php-7.3-httpd-2.4/bin/php
|
||||
# old
|
||||
curl -sS https://getcomposer.org/installer | /usr/local/php-8.0-httpd-2.4/bin/php
|
||||
# new (4 steps) https://getcomposer.org/download/
|
||||
/usr/local/php-8.0-httpd-2.4/bin/php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
|
||||
/usr/local/php-8.0-httpd-2.4/bin/php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
|
||||
/usr/local/php-8.0-httpd-2.4/bin/php composer-setup.php
|
||||
/usr/local/php-8.0-httpd-2.4/bin/php -r "unlink('composer-setup.php');
|
||||
|
||||
Update composer phar file
|
||||
/usr/local/php-7.3-httpd-2.4/bin/php composer.phar selfupdate
|
||||
/usr/local/php-8.0-httpd-2.4/bin/php composer.phar selfupdate
|
||||
|
||||
Install something:
|
||||
/usr/local/php-7.3-httpd-2.4/bin/php composer.phar require something/something
|
||||
/usr/local/php-8.0-httpd-2.4/bin/php composer.phar require something/something
|
||||
|
||||
Update all installed:
|
||||
/usr/local/php-7.3-httpd-2.4/bin/php composer.phar update
|
||||
/usr/local/php-8.0-httpd-2.4/bin/php composer.phar update
|
||||
|
||||
Or update only one package:
|
||||
/usr/local/php-7.3-httpd-2.4/bin/php composer.phar something/something
|
||||
/usr/local/php-8.0-httpd-2.4/bin/php composer.phar something/something
|
||||
|
||||
Install AWS SDK:
|
||||
/usr/local/php-7.3-httpd-2.4/bin/php -d memory_limit=-1 composer.phar require aws/aws-sdk-php
|
||||
/usr/local/php-8.0-httpd-2.4/bin/php -d memory_limit=-1 composer.phar require aws/aws-sdk-php
|
||||
|
||||
Install zipStream:
|
||||
/usr/local/php-7.3-httpd-2.4/bin/php composer.phar require maennchen/zipstream-php
|
||||
/usr/local/php-8.0-httpd-2.4/bin/php composer.phar require maennchen/zipstream-php
|
||||
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
phan --progress-bar -C --analyze-twice
|
||||
@@ -1 +0,0 @@
|
||||
phpstan
|
||||
137
4dev/tests/CoreLibsConvertMathTest.php
Normal file
137
4dev/tests/CoreLibsConvertMathTest.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Tests for
|
||||
* \CoreLibs\Convert\Math
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Undocumented class
|
||||
* @testdox CoreLibs\Convert\Math method tests
|
||||
*/
|
||||
final class CoreLibsConvertMathTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fceilProvider(): array
|
||||
{
|
||||
return [
|
||||
'5.5 must be 6' => [5.5, 6],
|
||||
'5.1234567890 with 5 must be 6' => [5.1234567890, 6],
|
||||
'6 must be 6' => [6, 6]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @dataProvider fceilProvider
|
||||
* @testdox Math::fceil: Input $input must be $expected
|
||||
*
|
||||
* @param int $input
|
||||
* @param int $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testMathFceilValue(float $input, int $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Convert\Math::fceil($input)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function floorProvider(): array
|
||||
{
|
||||
return [
|
||||
'5123456 with -3 must be 5123000' => [5123456, -3, 5123000],
|
||||
'5123456 with -10 must be 5000000' => [5123456, -10, 5000000]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @dataProvider floorProvider
|
||||
* @testdox Math::floor: Input $input with cutoff $cutoff must be $expected
|
||||
*
|
||||
* @param int $input
|
||||
* @param int $cutoff
|
||||
* @param int $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testMathFloorValue(int $input, int $cutoff, int $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Convert\Math::floorp($input, $cutoff)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function initNumericProvider(): array
|
||||
{
|
||||
return [
|
||||
'5 must be 5' => [5, 5, 'int'],
|
||||
'5.123 must be 5.123' => [5.123, 5.123, 'float'],
|
||||
"'5' must be 5" => ['5', 5, 'string'],
|
||||
"'5.123' must be 5.123" => ['5.123', 5.123, 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @dataProvider initNumericProvider
|
||||
* @testdox Math::initNumeric: Input $info $input must match $expected [$_dataName]
|
||||
*
|
||||
* @param int|float|string $input
|
||||
* @param float $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testMathInitNumericValue($input, float $expected, string $info): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Convert\Math::initNumeric($input)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A testWith sample
|
||||
*
|
||||
* @testdox Math::initNumeric: alternate tests $input => $expected ($info) [$_dataName]
|
||||
* @testWith [123.123, 123.123, "float"]
|
||||
* ["123.123", 123.123, "string"]
|
||||
*
|
||||
* @param [type] $input
|
||||
* @param float $expected
|
||||
* @param string $info
|
||||
* @return void
|
||||
*/
|
||||
public function testMathInitNumericValueAlt($input, float $expected, string $info): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Convert\Math::initNumeric($input)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,8 @@ parameters:
|
||||
- www/lib/Smarty*
|
||||
# ignore composer
|
||||
- www/vendor
|
||||
# ignore tst folder
|
||||
- www/tests
|
||||
# ignore errores with
|
||||
ignoreErrors:
|
||||
# -
|
||||
|
||||
6
phpunit.xml
Normal file
6
phpunit.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<phpunit
|
||||
cacheResultFile="/tmp/phpunit-corelibs.result.cache"
|
||||
colors="true"
|
||||
verbose="true"
|
||||
>
|
||||
</phpunit>
|
||||
@@ -41,11 +41,15 @@ print '<div><a href="class_test.php">Class Test Master</a></div>';
|
||||
|
||||
print "FCEIL: " . $_math->fceil(5.1234567890, 5) . "<br>";
|
||||
print "FLOORP: " . $_math->floorp(5123456, -3) . "<br>";
|
||||
print "FLOORP: " . $_math->floorp(5123456, -10) . "<br>";
|
||||
print "INITNUMERIC: " . $_math->initNumeric('123') . "<br>";
|
||||
|
||||
print "S-FCEIL: " . $math_class::fceil(5.1234567890, 5) . "<br>";
|
||||
print "S-FLOORP: " . $math_class::floorp(5123456, -3) . "<br>";
|
||||
print "S-INITNUMERIC: " . $math_class::initNumeric(123) . "<br>";
|
||||
print "S-INITNUMERIC: " . $math_class::initNumeric(123.456) . "<br>";
|
||||
print "S-INITNUMERIC: " . $math_class::initNumeric('123') . "<br>";
|
||||
print "S-INITNUMERIC: " . $math_class::initNumeric('123.456') . "<br>";
|
||||
|
||||
// DEPRECATED
|
||||
/* print "FCEIL: ".$basic->fceil(5.1234567890, 5)."<br>";
|
||||
|
||||
@@ -31,6 +31,11 @@ class Math
|
||||
*/
|
||||
public static function floorp(float $number, int $precision = -2): float
|
||||
{
|
||||
// if precision is requal or larger than the number length,
|
||||
// set precision to length -1
|
||||
if (abs($precision) >= strlen((string)$number)) {
|
||||
$precision = (strlen((string)$number) - 1) * -1;
|
||||
}
|
||||
$mult = pow(10, $precision); // Can be cached in lookup table
|
||||
return floor($number * $mult) / $mult;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user