Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Commit

Permalink
Improve tests, assign types (I miss PHP 8)
Browse files Browse the repository at this point in the history
  • Loading branch information
theresnotime committed Aug 19, 2022
1 parent 0f934c1 commit 5bc8eea
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
23 changes: 15 additions & 8 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

require_once __DIR__ . '/../vendor/autoload.php';

use Exception;

/**
* IPA Validator class
*
Expand Down Expand Up @@ -62,11 +64,11 @@ class Validator {
* @return void
*/
public function __construct( $ipa, $strip = true, $normalize = false, $google = false ) {
$this->originalIPA = $ipa;
$this->normalizedIPA = $ipa;
$this->strip = $strip;
$this->normalize = $normalize;
$this->google = $google;
$this->originalIPA = strval( $ipa );
$this->normalizedIPA = strval( $ipa );
$this->strip = boolval( $strip );
$this->normalize = boolval( $normalize );
$this->google = boolval( $google );
$this->valid = boolval( $this->validate() );
}

Expand All @@ -90,7 +92,7 @@ private function removeDiacritics() {
*
* @return string
*/
private function normalize() {
private function normalizeIPA() {
if ( $this->strip ) {
$this->stripIPA();
}
Expand All @@ -100,6 +102,7 @@ private function normalize() {
* and different from what anyone else will want.
*/
if ( $this->google ) {
/** @var string[] */
$charmap = [
[ '(', '' ],
[ ')', '' ],
Expand All @@ -122,6 +125,7 @@ private function normalize() {
}
$this->removeDiacritics();
} else {
/** @var string[] */
$charmap = [
[ "'", 'ˈ' ],
[ ':', 'ː' ],
Expand Down Expand Up @@ -157,10 +161,13 @@ private function validate() {
}

if ( $this->normalize ) {
$this->normalize();
$this->normalizeIPA();
}

if ( $this->google && !$this->normalize ) {
throw new Exception( 'Google normalization being enabled also requires normalization to also be enabled' );
}

return preg_match( $this->ipaRegex, $this->normalizedIPA );
}

}
25 changes: 24 additions & 1 deletion tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class ValidatorTest extends TestCase {
public function testCanBeCreatedFromIPA(): void {
$this->assertInstanceOf(
Validator::class,
new Validator( '/pʰə̥ˈkj̊uːliɚ/', true, true, true )
new Validator( '/pʰə̥ˈkj̊uːliɚ/' )
);
}

Expand All @@ -32,6 +32,24 @@ public function testNormalization(): void {
);
}

/**
* @covers TheresNoTime\IPAValidator\Validator::validate
*/
public function testException(): void {
$this->expectException( Exception::class );
( new Validator( '/pʰə̥ˈkj̊uːliɚ/', false, false, true ) )->normalizedIPA;
}

/**
* @covers TheresNoTime\IPAValidator\Validator::stripIPA
*/
public function testStripIPA(): void {
$this->assertEquals(
'pʰə̥ˈkj̊uːliɚ',
( new Validator( '/pʰə̥ˈkj̊uːliɚ/', true ) )->normalizedIPA
);
}

/**
* @covers TheresNoTime\IPAValidator\Validator
*/
Expand All @@ -51,6 +69,11 @@ public function testCorpus(): void {
( new Validator( 'pʰə̥ˈkj̊uːliɚ', true, true, true ) )->normalizedIPA
);

$this->assertNotEquals(
'pʰə̥ˈkj̊uːliɚ',
( new Validator( 'pʰə̥ˈkj̊uːliɚ', true, true, true ) )->normalizedIPA
);

$this->assertEquals(
'sotʃiˈmilko',
( new Validator( 'sotʃiˈmilko', true, true, true ) )->normalizedIPA
Expand Down

0 comments on commit 5bc8eea

Please sign in to comment.