PHP Classes

File: tests/TransliterateTest.php

Recommend this page to a friend!
  Classes of Lars Moelleken   portable ascii   tests/TransliterateTest.php   Download  
File: tests/TransliterateTest.php
Role: Class source
Content type: text/plain
Description: Class source
Class: portable ascii
Manipulate text strings without special extensions
Author: By
Last change:
Date: 4 years ago
Size: 7,831 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);

namespace
voku\tests;

use
voku\helper\ASCII;
use
voku\helper\ASCII as u;

/**
 * @internal
 */
final class TransliterateTest extends \PHPUnit\Framework\TestCase
{
    public function
testUtf8()
    {
       
$str = 'testiñg';
        static::
assertSame('testing', u::to_transliterate($str));
    }

    public function
testAscii()
    {
       
$str = 'testing';
        static::
assertSame('testing', u::to_transliterate($str));
    }

    public function
testInvalidChar()
    {
       
$str = "tes\xE9ting";
        static::
assertSame('testing', u::to_transliterate($str));
    }

    public function
testEmptyStr()
    {
       
$str = '';
        static::
assertEmpty(u::to_transliterate($str));
    }

    public function
testNulAndNon7Bit()
    {
       
$str = "a\x00ñ\x00c";
        static::
assertSame('anc', u::to_transliterate($str));
    }

    public function
testNul()
    {
       
$str = "a\x00b\x00c";
        static::
assertSame('abc', u::to_transliterate($str));
    }

    public function
testToASCII()
    {
       
$testsStrict = [];
        if (\
extension_loaded('intl') === true) {

           
// ---

           
$testString = \file_get_contents(__DIR__ . '/fixtures/sample-unicode-chart.txt');
           
$resultString = \file_get_contents(__DIR__ . '/fixtures/sample-ascii-chart.txt');

            static::
assertSame($resultString, ASCII::to_transliterate($testString, '?', true));

           
// ---

           
$testsStrict = [
               
// 1 => '1',
                // -1 => '-1',
               
' ' => ' ',
               
// '' => '',
               
'???' => 'abz',
               
"\xe2\x80\x99" => '\'',
               
'?test' => 'Btest',
               
' -ABC-????- ' => ' -ABC-zhong wen kong bai- ',
               
" - abc- \xc2\x87" => ' - abc- ++',
               
'abc' => 'abc',
               
'deja vu' => 'deja vu',
               
'déjà vu' => 'deja vu',
               
'déjà ??? i?ii' => 'deja sss iiii',
               
"test\x80-\xBFöäü" => 'test-oau',
               
'Internationalizaetion' => 'Internationalizaetion',
               
"? - &#20013; - %&? - \xc2\x80" => 'zhong - &#20013; - %&? - EUR',
               
'Un été brûlant sur la côte' => 'Un ete brulant sur la cote',
               
'???? ????? ??? ??????' => 'Aute einai mia dokime',
               
'????' => 'ahbk',
               
'?????' => 'kyanpasu',
               
'?????????????' => 'biologiceskom',
               
'?, ??' => 'jeong, byeongho',
               
'???, ????' => 'masuda, yoshihiko',
               
'?????' => 'monica',
               
'????' => 'kasadb',
               
'???? ?' => 'ahbk ?',
               
'???????????????? 5.99?' => 'dhrzsshsdtz\'gh[?][?][?][?][?] 5.99EUR',
               
'???????????????? £5.99' => 'dhrzsshsdtz\'gh[?][?][?][?][?] PS5.99',
               
'????????????????? $5.99' => '[?]\'bgdhwzhtykklmmn $5.99',
               
'??????????????? ¥5990' => 'ri yi guo hui ren nian da shi er ben zhong zhang chu san tong Y=5990',
               
'5.99? ???????? $5.99' => '5.99EUR ri yi guo hui ren nian da shi $5.99',
               
'????@??????.com' => 'bgdh@dtz\'gh[?].com',
               
'???@??????' => 'nian da shi@dtz\'gh[?]',
               
'???? & ???' => 'bgdh & nian da shi',
               
'?&? at ??????.???' => 'guo&m at dtz\'gh[?].hwz',
               
'my username is @????' => 'my username is @bgdh',
               
'The review gave 5* to ????' => 'The review gave 5* to z\'gh[?]',
               
'use ???@??????.??? to get a 10% discount' => 'use nian da shi@dtz\'gh[?].hwz to get a 10% discount',
               
'? = ??^2' => 'ri = ht^2',
               
'???? ?? ???? 9.81 m/s2' => 'kklm guo hui gh[?][?][?] 9.81 m/s2',
               
'The #? comment at @???? = 10% of *&*' => 'The #hui comment at @bgdh = 10% of *&*',
               
'? i ? ?' => '[?] i [?] N',
               
'? ? ? ? ? ? ? ?????' => '? ? ? ? ? ? ahbk',
            ];
        }

       
$tests = [
           
// 1 => '1',
            // -1 => '-1',
           
' ' => ' ',
           
// '' => '',
           
'???' => 'abz',
           
"\xe2\x80\x99" => '\'',
           
'?test' => 'Btest',
           
' -ABC-????- ' => ' -ABC-Zhong Wen Kong Bai - ',
           
" - abc- \xc2\x87" => ' - abc- ++',
           
'abc' => 'abc',
           
'deja vu' => 'deja vu',
           
'déjà vu?' => 'deja vu ',
           
'déjà ??? i?ii' => 'deja sss iiii',
           
'?????' => 'kosme',
           
"test\x80-\xBFöäü" => 'test-oau',
           
'Internationalizaetion' => 'Internationalizaetion',
           
"? - &#20013; - %&? - \xc2\x80" => 'Zhong - &#20013; - %&? - EUR',
           
'Un été brûlant sur la côte' => 'Un ete brulant sur la cote',
           
'???? ????? ??? ??????' => 'Aute einai mia dokime',
           
'????' => 'aHbk',
           
'?????' => 'kiyanpasu',
           
'?????????????' => 'biologicheskom',
           
'?, ??' => 'jeong, byeongho',
           
'???, ????' => 'masuda, yosihiko',
           
'?????' => 'monic',
           
'????' => 'kssdb',
           
'???? ?' => 'aHbk ?',
           
'? i ? ?' => '[?] i [?] N',
           
'? ? ? ? ? ? ? ?????' => '? ? ? ? ? ? aHbk',
        ];

        for (
$i = 0; $i <= 2; ++$i) { // keep this loop for simple performance tests
           
foreach ($tests as $before => $after) {
                static::
assertSame($after, ASCII::to_transliterate($before), 'tested: ' . $before);
            }
        }

        foreach (
$testsStrict as $before => $after) {
            static::
assertSame($after, ASCII::to_transliterate($before, '?', true), 'tested: ' . $before);
        }
    }
}