diff options
author | Stefan Parviainen <saparvia@caterva.eu> | 2014-12-30 19:57:12 +0100 |
---|---|---|
committer | Stefan Parviainen <saparvia@caterva.eu> | 2014-12-30 20:29:31 +0100 |
commit | 9cab8ae58a29ecf7387e6865aa170715caeabf04 (patch) | |
tree | 97c4791763ecb7c877c13b562a0ad3b80857b9d7 /library/intl/tests | |
parent | 8e034a3b6b67a9aaa20fe9db671350e198fe7c42 (diff) | |
download | volse-hubzilla-9cab8ae58a29ecf7387e6865aa170715caeabf04.tar.gz volse-hubzilla-9cab8ae58a29ecf7387e6865aa170715caeabf04.tar.bz2 volse-hubzilla-9cab8ae58a29ecf7387e6865aa170715caeabf04.zip |
Language names via intl library. Fixes #773
Diffstat (limited to 'library/intl/tests')
-rw-r--r-- | library/intl/tests/Country/CountryRepositoryTest.php | 114 | ||||
-rw-r--r-- | library/intl/tests/Country/CountryTest.php | 83 | ||||
-rw-r--r-- | library/intl/tests/Currency/CurrencyRepositoryTest.php | 113 | ||||
-rw-r--r-- | library/intl/tests/Currency/CurrencyTest.php | 83 | ||||
-rw-r--r-- | library/intl/tests/DummyRepository.php | 23 | ||||
-rw-r--r-- | library/intl/tests/Formatter/NumberFormatterTest.php | 378 | ||||
-rw-r--r-- | library/intl/tests/Language/LanguageRepositoryTest.php | 92 | ||||
-rw-r--r-- | library/intl/tests/Language/LanguageTest.php | 53 | ||||
-rw-r--r-- | library/intl/tests/LocaleResolverTest.php | 51 | ||||
-rw-r--r-- | library/intl/tests/NumberFormat/NumberFormatRepositoryTest.php | 69 | ||||
-rw-r--r-- | library/intl/tests/NumberFormat/NumberFormatTest.php | 131 |
11 files changed, 1190 insertions, 0 deletions
diff --git a/library/intl/tests/Country/CountryRepositoryTest.php b/library/intl/tests/Country/CountryRepositoryTest.php new file mode 100644 index 000000000..23e1509d8 --- /dev/null +++ b/library/intl/tests/Country/CountryRepositoryTest.php @@ -0,0 +1,114 @@ +<?php + +namespace CommerceGuys\Intl\Tests\Country; + +use CommerceGuys\Intl\Country\CountryRepository; +use org\bovigo\vfs\vfsStream; + +/** + * @coversDefaultClass \CommerceGuys\Intl\Country\CountryRepository + */ +class CountryRepositoryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Base country definitions. + * + * @var array + */ + protected $baseDefinitions = array( + 'FR' => array( + 'code' => 'FR', + 'three_letter_code' => 'FRA', + 'numeric_code' => '250', + 'telephone_code' => '33', + ), + 'US' => array( + 'code' => 'US', + 'three_letter_code' => 'USA', + 'numeric_code' => '840', + 'telephone_code' => '1', + ), + ); + + /** + * English country definitions. + * + * @var array + */ + protected $englishDefinitions = array( + 'FR' => array( + 'name' => 'France', + ), + 'US' => array( + 'name' => 'United States', + ), + ); + + /** + * @covers ::__construct + */ + public function testConstructor() + { + // Mock the existence of JSON definitions on the filesystem. + $root = vfsStream::setup('resources'); + vfsStream::newFile('country/base.json')->at($root)->setContent(json_encode($this->baseDefinitions)); + vfsStream::newFile('country/en.json')->at($root)->setContent(json_encode($this->englishDefinitions)); + + // Instantiate the country repository and confirm that the definition path + // was properly set. + $countryRepository = new CountryRepository('vfs://resources/country/'); + $definitionPath = $this->getObjectAttribute($countryRepository, 'definitionPath'); + $this->assertEquals('vfs://resources/country/', $definitionPath); + + return $countryRepository; + } + + /** + * @covers ::get + * @covers ::loadDefinitions + * @covers ::createCountryFromDefinition + * @uses \CommerceGuys\Intl\Country\Country + * @uses \CommerceGuys\Intl\LocaleResolverTrait + * @depends testConstructor + */ + public function testGet($countryRepository) + { + $country = $countryRepository->get('FR'); + $this->assertInstanceOf('CommerceGuys\\Intl\\Country\\Country', $country); + $this->assertEquals('FR', $country->getCountryCode()); + $this->assertEquals('France', $country->getName()); + $this->assertEquals('FRA', $country->getThreeLetterCode()); + $this->assertEquals('250', $country->getNumericCode()); + $this->assertEquals('33', $country->getTelephoneCode()); + $this->assertEquals('en', $country->getLocale()); + } + + /** + * @covers ::get + * @covers ::loadDefinitions + * @uses \CommerceGuys\Intl\LocaleResolverTrait + * @expectedException \CommerceGuys\Intl\Exception\UnknownCountryException + * @depends testConstructor + */ + public function testGetInvalidCountry($countryRepository) + { + $countryRepository->get('DE'); + } + + /** + * @covers ::getAll + * @covers ::loadDefinitions + * @covers ::createCountryFromDefinition + * @uses \CommerceGuys\Intl\Country\Country + * @uses \CommerceGuys\Intl\LocaleResolverTrait + * @depends testConstructor + */ + public function testGetAll($countryRepository) + { + $countries = $countryRepository->getAll(); + $this->assertArrayHasKey('FR', $countries); + $this->assertArrayHasKey('US', $countries); + $this->assertEquals('FR', $countries['FR']->getCountryCode()); + $this->assertEquals('US', $countries['US']->getCountryCode()); + } +} diff --git a/library/intl/tests/Country/CountryTest.php b/library/intl/tests/Country/CountryTest.php new file mode 100644 index 000000000..a0b2dc116 --- /dev/null +++ b/library/intl/tests/Country/CountryTest.php @@ -0,0 +1,83 @@ +<?php + +namespace CommerceGuys\Intl\Tests\Country; + +use CommerceGuys\Intl\Country\Country; + +/** + * @coversDefaultClass \CommerceGuys\Intl\Country\Country + */ +class CountryTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Country + */ + protected $country; + + public function setUp() + { + $this->country = new Country(); + } + + /** + * @covers ::getCountryCode + * @covers ::setCountryCode + * @covers ::__toString + */ + public function testCountryCode() + { + $this->country->setCountryCode('US'); + $this->assertEquals('US', $this->country->getCountryCode()); + $this->assertEquals('US', (string) $this->country); + } + + /** + * @covers ::getName + * @covers ::setName + */ + public function testName() + { + $this->country->setName('United States'); + $this->assertEquals('United States', $this->country->getName()); + } + + /** + * @covers ::getThreeLetterCode + * @covers ::setThreeLetterCode + */ + public function testThreeLetterCode() + { + $this->country->setThreeLetterCode('USA'); + $this->assertEquals('USA', $this->country->getThreeLetterCode()); + } + + /** + * @covers ::getNumericCode + * @covers ::setNumericCode + */ + public function testNumericCode() + { + $this->country->setNumericCode('840'); + $this->assertEquals('840', $this->country->getNumericCode()); + } + + /** + * @covers ::getTelephoneCode + * @covers ::setTelephoneCode + */ + public function testTelephoneCode() + { + $this->country->setTelephoneCode('1'); + $this->assertEquals('1', $this->country->getTelephoneCode()); + } + + /** + * @covers ::getLocale + * @covers ::setLocale + */ + public function testLocale() + { + $this->country->setLocale('en'); + $this->assertEquals('en', $this->country->getLocale()); + } +} diff --git a/library/intl/tests/Currency/CurrencyRepositoryTest.php b/library/intl/tests/Currency/CurrencyRepositoryTest.php new file mode 100644 index 000000000..4270dc70f --- /dev/null +++ b/library/intl/tests/Currency/CurrencyRepositoryTest.php @@ -0,0 +1,113 @@ +<?php + +namespace CommerceGuys\Intl\Tests\Currency; + +use CommerceGuys\Intl\Currency\CurrencyRepository; +use org\bovigo\vfs\vfsStream; + +/** + * @coversDefaultClass \CommerceGuys\Intl\Currency\CurrencyRepository + */ +class CurrencyRepositoryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Base currency definitions. + * + * @var array + */ + protected $baseDefinitions = array( + 'USD' => array( + 'code' => 'USD', + 'numeric_code' => '840', + ), + 'EUR' => array( + 'code' => 'EUR', + 'numeric_code' => '840', + 'fraction_digits' => '2', + ), + ); + + /** + * English currency definitions. + * + * @var array + */ + protected $englishDefinitions = array( + 'USD' => array( + 'name' => 'US Dollar', + 'symbol' => '$', + ), + 'EUR' => array( + 'name' => 'Euro', + 'symbol' => '€', + ), + ); + + /** + * @covers ::__construct + */ + public function testConstructor() + { + // Mock the existence of JSON definitions on the filesystem. + $root = vfsStream::setup('resources'); + vfsStream::newFile('currency/base.json')->at($root)->setContent(json_encode($this->baseDefinitions)); + vfsStream::newFile('currency/en.json')->at($root)->setContent(json_encode($this->englishDefinitions)); + + // Instantiate the currency repository and confirm that the definition path + // was properly set. + $currencyRepository = new CurrencyRepository('vfs://resources/currency/'); + $definitionPath = $this->getObjectAttribute($currencyRepository, 'definitionPath'); + $this->assertEquals('vfs://resources/currency/', $definitionPath); + + return $currencyRepository; + } + + /** + * @covers ::get + * @covers ::loadDefinitions + * @covers ::createCurrencyFromDefinition + * @uses \CommerceGuys\Intl\Currency\Currency + * @uses \CommerceGuys\Intl\LocaleResolverTrait + * @depends testConstructor + */ + public function testGet($currencyRepository) + { + $currency = $currencyRepository->get('USD'); + $this->assertInstanceOf('CommerceGuys\\Intl\\Currency\\Currency', $currency); + $this->assertEquals('USD', $currency->getCurrencyCode()); + $this->assertEquals('US Dollar', $currency->getName()); + $this->assertEquals('840', $currency->getNumericCode()); + $this->assertEquals('2', $currency->getFractionDigits()); + $this->assertEquals('$', $currency->getSymbol()); + $this->assertEquals('en', $currency->getLocale()); + } + + /** + * @covers ::get + * @covers ::loadDefinitions + * @uses \CommerceGuys\Intl\LocaleResolverTrait + * @expectedException \CommerceGuys\Intl\Exception\UnknownCurrencyException + * @depends testConstructor + */ + public function testGetInvalidCurrency($currencyRepository) + { + $currencyRepository->get('RSD'); + } + + /** + * @covers ::getAll + * @covers ::loadDefinitions + * @covers ::createCurrencyFromDefinition + * @uses \CommerceGuys\Intl\Currency\Currency + * @uses \CommerceGuys\Intl\LocaleResolverTrait + * @depends testConstructor + */ + public function testGetAll($currencyRepository) + { + $currencies = $currencyRepository->getAll(); + $this->assertArrayHasKey('USD', $currencies); + $this->assertArrayHasKey('EUR', $currencies); + $this->assertEquals('USD', $currencies['USD']->getCurrencyCode()); + $this->assertEquals('EUR', $currencies['EUR']->getCurrencyCode()); + } +} diff --git a/library/intl/tests/Currency/CurrencyTest.php b/library/intl/tests/Currency/CurrencyTest.php new file mode 100644 index 000000000..837e4d07a --- /dev/null +++ b/library/intl/tests/Currency/CurrencyTest.php @@ -0,0 +1,83 @@ +<?php + +namespace CommerceGuys\Intl\Tests\Currency; + +use CommerceGuys\Intl\Currency\Currency; + +/** + * @coversDefaultClass \CommerceGuys\Intl\Currency\Currency + */ +class CurrencyTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Currency + */ + protected $currency; + + public function setUp() + { + $this->currency = new Currency(); + } + + /** + * @covers ::getCurrencyCode + * @covers ::setCurrencyCode + * @covers ::__toString + */ + public function testCurrencyCode() + { + $this->currency->setCurrencyCode('USD'); + $this->assertEquals('USD', $this->currency->getCurrencyCode()); + $this->assertEquals('USD', (string) $this->currency); + } + + /** + * @covers ::getName + * @covers ::setName + */ + public function testName() + { + $this->currency->setName('US Dollar'); + $this->assertEquals('US Dollar', $this->currency->getName()); + } + + /** + * @covers ::getNumericCode + * @covers ::setNumericCode + */ + public function testNumericCode() + { + $this->currency->setNumericCode('840'); + $this->assertEquals('840', $this->currency->getNumericCode()); + } + + /** + * @covers ::getFractionDigits + * @covers ::setFractionDigits + */ + public function testFractionDigits() + { + $this->currency->setFractionDigits('2'); + $this->assertEquals('2', $this->currency->getFractionDigits()); + } + + /** + * @covers ::getSymbol + * @covers ::setSymbol + */ + public function testSymbol() + { + $this->currency->setSymbol('$'); + $this->assertEquals('$', $this->currency->getSymbol()); + } + + /** + * @covers ::getLocale + * @covers ::setLocale + */ + public function testLocale() + { + $this->currency->setLocale('en'); + $this->assertEquals('en', $this->currency->getLocale()); + } +} diff --git a/library/intl/tests/DummyRepository.php b/library/intl/tests/DummyRepository.php new file mode 100644 index 000000000..0d9ca760b --- /dev/null +++ b/library/intl/tests/DummyRepository.php @@ -0,0 +1,23 @@ +<?php + +namespace CommerceGuys\Intl\Tests; + +use CommerceGuys\Intl\LocaleResolverTrait; + +/** + * Dummy repository used for testing the LocaleResolverTrait. + */ +class DummyRepository +{ + use LocaleResolverTrait; + + public function __construct() + { + $this->definitionPath = 'vfs://resources/dummy/'; + } + + public function runResolveLocale($locale, $fallbackLocale = null) + { + return $this->resolveLocale($locale, $fallbackLocale); + } +} diff --git a/library/intl/tests/Formatter/NumberFormatterTest.php b/library/intl/tests/Formatter/NumberFormatterTest.php new file mode 100644 index 000000000..83574873b --- /dev/null +++ b/library/intl/tests/Formatter/NumberFormatterTest.php @@ -0,0 +1,378 @@ +<?php + +namespace CommerceGuys\Intl\Tests\Formatter; + +use CommerceGuys\Intl\Currency\Currency; +use CommerceGuys\Intl\Formatter\NumberFormatter; +use CommerceGuys\Intl\NumberFormat\NumberFormat; + +/** + * @coversDefaultClass \CommerceGuys\Intl\Formatter\NumberFormatter + */ +class NumberFormatterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Prepare two number formats. + */ + protected $numberFormats = array( + 'latn' => array( + 'numbering_system' => 'latn', + 'decimal_pattern' => '#,##0.###', + 'percent_pattern' => '#,##0%', + 'currency_pattern' => '¤#,##0.00', + 'accounting_currency_pattern' => '¤#,##0.00;(¤#,##0.00)', + ), + 'beng' => array( + 'numbering_system' => 'beng', + 'decimal_pattern' => '#,##,##0.###', + 'percent_pattern' => '#,##,##0%', + 'currency_pattern' => '#,##,##0.00¤', + 'accounting_currency_pattern' => '#,##,##0.00¤;(#,##,##0.00¤)', + ), + ); + + /** + * Prepare two currency formats. + */ + protected $currencies = array( + 'USD' => array( + 'code' => 'USD', + 'name' => 'US Dollar', + 'numeric_code' => '840', + 'symbol' => '$', + ), + 'BND' => array( + 'code' => 'BND', + 'name' => 'dollar Brunei', + 'numeric_code' => '096', + 'symbol' => 'BND', + ), + ); + + /** + * @covers ::__construct + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::getNumberFormat + * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat + */ + public function testConstructor() + { + $numberFormat = new NumberFormat(); + $formatter = new NumberFormatter($numberFormat, NumberFormatter::DECIMAL); + $this->assertSame($numberFormat, $formatter->getNumberFormat()); + } + + /** + * @covers ::__construct + * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat + * + * @expectedException \CommerceGuys\Intl\Exception\InvalidArgumentException + * @expectedExceptionMessage Unknown format style provided to NumberFormatter::__construct(). + */ + public function testConstructorWithInvalidStyle() + { + $numberFormat = new NumberFormat(); + new NumberFormatter($numberFormat, 'foo'); + } + + /** + * @covers ::format + * @covers ::replaceDigits + * @covers ::replaceSymbols + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::__construct + * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat + * + * @dataProvider numberValueProvider + */ + public function testFormat($number_format, $style, $value, $expected_value) + { + $formatter = new NumberFormatter($number_format, $style); + + $formattedNumber = $formatter->format($value); + $this->assertSame($expected_value, $formattedNumber); + } + + /** + * @covers ::SetMinimumFractionDigits + * @covers ::SetMaximumFractionDigits + * @covers ::format + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::__construct + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::replaceDigits + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::replaceSymbols + * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat + */ + public function testFormatFractionDigits() + { + $numberFormat = $this->createNumberFormat($this->numberFormats['latn']); + + $formatter = new NumberFormatter($numberFormat); + $formatter->setMinimumFractionDigits(2); + $formattedNumber = $formatter->format('12.5'); + $this->assertSame('12.50', $formattedNumber); + + $formatter = new NumberFormatter($numberFormat); + $formatter->setMaximumFractionDigits(1); + $formattedNumber = $formatter->format('12.50'); + $this->assertSame('12.5', $formattedNumber); + + $formatter = new NumberFormatter($numberFormat); + $formatter->setMinimumFractionDigits(4); + $formatter->setMaximumFractionDigits(5); + $formattedNumber = $formatter->format('12.50000'); + $this->assertSame('12.5000', $formattedNumber); + } + + /** + * @covers ::format + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::__construct + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::format + * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat + * + * @expectedException \CommerceGuys\Intl\Exception\InvalidArgumentException + */ + public function testFormatOnlyAllowsNumbers() + { + $numberFormat = $this->createNumberFormat($this->numberFormats['latn']); + $formatter = new NumberFormatter($numberFormat); + $formatter->format('a12.34'); + } + + /** + * @covers ::formatCurrency + * @covers ::replaceSymbols + * @uses \CommerceGuys\Intl\Currency\Currency + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::__construct + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::format + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::replaceDigits + * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat + * + * @dataProvider currencyValueProvider + */ + public function testFormatCurrency($number_format, $currency, $style, $value, $expected_value) + { + $formatter = new NumberFormatter($number_format, $style); + + $formattedNumber = $formatter->formatCurrency($value, $currency); + $this->assertSame($expected_value, $formattedNumber); + } + + /** + * @covers ::parseCurrency + * @uses \CommerceGuys\Intl\Currency\Currency + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::__construct + * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat + * + * @dataProvider formattedCurrencyProvider + */ + public function testParseCurrency($number_format, $currency, $style, $value, $expected_value) + { + $formatter = new NumberFormatter($number_format, $style); + + $parsedNumber = $formatter->parseCurrency($value, $currency); + $this->assertSame($expected_value, $parsedNumber); + } + + /** + * @covers ::getNumberFormat + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::__construct + * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat + */ + public function testGetNumberFormat() + { + $numberFormat = $this->createNumberFormat($this->numberFormats['latn']); + $formatter = new NumberFormatter($numberFormat, NumberFormatter::DECIMAL); + $this->assertSame($numberFormat, $formatter->getNumberFormat()); + } + + /** + * @covers ::getMinimumFractionDigits + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::__construct + * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat + */ + public function testMinimumFractionDigits() + { + $numberFormat = $this->createNumberFormat($this->numberFormats['latn']); + + // Defaults to 0 for decimal and percentage formats. + $formatter = new NumberFormatter($numberFormat, NumberFormatter::DECIMAL); + $this->assertEquals(0, $formatter->getMinimumFractionDigits()); + $formatter = new NumberFormatter($numberFormat, NumberFormatter::PERCENT); + $this->assertEquals(0, $formatter->getMinimumFractionDigits()); + + // Should default to null for currency formats. + $formatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY); + $this->assertNull($formatter->getMinimumFractionDigits()); + $formatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY_ACCOUNTING); + $this->assertNull($formatter->getMinimumFractionDigits()); + } + + /** + * @covers ::getMaximumFractionDigits + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::__construct + * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat + */ + public function testMaximumFractionDigits() + { + $numberFormat = $this->createNumberFormat($this->numberFormats['latn']); + + // Defaults to 3 for decimal and percentage formats. + $formatter = new NumberFormatter($numberFormat, NumberFormatter::DECIMAL); + $this->assertEquals(3, $formatter->getMaximumFractionDigits()); + $formatter = new NumberFormatter($numberFormat, NumberFormatter::PERCENT); + $this->assertEquals(3, $formatter->getMaximumFractionDigits()); + + // Should default to null for currency formats. + $formatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY); + $this->assertNull($formatter->getMaximumFractionDigits()); + $formatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY_ACCOUNTING); + $this->assertNull($formatter->getMaximumFractionDigits()); + } + + /** + * @covers ::isGroupingUsed + * @covers ::setGroupingUsed + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::__construct + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::format + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::replaceDigits + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::replaceSymbols + * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat + */ + public function testGroupingUsed() + { + $numberFormat = $this->createNumberFormat($this->numberFormats['latn']); + + // The formatter groups correctly. + $formatter = new NumberFormatter($numberFormat, NumberFormatter::DECIMAL); + $this->assertTrue($formatter->isGroupingUsed()); + $this->assertSame('10,000.9', $formatter->format('10000.90')); + + // The formatter respects grouping turned off. + $formatter = new NumberFormatter($numberFormat, NumberFormatter::DECIMAL); + $formatter->setGroupingUsed(false); + $this->assertFalse($formatter->isGroupingUsed()); + $this->assertSame('10000.9', $formatter->format('10000.90')); + } + + /** + * @covers ::getCurrencyDisplay + * @covers ::setCurrencyDisplay + * @covers ::formatCurrency + * @uses \CommerceGuys\Intl\Currency\Currency + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::__construct + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::format + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::replaceDigits + * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::replaceSymbols + * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat + */ + public function testCurrencyDisplay() + { + $numberFormat = $this->createNumberFormat($this->numberFormats['latn']); + $currency = $this->createCurrency($this->currencies['USD']); + + // Currency display defaults to symbol. + $formatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY); + $this->assertSame(NumberFormatter::CURRENCY_DISPLAY_SYMBOL, $formatter->getCurrencyDisplay()); + $formattedNumber = $formatter->formatCurrency('100', $currency); + $this->assertSame('$100.00', $formattedNumber); + + // Currency display respects setting the value to currency code. + $formatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY); + $formatter->setCurrencyDisplay(NumberFormatter::CURRENCY_DISPLAY_CODE); + $this->assertSame(NumberFormatter::CURRENCY_DISPLAY_CODE, $formatter->getCurrencyDisplay()); + $formattedNumber = $formatter->formatCurrency('100', $currency); + $this->assertSame('USD100.00', $formattedNumber); + } + + /** + * Provides the number format, number style, value and expected formatted value. + */ + public function numberValueProvider() + { + return array( + array($this->createNumberFormat($this->numberFormats['latn']), NumberFormatter::DECIMAL, '-50.5', '-50.5'), + array($this->createNumberFormat($this->numberFormats['latn']), NumberFormatter::PERCENT, '50.5', '50.5%'), + array($this->createNumberFormat($this->numberFormats['latn']), NumberFormatter::DECIMAL, '5000000.5', '5,000,000.5'), + array($this->createNumberFormat($this->numberFormats['beng'], 'bn'), NumberFormatter::DECIMAL, '-50.5', '-৫০.৫'), + array($this->createNumberFormat($this->numberFormats['beng'], 'bn'), NumberFormatter::PERCENT, '50.5', '৫০.৫%'), + array($this->createNumberFormat($this->numberFormats['beng'], 'bn'), NumberFormatter::DECIMAL, '5000000.5', '৫০,০০,০০০.৫') + ); + } + + /** + * Provides the number format, currency format, number style, value and expected formatted value. + */ + public function currencyValueProvider() + { + return array( + array($this->createNumberFormat($this->numberFormats['latn']), $this->createCurrency($this->currencies['USD']), NumberFormatter::CURRENCY, '-5.05', '-$5.05'), + array($this->createNumberFormat($this->numberFormats['latn']), $this->createCurrency($this->currencies['USD']), NumberFormatter::CURRENCY_ACCOUNTING, '-5.05', '($5.05)'), + array($this->createNumberFormat($this->numberFormats['latn']), $this->createCurrency($this->currencies['USD']), NumberFormatter::CURRENCY, '500100.05', '$500,100.05'), + array($this->createNumberFormat($this->numberFormats['beng'], 'bn'), $this->createCurrency($this->currencies['BND'], 'bn'), NumberFormatter::CURRENCY, '-50.5', '-৫০.৫০BND'), + array($this->createNumberFormat($this->numberFormats['beng'], 'bn'), $this->createCurrency($this->currencies['BND'], 'bn'), NumberFormatter::CURRENCY_ACCOUNTING, '-50.5', '(৫০.৫০BND)'), + array($this->createNumberFormat($this->numberFormats['beng'], 'bn'), $this->createCurrency($this->currencies['BND'], 'bn'), NumberFormatter::CURRENCY, '500100.05', '৫,০০,১০০.০৫BND'), + ); + } + + /** + * Provides values for the formatted currency parser. + */ + public function formattedCurrencyProvider() + { + return array( + array($this->createNumberFormat($this->numberFormats['latn']), $this->createCurrency($this->currencies['USD']), NumberFormatter::CURRENCY, '$500,100.05', '500100.05'), + array($this->createNumberFormat($this->numberFormats['latn']), $this->createCurrency($this->currencies['USD']), NumberFormatter::CURRENCY, '-$1,059.59', '-1059.59'), + array($this->createNumberFormat($this->numberFormats['latn']), $this->createCurrency($this->currencies['USD']), NumberFormatter::CURRENCY_ACCOUNTING, '($1,059.59)', '-1059.59'), + array($this->createNumberFormat($this->numberFormats['beng'], 'bn'), $this->createCurrency($this->currencies['BND'], 'bn'), NumberFormatter::CURRENCY, '৫,০০,১০০.০৫BND', '500100.05'), + ); + } + + /** + * Helper for initiating a new NumberFormat object. + */ + protected function createNumberFormat(array $definition, $locale = 'en') + { + $default = array( + 'decimal_separator' => '.', + 'grouping_separator' => ',', + 'plus_sign' => '+', + 'minus_sign' => '-', + 'percent_sign' => '%' + ); + $format = array_merge($default, $definition); + + $numberFormat = new NumberFormat(); + $numberFormat->setLocale($locale); + $numberFormat->setNumberingSystem($format['numbering_system']); + $numberFormat->setDecimalSeparator($format['decimal_separator']); + $numberFormat->setGroupingSeparator($format['grouping_separator']); + $numberFormat->setPlusSign($format['plus_sign']); + $numberFormat->setMinusSign($format['minus_sign']); + $numberFormat->setPercentSign($format['percent_sign']); + $numberFormat->setDecimalPattern($format['decimal_pattern']); + $numberFormat->setPercentPattern($format['percent_pattern']); + $numberFormat->setCurrencyPattern($format['currency_pattern']); + $numberFormat->setAccountingCurrencyPattern($format['accounting_currency_pattern']); + + return $numberFormat; + } + + /** + * Helper for initiating a new Currency object. + */ + protected function createCurrency(array $definition, $locale = 'en') + { + $default = array( + 'fraction_digits' => 2 + ); + $format = array_merge($default, $definition); + + $currency = new Currency(); + $currency->setCurrencyCode($format['code']); + $currency->setName($format['name']); + $currency->setNumericCode($format['numeric_code']); + $currency->setFractionDigits($format['fraction_digits']); + $currency->setSymbol($format['symbol']); + $currency->setLocale($locale); + + return $currency; + } +} diff --git a/library/intl/tests/Language/LanguageRepositoryTest.php b/library/intl/tests/Language/LanguageRepositoryTest.php new file mode 100644 index 000000000..ea52cbf4c --- /dev/null +++ b/library/intl/tests/Language/LanguageRepositoryTest.php @@ -0,0 +1,92 @@ +<?php + +namespace CommerceGuys\Intl\Tests\Language; + +use CommerceGuys\Intl\Language\LanguageRepository; +use org\bovigo\vfs\vfsStream; + +/** + * @coversDefaultClass \CommerceGuys\Intl\Language\LanguageRepository + */ +class LanguageRepositoryTest extends \PHPUnit_Framework_TestCase +{ + /** + * English language definitions. + * + * @var array + */ + protected $englishDefinitions = array( + 'en' => array( + 'code' => 'en', + 'name' => 'English', + ), + 'fr' => array( + 'code' => 'fr', + 'name' => 'French', + ), + ); + + /** + * @covers ::__construct + */ + public function testConstructor() + { + // Mock the existence of JSON definitions on the filesystem. + $root = vfsStream::setup('resources'); + vfsStream::newFile('language/en.json')->at($root)->setContent(json_encode($this->englishDefinitions)); + + // Instantiate the language repository and confirm that the definition path + // was properly set. + $languageRepository = new LanguageRepository('vfs://resources/language/'); + $definitionPath = $this->getObjectAttribute($languageRepository, 'definitionPath'); + $this->assertEquals('vfs://resources/language/', $definitionPath); + + return $languageRepository; + } + + /** + * @covers ::get + * @covers ::loadDefinitions + * @covers ::createLanguageFromDefinition + * @uses \CommerceGuys\Intl\Language\Language + * @uses \CommerceGuys\Intl\LocaleResolverTrait + * @depends testConstructor + */ + public function testGet($languageRepository) + { + $language = $languageRepository->get('en'); + $this->assertInstanceOf('CommerceGuys\\Intl\\Language\\Language', $language); + $this->assertEquals('en', $language->getLanguageCode()); + $this->assertEquals('English', $language->getName()); + $this->assertEquals('en', $language->getLocale()); + } + + /** + * @covers ::get + * @covers ::loadDefinitions + * @uses \CommerceGuys\Intl\LocaleResolverTrait + * @expectedException \CommerceGuys\Intl\Exception\UnknownLanguageException + * @depends testConstructor + */ + public function testGetInvalidLanguage($languageRepository) + { + $languageRepository->get('de'); + } + + /** + * @covers ::getAll + * @covers ::loadDefinitions + * @covers ::createLanguageFromDefinition + * @uses \CommerceGuys\Intl\Language\Language + * @uses \CommerceGuys\Intl\LocaleResolverTrait + * @depends testConstructor + */ + public function testGetAll($languageRepository) + { + $languages = $languageRepository->getAll(); + $this->assertArrayHasKey('en', $languages); + $this->assertArrayHasKey('fr', $languages); + $this->assertEquals('en', $languages['en']->getLanguageCode()); + $this->assertEquals('fr', $languages['fr']->getLanguageCode()); + } +} diff --git a/library/intl/tests/Language/LanguageTest.php b/library/intl/tests/Language/LanguageTest.php new file mode 100644 index 000000000..037483c88 --- /dev/null +++ b/library/intl/tests/Language/LanguageTest.php @@ -0,0 +1,53 @@ +<?php + +namespace CommerceGuys\Intl\Tests\Language; + +use CommerceGuys\Intl\Language\Language; + +/** + * @coversDefaultClass \CommerceGuys\Intl\Language\Language + */ +class LanguageTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Language + */ + protected $language; + + public function setUp() + { + $this->language = new Language(); + } + + /** + * @covers ::getLanguageCode + * @covers ::setLanguageCode + * @covers ::__toString + */ + public function testLanguageCode() + { + $this->language->setLanguageCode('en'); + $this->assertEquals('en', $this->language->getLanguageCode()); + $this->assertEquals('en', (string) $this->language); + } + + /** + * @covers ::getName + * @covers ::setName + */ + public function testName() + { + $this->language->setName('English'); + $this->assertEquals('English', $this->language->getName()); + } + + /** + * @covers ::getLocale + * @covers ::setLocale + */ + public function testLocale() + { + $this->language->setLocale('en'); + $this->assertEquals('en', $this->language->getLocale()); + } +} diff --git a/library/intl/tests/LocaleResolverTest.php b/library/intl/tests/LocaleResolverTest.php new file mode 100644 index 000000000..a52dea353 --- /dev/null +++ b/library/intl/tests/LocaleResolverTest.php @@ -0,0 +1,51 @@ +<?php + +namespace CommerceGuys\Intl\Tests; + +use org\bovigo\vfs\vfsStream; + +/** + * @coversDefaultClass \CommerceGuys\Intl\LocaleResolverTrait + */ +class LocaleResolverTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var DummyRepository + */ + protected $repository; + + public function setUp() + { + // Simulate the presence of various definitions. + $root = vfsStream::setup('resources'); + vfsStream::newFile('dummy/bs-Cyrl.json')->at($root)->setContent(''); + vfsStream::newFile('dummy/bs.json')->at($root)->setContent(''); + vfsStream::newFile('dummy/en.json')->at($root)->setContent(''); + + $this->repository = new DummyRepository(); + } + + /** + * @covers ::resolveLocale + * @covers ::getLocaleVariants + */ + public function testLocaleFallback() + { + $locale = $this->repository->runResolveLocale('bs-Cyrl-BA'); + $this->assertEquals('bs-Cyrl', $locale); + $locale = $this->repository->runResolveLocale('bs-Latn-BA'); + $this->assertEquals('bs', $locale); + $locale = $this->repository->runResolveLocale('de', 'en'); + $this->assertEquals('en', $locale); + } + + /** + * @covers ::resolveLocale + * @covers ::getLocaleVariants + * @expectedException \CommerceGuys\Intl\Exception\UnknownLocaleException + */ + public function testInvalidLocale() + { + $locale = $this->repository->runResolveLocale('de'); + } +} diff --git a/library/intl/tests/NumberFormat/NumberFormatRepositoryTest.php b/library/intl/tests/NumberFormat/NumberFormatRepositoryTest.php new file mode 100644 index 000000000..41e45805d --- /dev/null +++ b/library/intl/tests/NumberFormat/NumberFormatRepositoryTest.php @@ -0,0 +1,69 @@ +<?php + +namespace CommerceGuys\Intl\Tests\NumberFormat; + +use CommerceGuys\Intl\NumberFormat\NumberFormatRepository; +use org\bovigo\vfs\vfsStream; + +/** + * @coversDefaultClass \CommerceGuys\Intl\NumberFormat\NumberFormatRepository + */ +class NumberFormatRepositoryTest extends \PHPUnit_Framework_TestCase +{ + /** + * English number format definition. + * + * @var array + */ + protected $englishDefinition = array( + 'numbering_system' => 'latn', + 'decimal_pattern' => '#,##0.###', + 'percent_pattern' => '#,##0%', + 'currency_pattern' => '¤#,##0.00', + 'accounting_currency_pattern' => '¤#,##0.00;(¤#,##0.00)', + ); + + /** + * @covers ::__construct + */ + public function testConstructor() + { + // Mock the existence of JSON definitions on the filesystem. + $root = vfsStream::setup('resources'); + vfsStream::newFile('number_format/en.json')->at($root)->setContent(json_encode($this->englishDefinition)); + + // Instantiate the number format repository and confirm that the definition + // path was properly set. + $numberFormatRepository = new NumberFormatRepository('vfs://resources/number_format/'); + $definitionPath = $this->getObjectAttribute($numberFormatRepository, 'definitionPath'); + $this->assertEquals('vfs://resources/number_format/', $definitionPath); + + return $numberFormatRepository; + } + + /** + * @covers ::get + * @covers ::createNumberFormatFromDefinition + * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat + * @uses \CommerceGuys\Intl\LocaleResolverTrait + * @depends testConstructor + */ + public function testGet($numberFormatRepository) + { + $numberFormat = $numberFormatRepository->get('en'); + $this->assertInstanceOf('CommerceGuys\\Intl\\NumberFormat\\NumberFormat', $numberFormat); + $this->assertEquals('en', $numberFormat->getLocale()); + $this->assertEquals('latn', $numberFormat->getNumberingSystem()); + $this->assertEquals('.', $numberFormat->getDecimalSeparator()); + $this->assertEquals(',', $numberFormat->getGroupingSeparator()); + $this->assertEquals('+', $numberFormat->getPlusSign()); + $this->assertEquals('-', $numberFormat->getMinusSign()); + $this->assertEquals('%', $numberFormat->getPercentSign()); + $this->assertEquals('#,##0.###', $numberFormat->getDecimalPattern()); + $this->assertEquals('#,##0%', $numberFormat->getPercentPattern()); + $this->assertEquals('¤#,##0.00', $numberFormat->getCurrencyPattern()); + $this->assertEquals('¤#,##0.00;(¤#,##0.00)', $numberFormat->getAccountingCurrencyPattern()); + + return $numberFormat; + } +} diff --git a/library/intl/tests/NumberFormat/NumberFormatTest.php b/library/intl/tests/NumberFormat/NumberFormatTest.php new file mode 100644 index 000000000..299dc41cb --- /dev/null +++ b/library/intl/tests/NumberFormat/NumberFormatTest.php @@ -0,0 +1,131 @@ +<?php + +namespace CommerceGuys\Intl\Tests\NumberFormat; + +use CommerceGuys\Intl\NumberFormat\NumberFormat; + +/** + * @coversDefaultClass CommerceGuys\Intl\NumberFormat\NumberFormat + */ +class NumberFormatTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var NumberFormat + */ + protected $numberFormat; + + public function setUp() + { + $this->numberFormat = new NumberFormat(); + } + + /** + * @covers ::getLocale + * @covers ::setLocale + */ + public function testLocale() + { + $this->numberFormat->setLocale('en'); + $this->assertEquals('en', $this->numberFormat->getLocale()); + } + + /** + * @covers ::getNumberingSystem + * @covers ::setNumberingSystem + */ + public function testNumberingSystem() + { + $this->numberFormat->setNumberingSystem('latn'); + $this->assertEquals('latn', $this->numberFormat->getNumberingSystem()); + } + + /** + * @covers ::getDecimalSeparator + * @covers ::setDecimalSeparator + */ + public function testDecimalSeparator() + { + $this->numberFormat->setDecimalSeparator('.'); + $this->assertEquals('.', $this->numberFormat->getDecimalSeparator()); + } + + /** + * @covers ::getGroupingSeparator + * @covers ::setGroupingSeparator + */ + public function testGroupingSeparator() + { + $this->numberFormat->setGroupingSeparator(','); + $this->assertEquals(',', $this->numberFormat->getGroupingSeparator()); + } + + /** + * @covers ::getPlusSign + * @covers ::setPlusSign + */ + public function testPlusSign() + { + $this->numberFormat->setPlusSign('+'); + $this->assertEquals('+', $this->numberFormat->getPlusSign()); + } + + /** + * @covers ::getMinusSign + * @covers ::setMinusSign + */ + public function testMinusSign() + { + $this->numberFormat->setMinusSign('-'); + $this->assertEquals('-', $this->numberFormat->getMinusSign()); + } + + /** + * @covers ::getPercentSign + * @covers ::setPercentSign + */ + public function testPercentSign() + { + $this->numberFormat->setPercentSign('%'); + $this->assertEquals('%', $this->numberFormat->getPercentSign()); + } + + /** + * @covers ::getDecimalPattern + * @covers ::setDecimalPattern + */ + public function testDecimalPattern() + { + $this->numberFormat->setDecimalPattern('#,##0.###'); + $this->assertEquals('#,##0.###', $this->numberFormat->getDecimalPattern()); + } + + /** + * @covers ::getPercentPattern + * @covers ::setPercentPattern + */ + public function testPercentPattern() + { + $this->numberFormat->setPercentPattern('#,##0%'); + $this->assertEquals('#,##0%', $this->numberFormat->getPercentPattern()); + } + + /** + * @covers ::getCurrencyPattern + * @covers ::setCurrencyPattern + */ + public function testCurrencyPattern() + { + $this->numberFormat->setCurrencyPattern('¤#,##0.00'); + $this->assertEquals('¤#,##0.00', $this->numberFormat->getCurrencyPattern()); + } + + /** + * @covers ::getAccountingCurrencyPattern + * @covers ::setAccountingCurrencyPattern + */ + public function testAccountingCurrencyPattern() + { + $this->numberFormat->setAccountingCurrencyPattern('¤#,##0.00;(¤#,##0.00)'); + $this->assertEquals('¤#,##0.00;(¤#,##0.00)', $this->numberFormat->getAccountingCurrencyPattern()); + } +} |