8000 [Locale] IntlDateFormatter by igorw · Pull Request #63 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Locale] IntlDateFormatter #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 32 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8051e9a
[Locale] refactored Locale class
eriksencosta Jan 25, 2011
2850851
[Locale] renamed and simplified Locale::isIntlExtensionAvailable()
Jan 25, 2011
4c1ce75
[Locale] added intl's \Locale constants to the Locale class
Jan 25, 2011
2333ee3
[Locale] added NumberFormatterInterface
eriksencosta Jan 25, 2011
8f17d18
[Locale] added NumberFormatter class
Jan 25, 2011
5a51450
[Locale] changed NumberFormatInterface::getLocale() to use Locale::AC…
Jan 25, 2011
6672c39
[Locale] added SimpleNumberFormatter class (not finished)
Jan 26, 2011
d245fb8
[Locale] partialy implemented \NumberFormatter's format() and formatC…
Jan 29, 2011
af499ee
[Locale] partial impl of \NumberFormatter::parse()
eriksencosta Jan 30, 2011
8b99817
[Locale] extracted method
Feb 2, 2011
75b55ca
[Locale] extract method refactoring
Feb 2, 2011
1b0c671
[Locale] method throws exception for unsupported argument value
Feb 2, 2011
b198a9e
[Locale] refactored test code
Feb 2, 2011
ff6083f
[Locale] removed support for ceil and floor rounding modes as them do…
Feb 2, 2011
353418e
[Locale] updated docblock
Feb 2, 2011
1559557
[Locale] added implementation to getErrorCode and getErrorMessage met…
Feb 2, 2011
33bb0c0
[Locale] not implemented methods throws RuntimeException
Feb 2, 2011
cceeefd
[Locale] Removed NumberFormatterInterface and NumberFormatter wrapper…
Feb 5, 2011
4f024e3
[Locale] moved and renamed SimpleNumberFormatter to StubNumberFormatter
Feb 5, 2011
0aaf7b6
[Locale] added learning tests for the \NumberFormatter class
Feb 5, 2011
59a0f72
[Locale] first implementation of StubIntlDateFormatter
igorw Feb 5, 2011
3da4307
[Locale] add support for escaping, give specifics on implementation u…
igorw Feb 6, 2011
09b24fa
[Locale] move intl bugs to skipped tests
igorw Feb 6, 2011
43794ba
[Locale] support for G and Q placeholders in StubIntlDateFormatter::f…
igorw Feb 6, 2011
af6fae6
[Locale] add support for L, which is the same as M
igorw Feb 6, 2011
a91c970
[Locale] use assertSame instead of assertEquals
igorw Feb 6, 2011
c1aa274
[Locale] add support for h
igorw Feb 6, 2011
fec50dc
[Locale] support for D (day of year)
igorw Feb 6, 2011
b7f42f0
[Locale] support for E (day of week)
igorw Feb 6, 2011
d3e24d6
[Locale] support for a (AM/PM)
igorw Feb 6, 2011
85d05c8
[Locale] support for H (24 hour)
igorw Feb 6, 2011
0396dab
[Locale] refactor IntlDateFormatter::format to build regExp dynamically
igorw Feb 6, 2011
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Locale] added learning tests for the \NumberFormatter class
  • Loading branch information
Eriksen Costa committed Feb 5, 2011
commit 0aaf7b655e5c882ce6910fb1d525ce06cbc090c9
10000 < ACE5 tr data-hunk="b7ecfb515ba440713d7158a5156fb171918374d1c4c06d70b0be3ddce1ffbd2d" class="show-top-border">
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
<?php

namespace Symfony\Tests\Component\Locale\Stub\Learning;

class NumberFormatterTest extends \PHPUnit_Framework_TestCase
{
private $formatter = null;

public function setUp()
{
$this->formatter = $this->getDecimalFormatter();
}

private function getDecimalFormatter()
{
return new \NumberFormatter('en', \NumberFormatter::DECIMAL);
}

private function getCurrencyFormatter()
{
$formatter = new \NumberFormatter('en', \NumberFormatter::CURRENCY);
$formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, 'SFD');
return $formatter;
}

/**
* @dataProvider formatCurrencyProvider
*/
public function testFormatCurrency(\NumberFormatter $formatter, $value, $currency, $expectedValue)
{
$formattedCurrency = $formatter->formatCurrency($value, $currency);
$this->assertEquals($expectedValue, $formattedCurrency);
}

public function formatCurrencyProvider()
{
$df = $this->getDecimalFormatter();
$cf = $this->getCurrencyFormatter();

return array(
array($df, 100, 'BRL', '100'),
array($df, 100.1, 'BRL', '100.1'),
array($cf, 100, 'BRL', 'R$100.00'),
array($cf, 100.1, 'BRL', 'R$100.10')
);
}

/**
* @dataProvider formatProvider
*/
public function testFormat($formatter, $value, $expectedValue)
{
$formattedValue = $formatter->format($value);
$this->assertEquals($expectedValue, $formattedValue);
}

public function formatProvider()
{
$df = $this->getDecimalFormatter();
$cf = $this->getCurrencyFormatter();

return array(
array($df, 1, '1'),
array($df, 1.1, '1.1'),
array($cf, 1, 'SFD1.00'),
array($cf, 1.1, 'SFD1.10')
);
}

/**
* @dataProvider formatTypeDefaultProvider
*/
public function testFormatTypeDefault($formatter, $value, $expectedValue)
{
$formattedValue = $formatter->format($value, \NumberFormatter::TYPE_DEFAULT);
$this->assertEquals($expectedValue, $formattedValue);
}

public function formatTypeDefaultProvider()
{
$df = $this->getDecimalFormatter();
$cf = $this->getCurrencyFormatter();

return array(
array($df, 1, '1'),
array($df, 1.1, '1.1'),
array($cf, 1, 'SFD1.00'),
array($cf, 1.1, 'SFD1.10')
);
}

/**
* @dataProvider formatTypeInt32Provider
*/
public function testFormatTypeInt32($formatter, $value, $expectedValue, $message = '')
{
$formattedValue = $formatter->format($value, \NumberFormatter::TYPE_INT32);
$this->assertEquals($expectedValue, $formattedValue, $message);
}

public function formatTypeInt32Provider()
{
$df = $this->getDecimalFormatter();
$cf = $this->getCurrencyFormatter();

$message = '->format() TYPE_INT32 formats incosistencily an integer if out of 32 bit range.';

return array(
array($df, 1, '1'),
array($df, 1.1, '1'),
array($df, 2147483648, '-2,147,483,648', $message),
array($df, -2147483649, '2,147,483,647', $message),
array($cf, 1, 'SFD1.00'),
array($cf, 1.1, 'SFD1.00'),
array($cf, 2147483648, '(SFD2,147,483,648.00)', $message),
array($cf, -2147483649, 'SFD2,147,483,647.00', $message)
);
}

/**
* The parse() method works differently with integer out of the 32 bit range. format() works fine.
* @dataProvider formatTypeInt64Provider
*/
public function testFormatTypeInt64($formatter, $value, $expectedValue)
{
$formattedValue = $formatter->format($value, \NumberFormatter::TYPE_INT64);
$this->assertEquals($expectedValue, $formattedValue);
}

public function formatTypeInt64Provider()
{
$df = $this->getDecimalFormatter();
$cf = $this->getCurrencyFormatter();

return array(
array($df, 1, '1'),
array($df, 1.1, '1'),
array($df, 2147483648, '2,147,483,648'),
array($df, -2147483649, '-2,147,483,649'),
array($cf, 1, 'SFD1.00'),
array($cf, 1.1, 'SFD1.00'),
array($cf, 2147483648, 'SFD2,147,483,648.00'),
array($cf, -2147483649, '(SFD2,147,483,649.00)')
);
}

/**
* @dataProvider formatTypeDoubleProvider
*/
public function testFormatTypeDouble($formatter, $value, $expectedValue)
{
$formattedValue = $formatter->format($value, \NumberFormatter::TYPE_DOUBLE);
$this->assertEquals($expectedValue, $formattedValue);
}

public function formatTypeDoubleProvider()
{
$df = $this->getDecimalFormatter();
$cf = $this->getCurrencyFormatter();

return array(
array($df, 1, '1'),
array($df, 1.1, '1.1'),
array($cf, 1, 'SFD1.00'),
array($cf, 1.1, 'SFD1.10'),
);
}

/**
* @dataProvider formatTypeCurrencyProvider
* @expectedException PHPUnit_Framework_Error_Warning
*/
public function testFormatTypeCurrency($formatter, $value)
{
$formattedValue = $formatter->format($value, \NumberFormatter::TYPE_CURRENCY);
}

public function formatTypeCurrencyProvider()
{
$df = $this->getDecimalFormatter();
$cf = $this->getCurrencyFormatter();

return array(
array($df, 1),
array($df, 1),
);
}

/**
* @dataProvider parseCurrencyProvider
*/
public function testParseCurrency($formatter, $value, $expectedValue, $expectedCurrency)
{
$currency = '';
$parsedValue = $formatter->parseCurrency($value, $currency);
$this->assertEquals($expectedValue, $parsedValue);
$this->assertEquals($expectedCurrency, $currency);
}

public function parseCurrencyProvider()
{
$df = $this->getDecimalFormatter();
$cf = $this->getCurrencyFormatter();

return array(
array($df, 1, 1, ''),
array($df, 1.1, 1.1, ''),
array($cf, '$1.00', 1, 'USD'),
array($cf, '€1.00', 1, 'EUR'),
array($cf, 'R$1.00', 1, 'BRL')
);
}

public function testParse()
{
$parsedValue = $this->formatter->parse('1');
$this->assertInternalType('float', $parsedValue, '->parse() as double by default.');
$this->assertEquals(1, $parsedValue);

$parsedValue = $this->formatter->parse('1', \NumberFormatter::TYPE_DOUBLE, $position);
$this->assertNull($position, '->parse() returns null to the $position reference if it doesn\'t had a defined value.');

$position = 0;
$parsedValue = $this->formatter->parse('1', \NumberFormatter::TYPE_DOUBLE, $position);
$this->assertEquals(1, $position);

$parsedValue = $this->formatter->parse('prefix1', \NumberFormatter::TYPE_DOUBLE);
$this->assertFalse($parsedValue, '->parse() does not parse a number with a string prefix.');

$parsedValue = $this->formatter->parse('1suffix', \NumberFormatter::TYPE_DOUBLE);
$this->assertEquals(1, $parsedValue, '->parse() parses a number with a string suffix.');

$position = 0;
$parsedValue = $this->formatter->parse('1suffix', \NumberFormatter::TYPE_DOUBLE, $position);
$this->assertEquals(1, $parsedValue);
$this->assertEquals(1, $position, '->parse() ignores anything not a number before the number.');
}

/**
* @expectedException PHPUnit_Framework_Error_Warning
*/
public function testParseTypeDefault()
{
$this->formatter->parse('1', \NumberFormatter::TYPE_DEFAULT);
}

public function testParseTypeInt32()
{
$parsedValue = $this->formatter->parse('1', \NumberFormatter::TYPE_INT32);
$this->assertInternalType('integer', $parsedValue);
$this->assertEquals(1, $parsedValue);

$parsedValue = $this->formatter->parse('1.1', \NumberFormatter::TYPE_INT32);
$this->assertInternalType('integer', $parsedValue);
$this->assertEquals(1, $parsedValue, '->parse() TYPE_INT32 ignores the decimal part of a number and uses only the integer one.');

// int 32 out of range
$parsedValue = $this->formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT32);
$this->assertFalse($parsedValue, '->parse() TYPE_INT32 returns false if the value is out of range.');
$parsedValue = $this->formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT32);
$this->assertFalse($parsedValue, '->parse() TYPE_INT32 returns false if the value is out of range.');
}

public function testParseInt64()
{
// int 64 parsing
$parsedValue = $this->formatter->parse('2,147,483,647', \NumberFormatter::TYPE_INT64);
$this->assertInternalType('integer', $parsedValue);
$this->assertEquals(2147483647, $parsedValue);

$parsedValue = $this->formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
$this->assertInternalType('integer', $parsedValue);
$this->assertEquals(-2147483648, $parsedValue);

// int 64 using only 32 bit range strangeness
$parsedValue = $this->formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
$this->assertInternalType('integer', $parsedValue);
$this->assertEquals(-2147483648, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');

$parsedValue = $this->formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
$this->assertInternalType('integer', $parsedValue);
$this->assertEquals(2147483647, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');
}

public function testParseTypeDouble()
{
$parsedValue = $this->formatter->parse('1', \NumberFormatter::TYPE_DOUBLE);
$this->assertInternalType('float', $parsedValue);
$this->assertEquals(1, $parsedValue);

$parsedValue = $this->formatter->parse('1.1');
$this->assertInternalType('float', $parsedValue);
$this->assertEquals(1.1, $parsedValue);

$parsedValue = $this->formatter->parse('1,1');
$this->assertInternalType('float', $parsedValue);
$this->assertEquals(11, $parsedValue);
}

/**
* @expectedException PHPUnit_Framework_Error_Warning
*/
public function testParseTypeCurrency()
{
$this->formatter->parse('1', \NumberFormatter::TYPE_CURRENCY);
}
}
0