Improve test coverage
This commit is contained in:
@ -7,10 +7,16 @@
|
||||
declare(strict_types = 1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\DataProviderExternal;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
|
||||
/**
|
||||
* Tests for MD_STD_IN.
|
||||
*/
|
||||
#[small]
|
||||
#[CoversClass(\MD_STD_IN::class)]
|
||||
final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Data provider for valid IDs.
|
||||
@ -34,17 +40,112 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing sanitize_id().
|
||||
* Data provider for valid longitudes.
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_id
|
||||
* @dataProvider \MD_STD_IN_Test::valid_id_provider
|
||||
* @return array<array{0: mixed, 1: float}>
|
||||
*/
|
||||
public static function valid_longitude_provider():array {
|
||||
|
||||
$values = [
|
||||
["1", 1.0],
|
||||
["12", 12.0],
|
||||
[12, 12.0],
|
||||
[12.0, 12.0],
|
||||
[95, 95.0],
|
||||
[-95, -95.0],
|
||||
];
|
||||
|
||||
$output = [];
|
||||
foreach ($values as $value) {
|
||||
$output[gettype($value[0]) . ': ' . var_export($value[0], true)] = $value;
|
||||
}
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for invalid longitudes.
|
||||
*
|
||||
* @return array<array{0: mixed, 1: string}>
|
||||
*/
|
||||
public static function invalid_longitude_provider():array {
|
||||
|
||||
$values = [
|
||||
["test", MDgenericInvalidInputsException::class],
|
||||
["test1", MDgenericInvalidInputsException::class],
|
||||
["", MDgenericInvalidInputsException::class],
|
||||
["1900", MDCoordinateOutOfRange::class],
|
||||
[1900, MDCoordinateOutOfRange::class],
|
||||
[-1900, MDCoordinateOutOfRange::class],
|
||||
[185, MDCoordinateOutOfRange::class],
|
||||
[-185, MDCoordinateOutOfRange::class],
|
||||
];
|
||||
|
||||
foreach ($values as $value) {
|
||||
$output[gettype($value[0]) . ': ' . var_export($value[0], true)] = $value;
|
||||
}
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for valid latitudes.
|
||||
*
|
||||
* @return array<array{0: mixed, 1: float}>
|
||||
*/
|
||||
public static function valid_latitude_provider():array {
|
||||
|
||||
$values = [
|
||||
["1", 1.0],
|
||||
["12", 12.0],
|
||||
[12, 12.0],
|
||||
[12.0, 12.0],
|
||||
[85, 85.0],
|
||||
[-85, -85.0],
|
||||
];
|
||||
|
||||
$output = [];
|
||||
foreach ($values as $value) {
|
||||
$output[gettype($value[0]) . ': ' . var_export($value[0], true)] = $value;
|
||||
}
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for invalid latitudes.
|
||||
*
|
||||
* @return array<array{0: mixed, 1: string}>
|
||||
*/
|
||||
public static function invalid_latitude_provider():array {
|
||||
|
||||
$values = [
|
||||
["test", MDgenericInvalidInputsException::class],
|
||||
["test1", MDgenericInvalidInputsException::class],
|
||||
["", MDgenericInvalidInputsException::class],
|
||||
["1900", MDCoordinateOutOfRange::class],
|
||||
[1900, MDCoordinateOutOfRange::class],
|
||||
[-1900, MDCoordinateOutOfRange::class],
|
||||
[95, MDCoordinateOutOfRange::class],
|
||||
[-95, MDCoordinateOutOfRange::class],
|
||||
];
|
||||
|
||||
foreach ($values as $value) {
|
||||
$output[gettype($value[0]) . ': ' . var_export($value[0], true)] = $value;
|
||||
}
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing sanitize_id().
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
* @param integer $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[DataProvider('valid_id_provider')]
|
||||
public function test_sanitize_id_works(mixed $to_validate, int $expected):void {
|
||||
self::assertEquals($expected, MD_STD_IN::sanitize_id($to_validate));
|
||||
|
||||
@ -66,14 +167,11 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing sanitize_id().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_id
|
||||
* @dataProvider \MD_STD_IN_Test::invalid_id_provider
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[DataProvider('invalid_id_provider')]
|
||||
public function test_sanitize_id_fails(mixed $to_validate):void {
|
||||
|
||||
self::expectException(MDpageParameterNotNumericException::class);
|
||||
@ -98,15 +196,12 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing sanitize_id_or_zero().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_id_or_zero
|
||||
* @dataProvider \MD_STD_IN_Test::valid_id_or_zero_provider
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
* @param integer $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[DataProvider('valid_id_or_zero_provider')]
|
||||
public function test_sanitize_id_or_zero_works(mixed $to_validate, int $expected):void {
|
||||
|
||||
self::assertEquals($expected, MD_STD_IN::sanitize_id_or_zero($to_validate));
|
||||
@ -132,14 +227,11 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing sanitize_id_or_zero().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_id_or_zero
|
||||
* @dataProvider \MD_STD_IN_Test::invalid_id_provider_without_zero
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[DataProvider('invalid_id_provider_without_zero')]
|
||||
public function test_sanitize_id_or_zero_fails(mixed $to_validate):void {
|
||||
|
||||
self::expectException(MDpageParameterNotNumericException::class);
|
||||
@ -171,15 +263,12 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing sanitize_text().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_text
|
||||
* @dataProvider \MD_STD_IN_Test::text_with_expected_return_value_provider
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
* @param string $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[DataProvider('text_with_expected_return_value_provider')]
|
||||
public function test_sanitize_text(mixed $to_validate, string $expected):void {
|
||||
|
||||
self::assertEquals($expected, MD_STD_IN::sanitize_text($to_validate));
|
||||
@ -230,15 +319,12 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing sanitize_rgb_color().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_rgb_color
|
||||
* @dataProvider \MD_STD_IN_Test::valid_rgb_colors_provider
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
* @param string $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[DataProvider('valid_rgb_colors_provider')]
|
||||
public function test_sanitize_rgb_color_works(mixed $to_validate, string $expected):void {
|
||||
|
||||
self::assertEquals($expected, MD_STD_IN::sanitize_rgb_color($to_validate));
|
||||
@ -248,14 +334,11 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing sanitize_rgb_color()'s failure modes.
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_rgb_color
|
||||
* @dataProvider \MD_STD_IN_Test::invalid_rgb_colors_provider
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[DataProvider('invalid_rgb_colors_provider')]
|
||||
public function test_sanitize_rgb_color_fails(mixed $to_validate):void {
|
||||
|
||||
self::expectException(MDInvalidColorCode::class);
|
||||
@ -264,10 +347,22 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing sanitize_id_array().
|
||||
* Function for testing sanitize_text_array().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_id_array
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_text_array():void {
|
||||
|
||||
self::assertEquals(["1"], MD_STD_IN::sanitize_text_array([1, '']));
|
||||
self::assertEquals(["1", "2"], MD_STD_IN::sanitize_text_array(["1", 2]));
|
||||
|
||||
self::expectException(MDpageParameterNotNumericException::class);
|
||||
MD_STD_IN::sanitize_id_array([[]]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing sanitize_id_array().
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
@ -294,9 +389,6 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing get_http_input_text().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::get_http_input_text
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_get_http_input_text():void {
|
||||
@ -323,9 +415,6 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing get_http_post_text().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::get_http_post_text
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_get_http_post_text():void {
|
||||
@ -348,9 +437,6 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing sanitize_url().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_url
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_url_with_empty_string():void {
|
||||
@ -363,15 +449,12 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing sanitize_url().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_url
|
||||
* @dataProvider \MD_STD_TEST_PROVIDERS::valid_url_provider
|
||||
*
|
||||
* @param string $to_validate Input to validate.
|
||||
* @param string $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[DataProviderExternal(\MD_STD_TEST_PROVIDERS::class, 'valid_url_provider')]
|
||||
public function test_sanitize_url_works(string $to_validate, string $expected):void {
|
||||
|
||||
self::assertEquals($expected, MD_STD_IN::sanitize_url($to_validate));
|
||||
@ -381,15 +464,11 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing sanitize_url().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_url
|
||||
* @dataProvider \MD_STD_TEST_PROVIDERS::invalid_url_provider
|
||||
*
|
||||
* @param string $to_validate Input to validate.
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[DataProviderExternal(\MD_STD_TEST_PROVIDERS::class, 'invalid_url_provider')]
|
||||
public function test_sanitize_url_fails(string $to_validate):void {
|
||||
|
||||
self::expectException(MDInvalidUrl::class);
|
||||
@ -400,9 +479,6 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing sanitize_email().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_email
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_email_empty():void {
|
||||
@ -414,15 +490,12 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing sanitize_email().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_email
|
||||
* @dataProvider \MD_STD_TEST_PROVIDERS::valid_email_provider
|
||||
*
|
||||
* @param string $to_validate Input to validate.
|
||||
* @param string $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[DataProviderExternal(\MD_STD_TEST_PROVIDERS::class, 'valid_email_provider')]
|
||||
public function test_sanitize_email_works(string $to_validate, string $expected):void {
|
||||
self::assertEquals($expected, MD_STD_IN::sanitize_email($to_validate));
|
||||
|
||||
@ -431,14 +504,11 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing sanitize_email() fails when it should.
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_email
|
||||
* @dataProvider \MD_STD_TEST_PROVIDERS::invalid_email_provider
|
||||
*
|
||||
* @param string $to_validate Input to validate.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[DataProviderExternal(\MD_STD_TEST_PROVIDERS::class, 'invalid_email_provider')]
|
||||
public function test_sanitize_email_fails(string $to_validate):void {
|
||||
|
||||
self::expectException(MDInvalidEmail::class);
|
||||
@ -449,9 +519,6 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing validate_password().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::validate_password
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_validate_password():void {
|
||||
@ -466,9 +533,6 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing validate_phone_number().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::validate_phone_number
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_validate_phone_number():void {
|
||||
@ -488,9 +552,6 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing sanitize_float().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::sanitize_float
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_sanitize_float():void {
|
||||
@ -510,69 +571,68 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing validate_longitude().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::validate_longitude
|
||||
* @param mixed $to_validate Input to validate.
|
||||
* @param float $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_validate_longitude():void {
|
||||
#[DataProvider('valid_longitude_provider')]
|
||||
public function test_validate_longitude_with_valid_entries(mixed $to_validate, float $expected):void {
|
||||
|
||||
self::assertEquals(0, MD_STD_IN::validate_longitude("0"));
|
||||
self::assertEquals(12, MD_STD_IN::validate_longitude("12"));
|
||||
self::assertEquals(12, MD_STD_IN::validate_longitude(12));
|
||||
self::assertEquals(12.12, MD_STD_IN::validate_longitude("12.12"));
|
||||
self::assertEquals(12.12, MD_STD_IN::validate_longitude("12,12"));
|
||||
self::assertEquals(12.12, MD_STD_IN::validate_longitude(12.12));
|
||||
self::assertEquals($expected, MD_STD_IN::validate_longitude($to_validate));
|
||||
|
||||
self::expectException(MDgenericInvalidInputsException::class);
|
||||
MD_STD_IN::validate_longitude("test");
|
||||
self::expectException(MDgenericInvalidInputsException::class);
|
||||
MD_STD_IN::validate_longitude("");
|
||||
self::expectException(MDCoordinateOutOfRange::class);
|
||||
MD_STD_IN::validate_longitude("1900");
|
||||
self::expectException(MDCoordinateOutOfRange::class);
|
||||
MD_STD_IN::validate_longitude(1900);
|
||||
self::expectException(MDCoordinateOutOfRange::class);
|
||||
MD_STD_IN::validate_longitude(-1900);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing validate_longitude().
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
* @param string $exceptionClass Exception class.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[DataProvider('invalid_longitude_provider')]
|
||||
public function test_validate_longitude_with_invalid_entries(mixed $to_validate, string $exceptionClass):void {
|
||||
|
||||
self::expectException($exceptionClass);
|
||||
MD_STD_IN::validate_longitude($to_validate);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing validate_latitude().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::validate_latitude
|
||||
* @param mixed $to_validate Input to validate.
|
||||
* @param float $expected Expected output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_validate_latitude():void {
|
||||
#[DataProvider('valid_latitude_provider')]
|
||||
public function test_validate_latitude_with_valid_entries(mixed $to_validate, float $expected):void {
|
||||
|
||||
self::assertEquals(0, MD_STD_IN::validate_latitude("0"));
|
||||
self::assertEquals(12, MD_STD_IN::validate_latitude("12"));
|
||||
self::assertEquals(12, MD_STD_IN::validate_latitude(12));
|
||||
self::assertEquals(12.12, MD_STD_IN::validate_latitude("12.12"));
|
||||
self::assertEquals(12.12, MD_STD_IN::validate_latitude("12,12"));
|
||||
self::assertEquals(12.12, MD_STD_IN::validate_latitude(12.12));
|
||||
self::assertEquals($expected, MD_STD_IN::validate_latitude($to_validate));
|
||||
|
||||
self::expectException(MDgenericInvalidInputsException::class);
|
||||
MD_STD_IN::validate_latitude("test");
|
||||
self::expectException(MDgenericInvalidInputsException::class);
|
||||
MD_STD_IN::validate_latitude("");
|
||||
self::expectException(MDCoordinateOutOfRange::class);
|
||||
MD_STD_IN::validate_latitude("1900");
|
||||
self::expectException(MDCoordinateOutOfRange::class);
|
||||
MD_STD_IN::validate_latitude(1900);
|
||||
self::expectException(MDCoordinateOutOfRange::class);
|
||||
MD_STD_IN::validate_latitude(-1900);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing validate_latitude().
|
||||
*
|
||||
* @param mixed $to_validate Input to validate.
|
||||
* @param string $exceptionClass Exception class.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[DataProvider('invalid_latitude_provider')]
|
||||
public function test_validate_latitude_with_invalid_entries(mixed $to_validate, string $exceptionClass):void {
|
||||
|
||||
self::expectException($exceptionClass);
|
||||
MD_STD_IN::validate_latitude($to_validate);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for testing validate_isbn().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::validate_isbn
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_validate_isbn():void {
|
||||
@ -596,9 +656,6 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing validate_zip_code().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::validate_zip_code
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_validate_zip_code():void {
|
||||
@ -614,9 +671,6 @@ final class MD_STD_IN_Test extends TestCase {
|
||||
/**
|
||||
* Function for testing ensureStringIsUtf8().
|
||||
*
|
||||
* @small
|
||||
* @covers \MD_STD_IN::ensureStringIsUtf8
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_ensureStringIsUtf8():void {
|
||||
|
Reference in New Issue
Block a user