Add a class NodaNameSplitterTest, for now splitting names into given

name and family name
This commit is contained in:
Joshua Ramon Enslin 2021-05-04 23:04:41 +02:00
parent 1dd05a3822
commit bde3c2cb9e
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
4 changed files with 130 additions and 14 deletions

84
src/NodaNameSplitter.php Normal file
View File

@ -0,0 +1,84 @@
<?PHP
/**
* Class containing static functions for splitting an actor name into given name and family name.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
/**
* Class for splitting actor names.
*/
final class NodaNameSplitter {
const LANGS_FAMILY_NAME_FIRST = [
'hu'
];
/** @var string */
private string $_lang;
/**
* Returns assumed given name and family name based on an input string.
*
* @param string $fullName Input string.
*
* @return array{family_name: string, given_name: string};
*/
public function givenNameFamilyNameFromString(string $fullName):array {
$words = array_diff(explode(' ', $fullName), ['']);
if (count($words) <= 1) {
return [
'family_name' => '',
'given_name' => $fullName,
];
}
// Languages, which have names starting with the family name, need to have a special
// way of splitting up the full name as follows.
if (in_array($this->_lang, self::LANGS_FAMILY_NAME_FIRST, true)) {
$familyName = array_shift($words);
$givenName = implode(' ', $words);
return [
'family_name' => $familyName,
'given_name' => $givenName,
];
}
// If the first "word" / name part of the name ends with a comma, it is assumed to be
// a family name.
if (substr($words[0], -1) === ',') {
$familyName = substr(array_shift($words), 0, -1);
$givenName = implode(' ', $words);
return [
'family_name' => $familyName,
'given_name' => $givenName,
];
}
$familyName = array_pop($words);
$givenName = implode(' ', $words);
return [
'family_name' => $familyName,
'given_name' => $givenName,
];
}
/**
* Constructor.
*
* @param string $lang The user's language.
*
* @return void
*/
public function __construct(string $lang) {
$this->_lang = $lang;
}
}

View File

@ -0,0 +1,46 @@
<?PHP
/**
* This script contains tests for the home page.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
require_once __DIR__ . "/../src/NodaNameSplitter.php";
require_once __DIR__ . "/../../MD_STD/src/MD_STD.php";
/**
* Tests for home page.
*/
final class NodaNameSplitterTest extends TestCase {
/**
* Test to check whether the HTML page is correctly generated.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
* @group ValidOutput
*
* @return void
*/
public function testSplitGivenNameFamilyNameFromString():void {
$splitter = new NodaNameSplitter('de');
// This applied to all languages
self::assertEquals($splitter->givenNameFamilyNameFromString('Wilhelm'), ['family_name' => '', 'given_name' => 'Wilhelm']);
self::assertEquals($splitter->givenNameFamilyNameFromString('Friedrich Wilhelm'), ['family_name' => 'Wilhelm', 'given_name' => 'Friedrich']);
self::assertEquals($splitter->givenNameFamilyNameFromString('Wilhelm, Friedrich '), ['family_name' => 'Wilhelm', 'given_name' => 'Friedrich']);
self::assertEquals($splitter->givenNameFamilyNameFromString('Therese von Bacheracht'), ['family_name' => 'Bacheracht', 'given_name' => 'Therese von']);
self::assertEquals($splitter->givenNameFamilyNameFromString('Bacheracht, Therese von'), ['family_name' => 'Bacheracht', 'given_name' => 'Therese von']);
self::assertEquals($splitter->givenNameFamilyNameFromString('von Bacheracht, Therese '), ['family_name' => 'Therese', 'given_name' => 'von Bacheracht,']);
self::assertEquals($splitter->givenNameFamilyNameFromString('Filomeno V. Aguilar, Jr.'), ['family_name' => 'Jr.', 'given_name' => 'Filomeno V. Aguilar,']);
$splitter = new NodaNameSplitter('hu');
self::assertEquals($splitter->givenNameFamilyNameFromString('Friedrich Wilhelm'), ['family_name' => 'Friedrich', 'given_name' => 'Wilhelm']);
self::assertEquals($splitter->givenNameFamilyNameFromString('Wilhelm, Friedrich '), ['family_name' => 'Wilhelm,', 'given_name' => 'Friedrich']);
self::assertEquals($splitter->givenNameFamilyNameFromString('Therese von Bacheracht'), ['family_name' => 'Therese', 'given_name' => 'von Bacheracht']);
self::assertEquals($splitter->givenNameFamilyNameFromString('Bacheracht, Therese von'), ['family_name' => 'Bacheracht,', 'given_name' => 'Therese von']);
self::assertEquals($splitter->givenNameFamilyNameFromString('von Bacheracht, Therese '), ['family_name' => 'von', 'given_name' => 'Bacheracht, Therese']);
self::assertEquals($splitter->givenNameFamilyNameFromString('Filomeno V. Aguilar, Jr.'), ['family_name' => 'Filomeno', 'given_name' => 'V. Aguilar, Jr.']);
}
}

View File

@ -13,7 +13,6 @@ require_once __DIR__ . "/../../MD_STD/src/MD_STD.php";
* Tests for home page. * Tests for home page.
*/ */
final class NodaTimeSplitterTest extends TestCase { final class NodaTimeSplitterTest extends TestCase {
/** /**
* Test to check whether the HTML page is correctly generated. * Test to check whether the HTML page is correctly generated.
* *
@ -525,5 +524,4 @@ final class NodaTimeSplitterTest extends TestCase {
# self::assertEquals($output, []); # self::assertEquals($output, []);
} }
} }

View File

@ -9,14 +9,11 @@
beStrictAboutTodoAnnotatedTests="true" beStrictAboutTodoAnnotatedTests="true"
beStrictAboutCoversAnnotation="false" beStrictAboutCoversAnnotation="false"
cacheResult="false" cacheResult="false"
cacheTokens="false"
colors="true" colors="true"
convertErrorsToExceptions="true" convertErrorsToExceptions="true"
convertDeprecationsToExceptions="true" convertDeprecationsToExceptions="true"
convertNoticesToExceptions="true" convertNoticesToExceptions="true"
convertWarningsToExceptions="true" convertWarningsToExceptions="true"
preserveGlobalState="false"
runTestInSeparateProcess="true"
enforceTimeLimit="true" enforceTimeLimit="true"
failOnWarning="true" failOnWarning="true"
forceCoversAnnotation="false" forceCoversAnnotation="false"
@ -31,14 +28,5 @@
timeoutForMediumTests="10" timeoutForMediumTests="10"
timeoutForLargeTests="60" timeoutForLargeTests="60"
verbose="true"> verbose="true">
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<!-- <file>/path/to/file</file>
<exclude>
<directory suffix=".php">/path/to/files</directory>
<file>/path/to/file</file>
</exclude>-->
</whitelist>
</filter>
</phpunit> </phpunit>