Improve test coverage
This commit is contained in:
@@ -110,7 +110,7 @@ final class MD_STD {
|
||||
public static function unlink(string $filename):void {
|
||||
|
||||
if (\unlink($filename) === false) {
|
||||
throw new Exception("Failed to delete: $filename");
|
||||
throw new MDFileDoesNotExist("Failed to delete: $filename");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -173,26 +173,6 @@ final class MD_STD {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function checking if a string starts with another.
|
||||
* DEPRECATED. Can be replaced by PHP8's str_starts_with.
|
||||
*
|
||||
* @param non-empty-string $haystack String to check.
|
||||
* @param non-empty-string $needle Potential start of $haystack.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function startsWith(string $haystack, string $needle):bool {
|
||||
|
||||
if (substr($haystack, 0, \strlen($needle)) === $needle) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function checking if a string starts with any input from the input array.
|
||||
*
|
||||
@@ -470,12 +450,9 @@ final class MD_STD {
|
||||
*/
|
||||
public static function checkUrlIsReachable(string $url):bool {
|
||||
|
||||
if (empty($url)) {
|
||||
if (empty($url = MD_STD_IN::sanitize_url($url))) {
|
||||
throw new MDInvalidUrl("Input URL cannot be empty");
|
||||
}
|
||||
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
|
||||
throw new MDInvalidUrl("URL to check (" . $url . ") does not seem to be a valid URL");
|
||||
}
|
||||
|
||||
$ch = self::curl_init($url, 5000);
|
||||
curl_setopt_array($ch, [
|
||||
@@ -869,7 +846,11 @@ final class MD_STD {
|
||||
*/
|
||||
public static function string_to_color_code(string $str):string {
|
||||
|
||||
return \substr(\dechex(\crc32($str)), 0, 6);
|
||||
$output = \dechex(\crc32($str));
|
||||
|
||||
if (\strlen($output) < 6) return '000000';
|
||||
|
||||
return \substr($output, 0, 6);
|
||||
|
||||
}
|
||||
|
||||
@@ -911,10 +892,12 @@ final class MD_STD {
|
||||
|
||||
$max = count($input);
|
||||
$offset = 0;
|
||||
$sizePerEntry = max($size - 1, 1); // Size - 1 is expected, but size below 1 ends in endless loops
|
||||
|
||||
while ($offset < $max) {
|
||||
$cur = array_slice($input, $offset, $size - 1);
|
||||
$cur = array_slice($input, $offset, $sizePerEntry);
|
||||
if (!empty($cur)) $output[] = $cur;
|
||||
$offset += $size - 1;
|
||||
$offset += $sizePerEntry;
|
||||
}
|
||||
|
||||
return $output;
|
||||
@@ -940,10 +923,12 @@ final class MD_STD {
|
||||
|
||||
$max = count($input);
|
||||
$offset = 0;
|
||||
$sizePerEntry = max($size - 1, 1); // Size - 1 is expected, but size below 1 ends in endless loops
|
||||
|
||||
while ($offset < $max) {
|
||||
$cur = array_slice($input, $offset, $size - 1);
|
||||
$cur = array_slice($input, $offset, $sizePerEntry);
|
||||
if (!empty($cur)) $output[] = $cur;
|
||||
$offset += $size - 1;
|
||||
$offset += $sizePerEntry;
|
||||
}
|
||||
|
||||
return $output;
|
||||
|
||||
@@ -351,7 +351,7 @@ final class MD_STD_IN {
|
||||
|
||||
$output = \str_replace(",", ".", $input);
|
||||
if (($output = \filter_var($output, FILTER_VALIDATE_FLOAT)) === false) {
|
||||
throw new MDgenericInvalidInputsException("Input is readable as a floating point value");
|
||||
throw new MDgenericInvalidInputsException("Input is not readable as a floating point value");
|
||||
}
|
||||
return $output;
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ use PHPUnit\Framework\TestCase;
|
||||
* Tests for the manifest.
|
||||
*/
|
||||
final class MD_STD_TEST_PROVIDERS {
|
||||
|
||||
/**
|
||||
* Data provider for returning invalid URLs.
|
||||
*
|
||||
@@ -21,7 +20,7 @@ final class MD_STD_TEST_PROVIDERS {
|
||||
*/
|
||||
public static function invalid_url_provider():array {
|
||||
|
||||
$output = [
|
||||
return [
|
||||
'Space in protocol name' => ["h ttps://www.museum-digital.org"],
|
||||
'Unwanted protocol' => ["telegram://www.museum-digital.org"],
|
||||
'String without protocol' => ["www.museum-digital.org"],
|
||||
@@ -32,8 +31,6 @@ final class MD_STD_TEST_PROVIDERS {
|
||||
'Overly long URL (> 10000 chars)' => ["https://www.museum-digital.org/" . str_repeat('a', 10000)],
|
||||
];
|
||||
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -145,5 +142,4 @@ final class MD_STD_TEST_PROVIDERS {
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user