Add function for validating ZIP codes (somewhat)

This commit is contained in:
Joshua Ramon Enslin 2023-11-08 02:18:34 +01:00
parent d83ed2d0eb
commit a03f072a69
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
4 changed files with 56 additions and 28 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/.phpunit.cache/

View File

@ -1,30 +1,14 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false" backupStaticAttributes="false"
beStrictAboutChangesToGlobalState="true" beStrictAboutOutputDuringTests="true" beStrictAboutResourceUsageDuringSmallTests="true" beStrictAboutTodoAnnotatedTests="true" beStrictAboutCoversAnnotation="false"
bootstrap="tests/bootstrap.php"
cacheResultFile=".phpunit.cache/test-results"
cacheResult="false"
colors="true"
convertErrorsToExceptions="true" convertDeprecationsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true"
enforceTimeLimit="true"
executionOrder="depends,defects"
failOnRisky="true"
failOnWarning="true"
forceCoversAnnotation="false"
processIsolation="true"
stopOnError="true" stopOnFailure="true" stopOnIncomplete="true" stopOnSkipped="true" stopOnRisky="true"
testdox="true"
timeoutForSmallTests="1" timeoutForMediumTests="10" timeoutForLargeTests="60"
verbose="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" backupGlobals="false" beStrictAboutChangesToGlobalState="true" beStrictAboutOutputDuringTests="true" bootstrap="tests/bootstrap.php" cacheResult="false" colors="true" enforceTimeLimit="true" executionOrder="depends,defects" failOnRisky="true" failOnWarning="true" processIsolation="true" stopOnError="true" stopOnFailure="true" stopOnIncomplete="true" stopOnSkipped="true" stopOnRisky="true" testdox="true" timeoutForSmallTests="1" timeoutForMediumTests="10" timeoutForLargeTests="60" cacheDirectory=".phpunit.cache" backupStaticProperties="false" requireCoverageMetadata="false" beStrictAboutCoverageMetadata="false">
<testsuites>
<testsuite name="tests">
<directory>tests/</directory>
</testsuite>
</testsuites>
<coverage>
<coverage/>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</source>
</phpunit>

View File

@ -392,6 +392,27 @@ final class MD_STD_IN {
}
/**
* Validates a ZIP code.
*
* @param string $input Input string.
*
* @return string
*/
public static function validate_zip_code(string $input):string {
if (($input = trim($input)) === "") {
return "";
}
if (\mb_strlen($input) > 7) {
throw new MDgenericInvalidInputsException("ZIP code is too long");
}
return $input;
}
/**
* Returns an UTF8 version of a string.
*

View File

@ -318,6 +318,8 @@ final class MD_STD_IN_Test extends TestCase {
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);
@ -346,6 +348,8 @@ final class MD_STD_IN_Test extends TestCase {
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);
@ -378,6 +382,24 @@ 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 {
self::assertEquals("", MD_STD_IN::validate_zip_code(""));
self::assertEquals("1234", MD_STD_IN::validate_zip_code(" 1234"));
self::expectException(MDgenericInvalidInputsException::class);
MD_STD_IN::validate_zip_code("X094339604");
}
/**
* Function for testing ensureStringIsUtf8().
*