Skip to content

Commit

Permalink
Improved intl tests to tolerate ICU data differences
Browse files Browse the repository at this point in the history
- Added new assertion.
- Adjusted tests not to rely on intl version allowing multiple valid variants for Russian and Ukrainian currencies and transliteration.
  • Loading branch information
samdark authored Mar 18, 2017
1 parent a600e16 commit cc44339
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 66 deletions.
41 changes: 41 additions & 0 deletions tests/IsOneOfAssert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace yiiunit;

/**
* IsOneOfAssert asserts that the value is one of the expected values.
*/
class IsOneOfAssert extends \PHPUnit_Framework_Constraint
{
private $allowedValues;

/**
* IsOneOfAssert constructor.
* @param array $allowedValues
*/
public function __construct(array $allowedValues)
{
parent::__construct();
$this->allowedValues = $allowedValues;
}


/**
* Returns a string representation of the object.
*
* @return string
*/
public function toString()
{
$expectedAsString = "'" . implode("', '", $this->allowedValues) . "'";
return "is one of $expectedAsString";
}

/**
* @inheritdoc
*/
protected function matches($other)
{
return in_array($other, $this->allowedValues, false);
}
}
15 changes: 11 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,15 @@ protected function getInaccessibleProperty($object, $propertyName, $revoke = tru
}






/**
* Asserts that value is one of expected values
*
* @param mixed $actual
* @param array $expected
* @param string $message
*/
public function assertIsOneOf($actual, array $expected, $message = '')
{
self::assertThat($actual, new IsOneOfAssert($expected), $message);
}
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@

Yii::setAlias('@yiiunit', __DIR__);

require_once(__DIR__ . '/compatibility.php');
require_once(__DIR__ . '/TestCase.php');
8 changes: 8 additions & 0 deletions tests/compatibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/*
* Ensures compatibility with PHPUnit 6.x
*/

if (!class_exists('PHPUnit_Framework_Constraint') && class_exists('PHPUnit\Framework\Constraint\Constraint')) {
class PHPUnit_Framework_Constraint extends \PHPUnit\Framework\Constraint\Constraint {}
}
69 changes: 30 additions & 39 deletions tests/framework/helpers/InflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace yiiunit\framework\helpers;

use Yii;
use yii\helpers\Inflector;
use yiiunit\TestCase;

Expand Down Expand Up @@ -234,14 +233,6 @@ public function testTransliterateStrict()
}
}

/**
* @return boolean if ICU version is >= 57.1
*/
private function hasNewICUData()
{
return version_compare(INTL_ICU_DATA_VERSION, '57.1', '>=');
}

public function testTransliterateMedium()
{
if (!extension_loaded('intl')) {
Expand All @@ -251,37 +242,37 @@ public function testTransliterateMedium()
// Some test strings are from https://github.com/bergie/midgardmvc_helper_urlize. Thank you, Henri Bergius!
$data = [
// Korean
'해동검도' => 'haedong-geomdo',
'해동검도' => ['haedong-geomdo'],
// Hiragana
'ひらがな' => 'hiragana',
'ひらがな' => ['hiragana'],
// Georgian
'საქართველო' => 'sakartvelo',
'საქართველო' => ['sakartvelo'],
// Arabic
'العربي' => 'alʿrby',
'عرب' => 'ʿrb',
'العربي' => ['alʿrby'],
'عرب' => ['ʿrb'],
// Hebrew
'עִבְרִית' => $this->hasNewICUData() ? '\'iberiyt' : 'ʻiberiyt',
'עִבְרִית' => ['\'iberiyt', 'ʻiberiyt'],
// Turkish
'Sanırım hepimiz aynı şeyi düşünüyoruz.' => 'Sanirim hepimiz ayni seyi dusunuyoruz.',
'Sanırım hepimiz aynı şeyi düşünüyoruz.' => ['Sanirim hepimiz ayni seyi dusunuyoruz.'],

// Russian
'недвижимость' => $this->hasNewICUData() ? 'nedvizimost\'' : 'nedvizimostʹ',
'Контакты' => 'Kontakty',
'недвижимость' => ['nedvizimost\'', 'nedvizimostʹ'],
'Контакты' => ['Kontakty'],

// Ukrainian
'Українська: ґанок, європа' => 'Ukrainsʹka: ganok, evropa',
'Українська: ґанок, європа' => ['Ukrainsʹka: ganok, evropa', 'Ukrains\'ka: ganok, evropa'],

// Serbian
'Српска: ђ, њ, џ!' => 'Srpska: d, n, d!',
'Српска: ђ, њ, џ!' => ['Srpska: d, n, d!'],

// Spanish
'¿Español?' => '¿Espanol?',
'¿Español?' => ['¿Espanol?'],
// Chinese
'美国' => 'mei guo',
'美国' => ['mei guo'],
];

foreach ($data as $source => $expected) {
$this->assertEquals($expected, Inflector::transliterate($source, Inflector::TRANSLITERATE_MEDIUM));
foreach ($data as $source => $allowed) {
$this->assertIsOneOf(Inflector::transliterate($source, Inflector::TRANSLITERATE_MEDIUM), $allowed);
}
}

Expand All @@ -294,37 +285,37 @@ public function testTransliterateLoose()
// Some test strings are from https://github.com/bergie/midgardmvc_helper_urlize. Thank you, Henri Bergius!
$data = [
// Korean
'해동검도' => 'haedong-geomdo',
'해동검도' => ['haedong-geomdo'],
// Hiragana
'ひらがな' => 'hiragana',
'ひらがな' => ['hiragana'],
// Georgian
'საქართველო' => 'sakartvelo',
'საქართველო' => ['sakartvelo'],
// Arabic
'العربي' => 'alrby',
'عرب' => 'rb',
'العربي' => ['alrby'],
'عرب' => ['rb'],
// Hebrew
'עִבְרִית' => $this->hasNewICUData() ? '\'iberiyt' : 'iberiyt',
'עִבְרִית' => ['\'iberiyt', 'iberiyt'],
// Turkish
'Sanırım hepimiz aynı şeyi düşünüyoruz.' => 'Sanirim hepimiz ayni seyi dusunuyoruz.',
'Sanırım hepimiz aynı şeyi düşünüyoruz.' => ['Sanirim hepimiz ayni seyi dusunuyoruz.'],

// Russian
'недвижимость' => $this->hasNewICUData() ? 'nedvizimost\'' : 'nedvizimost',
'Контакты' => 'Kontakty',
'недвижимость' => ['nedvizimost\'', 'nedvizimost'],
'Контакты' => ['Kontakty'],

// Ukrainian
'Українська: ґанок, європа' => 'Ukrainska: ganok, evropa',
'Українська: ґанок, європа' => ['Ukrainska: ganok, evropa', 'Ukrains\'ka: ganok, evropa'],

// Serbian
'Српска: ђ, њ, џ!' => 'Srpska: d, n, d!',
'Српска: ђ, њ, џ!' => ['Srpska: d, n, d!'],

// Spanish
'¿Español?' => 'Espanol?',
'¿Español?' => ['Espanol?'],
// Chinese
'美国' => 'mei guo',
'美国' => ['mei guo'],
];

foreach ($data as $source => $expected) {
$this->assertEquals($expected, Inflector::transliterate($source, Inflector::TRANSLITERATE_LOOSE));
foreach ($data as $source => $allowed) {
$this->assertIsOneOf(Inflector::transliterate($source, Inflector::TRANSLITERATE_LOOSE), $allowed);
}
}

Expand Down
34 changes: 11 additions & 23 deletions tests/framework/i18n/FormatterNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,9 @@ public function testIntlAsCurrency()
// default russian currency symbol
$this->formatter->locale = 'ru-RU';
$this->formatter->currencyCode = null;
if ($this->hasNewICUData()) {
$this->assertSame('123,00 ₽', $this->formatter->asCurrency('123'));
} else {
$this->assertSame('123,00 руб.', $this->formatter->asCurrency('123'));
}
$this->assertIsOneOf($this->formatter->asCurrency('123'), ['123,00 ₽', '123,00 руб.']);
$this->formatter->currencyCode = 'RUB';
$this->assertSame('123,00 руб.', $this->formatter->asCurrency('123'));
$this->assertIsOneOf($this->formatter->asCurrency('123'), ['123,00 ₽', '123,00 руб.']);

// custom currency symbol
$this->formatter->currencyCode = null;
Expand All @@ -279,16 +275,19 @@ public function testIntlAsCurrency()
];
$this->assertSame('123,00 ₽', $this->formatter->asCurrency('123'));
$this->formatter->numberFormatterSymbols = [
NumberFormatter::CURRENCY_SYMBOL => '&#8381;',
NumberFormatter::CURRENCY_SYMBOL => 'RUR',
];
$this->assertSame('123,00 &#8381;', $this->formatter->asCurrency('123'));
$this->assertSame('123,00 RUR', $this->formatter->asCurrency('123'));

/* See https://github.com/yiisoft/yii2/issues/13629
// setting the currency code overrides the symbol
$this->formatter->currencyCode = 'RUB';
$this->assertSame('123,00 руб.', $this->formatter->asCurrency('123'));
$this->assertIsOneOf($this->formatter->asCurrency('123'), ['123,00 ₽', '123,00 руб.']);
$this->formatter->numberFormatterSymbols = [NumberFormatter::CURRENCY_SYMBOL => '₽'];
$this->assertSame('123,00 $', $this->formatter->asCurrency('123', 'USD'));
$this->formatter->numberFormatterSymbols = [NumberFormatter::CURRENCY_SYMBOL => '₽'];
$this->assertSame('123,00 €', $this->formatter->asCurrency('123', 'EUR'));
*/

// custom separators
$this->formatter->locale = 'de-DE';
Expand Down Expand Up @@ -342,19 +341,16 @@ public function testIntlCurrencyFraction()

$this->formatter->locale = 'ru-RU';
$this->formatter->currencyCode = null;
if ($this->hasNewICUData()) {
$this->assertSame('123 ₽', $this->formatter->asCurrency('123'));
} else {
$this->assertSame('123 руб.', $this->formatter->asCurrency('123'));
}
$this->assertIsOneOf($this->formatter->asCurrency('123'), ['123 ₽', '123 руб.']);

$this->formatter->numberFormatterSymbols = [
NumberFormatter::CURRENCY_SYMBOL => '&#8381;',
];
$this->assertSame('123 &#8381;', $this->formatter->asCurrency('123'));

$this->formatter->numberFormatterSymbols = [];
$this->formatter->currencyCode = 'RUB';
$this->assertSame('123 руб.', $this->formatter->asCurrency('123'));
$this->assertIsOneOf($this->formatter->asCurrency('123'), ['123 ₽', '123 руб.']);
}

/**
Expand Down Expand Up @@ -673,12 +669,4 @@ public function testAsSizeConfiguration()
$this->assertSame("1023 bytes", $this->formatter->asSize(1023));
$this->assertSame("1023 B", $this->formatter->asShortSize(1023));
}

/**
* @return boolean if ICU version is >= 57.1
*/
private function hasNewICUData()
{
return version_compare(INTL_ICU_DATA_VERSION, '57.1', '>=');
}
}

0 comments on commit cc44339

Please sign in to comment.