Improve test coverage

This commit is contained in:
2024-07-11 14:53:05 +02:00
parent fb008e1b59
commit 11faeaa7e7
8 changed files with 944 additions and 156 deletions

View File

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