Initial commit: apps.supersam24.ru
This commit is contained in:
commit
e955e1da36
6566 files changed
+1271095
No files matched your search
+29
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace Aura\Intl;
|
||||
|
||||
class BasicFormatterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected function newFormatter()
|
||||
{
|
||||
return new BasicFormatter;
|
||||
}
|
||||
|
||||
public function testFormat()
|
||||
{
|
||||
$formatter = $this->newFormatter();
|
||||
|
||||
$locale = 'en_US';
|
||||
$expect = 'Hello world 88!';
|
||||
$tokens_values = ['foo' => 'world', 'bar' => '88', 'baz' => '!'];
|
||||
|
||||
$string = 'Hello {foo} {bar}{baz}';
|
||||
$actual = $formatter->format($locale, $string, $tokens_values);
|
||||
$this->assertSame($expect, $actual);
|
||||
|
||||
$tokens_values = ['array' => ['foo', 'bar', 'baz']];
|
||||
$string = 'Array {array}';
|
||||
$expect = 'Array "foo", "bar", "baz"';
|
||||
$actual = $formatter->format($locale, $string, $tokens_values);
|
||||
$this->assertSame($expect, $actual);
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
namespace Aura\Intl;
|
||||
|
||||
class FormatterLocatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function test__constructAndGet()
|
||||
{
|
||||
$formatters = new FormatterLocator([
|
||||
'mock' => function () {
|
||||
return new \Aura\Intl\MockFormatter;
|
||||
},
|
||||
]);
|
||||
|
||||
$expect = 'Aura\Intl\MockFormatter';
|
||||
$actual = $formatters->get('mock');
|
||||
$this->assertInstanceOf($expect, $actual);
|
||||
}
|
||||
|
||||
public function testSetAndGet()
|
||||
{
|
||||
$formatters = new FormatterLocator;
|
||||
$formatters->set('mock', function () {
|
||||
return new \Aura\Intl\MockFormatter;
|
||||
});
|
||||
|
||||
$expect = 'Aura\Intl\MockFormatter';
|
||||
$actual = $formatters->get('mock');
|
||||
$this->assertInstanceOf($expect, $actual);
|
||||
}
|
||||
|
||||
public function testGet_noSuchFormatter()
|
||||
{
|
||||
$formatters = new FormatterLocator;
|
||||
$this->setExpectedException('Aura\Intl\Exception\FormatterNotMapped');
|
||||
$formatters->get('noSuchFormatter');
|
||||
}
|
||||
}
|
||||
+195
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
namespace Aura\Intl;
|
||||
|
||||
class IntlFormatterTest extends BasicFormatterTest
|
||||
{
|
||||
protected function newFormatter()
|
||||
{
|
||||
return new IntlFormatter;
|
||||
}
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
if (! extension_loaded('intl')) {
|
||||
$this->markTestSkipped('This test is skipped if the Intl Extension is not loaded.');
|
||||
}
|
||||
}
|
||||
|
||||
public function testIntlVersion()
|
||||
{
|
||||
$this->setExpectedException('Aura\Intl\Exception\IcuVersionTooLow');
|
||||
$formatter = new IntlFormatter('4.7');
|
||||
}
|
||||
|
||||
/**
|
||||
* This test fails on PHP 5.4.4
|
||||
* The return value expected is No pages , but returns false
|
||||
*
|
||||
* var_dump( msgfmt_get_error_message($fmt) );
|
||||
* U_ZERO_ERROR
|
||||
*
|
||||
* It seems $fmt = msgfmt_create($locale, $string); is not creating with
|
||||
* this string , so the msgfmt_format() throws expects parameter 1 to be
|
||||
* MessageFormatter, null given error.
|
||||
*/
|
||||
public function testFormat_plural()
|
||||
{
|
||||
$formatter = $this->newFormatter();
|
||||
$locale = 'en_US';
|
||||
$string = '{pages,plural,'
|
||||
. '=0{No pages.}'
|
||||
. '=1{One page only.}'
|
||||
. 'other{Page {page} of # pages.}'
|
||||
. '}';
|
||||
|
||||
$tokens_values = ['page' => 0, 'pages' => 0];
|
||||
$expect = 'No pages.';
|
||||
$actual = $formatter->format($locale, $string, $tokens_values);
|
||||
$this->assertSame($expect, $actual);
|
||||
|
||||
$tokens_values = ['page' => 1, 'pages' => 1];
|
||||
$expect = 'One page only.';
|
||||
$actual = $formatter->format($locale, $string, $tokens_values);
|
||||
$this->assertSame($expect, $actual);
|
||||
|
||||
$tokens_values = ['page' => 1, 'pages' => 2];
|
||||
$expect = 'Page 1 of 2 pages.';
|
||||
$actual = $formatter->format($locale, $string, $tokens_values);
|
||||
$this->assertSame($expect, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provide_testFormat_select
|
||||
*/
|
||||
public function testFormat_select($tokens_values, $expect)
|
||||
{
|
||||
$locale = 'en_US';
|
||||
$string = "
|
||||
{gender, select,
|
||||
female {
|
||||
{count, plural, offset:1
|
||||
=0 {{from} does not give a party.}
|
||||
=1 {{from} invites {to} to her party.}
|
||||
=2 {{from} invites {to} and one other person to her party.}
|
||||
other {{from} invites {to} as one of the # people invited to her party.}
|
||||
}
|
||||
}
|
||||
male {
|
||||
{count, plural, offset:1
|
||||
=0 {{from} does not give a party.}
|
||||
=1 {{from} invites {to} to his party.}
|
||||
=2 {{from} invites {to} and one other person to his party.}
|
||||
other {{from} invites {to} as one of the # other people invited to his party.}
|
||||
}
|
||||
}
|
||||
other {
|
||||
{count, plural, offset:1
|
||||
=0 {{from} does not give a party.}
|
||||
=1 {{from} invites {to} to their party.}
|
||||
=2 {{from} invites {to} and one other person to their party.}
|
||||
other {{from} invites {to} as one of the # other people invited to their party.}
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
$formatter = $this->newFormatter();
|
||||
$actual = $formatter->format($locale, $string, $tokens_values);
|
||||
$this->assertSame(trim($expect), trim($actual));
|
||||
}
|
||||
|
||||
public function provide_testFormat_select()
|
||||
{
|
||||
return [
|
||||
[array('gender' => 'female', 'count' => 0, 'from' => 'Alice', 'to' => 'Bob'), 'Alice does not give a party.'],
|
||||
[array('gender' => 'male', 'count' => 1, 'from' => 'Bob', 'to' => 'Alice'), 'Bob invites Alice to his party.'],
|
||||
[array('gender' => 'none', 'count' => 2, 'from' => 'Alice', 'to' => 'Bob'), 'Alice invites Bob and one other person to their party.'],
|
||||
[array('gender' => 'female', 'count' => 27, 'from' => 'Alice', 'to' => 'Bob'), 'Alice invites Bob as one of the 26 people invited to her party.'],
|
||||
];
|
||||
}
|
||||
|
||||
public function testFormat_cannotInstantiateFormatter()
|
||||
{
|
||||
$locale = 'en_US';
|
||||
// uses {count} instead of #, which should fail
|
||||
$string = "
|
||||
{gender, select,
|
||||
female {
|
||||
{count, plural, offset:1
|
||||
=0 {{from} does not give a party.}
|
||||
=1 {{from} invites {to} to her party.}
|
||||
=2 {{from} invites {to} and one other person to her party.}
|
||||
other {{from} invites {to} as one of the {count} people invited to her party.}
|
||||
}
|
||||
}
|
||||
male {
|
||||
{count, plural, offset:1
|
||||
=0 {{from} does not give a party.}
|
||||
=1 {{from} invites {to} to his party.}
|
||||
=2 {{from} invites {to} and one other person to his party.}
|
||||
other {{from} invites {to} as one of the {count} other people invited to his party.}
|
||||
}
|
||||
}
|
||||
other {
|
||||
{count, plural, offset:1
|
||||
=0 {{from} does not give a party.}
|
||||
=1 {{from} invites {to} to their party.}
|
||||
=2 {{from} invites {to} and one other person to their party.}
|
||||
other {{from} invites {to} as one of the {count} other people invited to their party.}
|
||||
}
|
||||
}
|
||||
}";
|
||||
$formatter = $this->newFormatter();
|
||||
$this->setExpectedException('Aura\Intl\Exception\CannotFormat');
|
||||
$actual = $formatter->format($locale, $string, array('gender' => 'female', 'count' => 5, 'from' => 'Alice', 'to' => 'Bob'));
|
||||
}
|
||||
|
||||
// @todo MAKE IT SO THAT WE CHECK FOR TOKENS IN THE ARRAY
|
||||
public function testFormat_cannotFormat()
|
||||
{
|
||||
$locale = 'en_US';
|
||||
$string = 'Hello {foo}';
|
||||
$tokens_values = ['bar' => 'baz']; // no 'foo' token
|
||||
$formatter = $this->newFormatter();
|
||||
|
||||
$expect = 'Hello {foo}';
|
||||
$actual = $formatter->format($locale, $string, $tokens_values);
|
||||
$this->assertSame($expect, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provide_testIssue6
|
||||
*/
|
||||
public function testIssue6($tokens_values, $expect)
|
||||
{
|
||||
$locale = 'en_US';
|
||||
$string = '{gender, select, female{{name} is {gender} and she report bugs!} male{{name} is {gender} and he report bugs!} other{{name} report bugs!}}';
|
||||
$formatter = $this->newFormatter();
|
||||
$actual = $formatter->format($locale, $string, $tokens_values);
|
||||
$this->assertSame($expect, $actual);
|
||||
}
|
||||
|
||||
public function testEmptyStringThrowsException()
|
||||
{
|
||||
$locale = 'en_US';
|
||||
$string = '';
|
||||
$formatter = $this->newFormatter();
|
||||
if (! defined('HHVM_VERSION')) {
|
||||
$this->setExpectedException('Aura\Intl\Exception\CannotInstantiateFormatter');
|
||||
}
|
||||
$actual = $formatter->format($locale, $string, []);
|
||||
if (defined('HHVM_VERSION')) {
|
||||
// HHVM will instantiate Formatter even if empty string is passed.
|
||||
$expect = '';
|
||||
$this->assertSame($expect, $actual);
|
||||
}
|
||||
}
|
||||
|
||||
public function provide_testIssue6()
|
||||
{
|
||||
return [
|
||||
[array('gender' => 'female', 'name' => 'Alice'), 'Alice is female and she report bugs!'],
|
||||
[array('gender' => 'male', 'name' => 'Alexander' ), 'Alexander is male and he report bugs!'],
|
||||
[array('gender' => '', 'name' => 'Unknown'), 'Unknown report bugs!'],
|
||||
];
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace Aura\Intl;
|
||||
|
||||
class MockFormatter implements FormatterInterface
|
||||
{
|
||||
public function format($locale, $string, array $tokens_values = [])
|
||||
{
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace Aura\Intl;
|
||||
|
||||
/**
|
||||
* Test class for Package.
|
||||
* Generated by PHPUnit on 2012-10-27 at 22:46:01.
|
||||
*/
|
||||
class PackageLocatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $packages;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->packages = new PackageLocator([
|
||||
'Vendor.Foo' => [
|
||||
'en_US' => function () {
|
||||
return new \Aura\Intl\Package;
|
||||
},
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
// get once to create it the first time
|
||||
$first = $this->packages->get('Vendor.Foo', 'en_US');
|
||||
$this->assertInstanceOf('Aura\Intl\Package', $first);
|
||||
|
||||
// get again to make sure it's the same object
|
||||
$again = $this->packages->get('Vendor.Foo', 'en_US');
|
||||
$this->assertSame($first, $again);
|
||||
|
||||
// try for an unregistered package
|
||||
$this->setExpectedException('Aura\Intl\Exception');
|
||||
$this->packages->get('Vendor.Bar', 'en_US');
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
namespace Aura\Intl;
|
||||
|
||||
/**
|
||||
* Test class for Package.
|
||||
* Generated by PHPUnit on 2012-10-27 at 22:46:01.
|
||||
*/
|
||||
class PackageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $package;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$factory = new PackageFactory;
|
||||
$this->package = $factory->newInstance([
|
||||
'fallback' => 'Vendor.Fallback',
|
||||
'formatter' => 'intl',
|
||||
'messages' => [
|
||||
'ERR_NO_SUCH_OPTION' => "The option {option} is not recognized.",
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
$expect = 'Vendor.Fallback';
|
||||
$actual = $this->package->getFallback();
|
||||
$this->assertSame($expect, $actual);
|
||||
|
||||
$expect = 'intl';
|
||||
$actual = $this->package->getFormatter();
|
||||
$this->assertSame($expect, $actual);
|
||||
|
||||
$expect = [
|
||||
'ERR_NO_SUCH_OPTION' => "The option {option} is not recognized.",
|
||||
];
|
||||
$actual = $this->package->getMessages();
|
||||
$this->assertSame($expect, $actual);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace Aura\Intl;
|
||||
|
||||
class TranslatorLocatorFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function test__newInstance()
|
||||
{
|
||||
$factory = new TranslatorLocatorFactory();
|
||||
$this->assertInstanceOf('Aura\Intl\TranslatorLocator', $factory->newInstance());
|
||||
}
|
||||
}
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
namespace Aura\Intl;
|
||||
|
||||
class TranslatorLocatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $factory;
|
||||
|
||||
protected $translators;
|
||||
|
||||
protected $packages;
|
||||
|
||||
protected $formatters;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$registry['Vendor.Package']['en_US'] = function () {
|
||||
return new \Aura\Intl\Package(
|
||||
'mock',
|
||||
null,
|
||||
[
|
||||
'ERR_NO_SUCH_OPTION' => "The option {option} is not recognized.",
|
||||
]
|
||||
);
|
||||
};
|
||||
|
||||
$registry['Vendor.Package']['pt_BR'] = function () {
|
||||
return new \Aura\Intl\Package(
|
||||
'mock',
|
||||
null,
|
||||
[
|
||||
'ERR_NO_SUCH_OPTION' => "O {option} opção não é reconhecido.",
|
||||
]
|
||||
);
|
||||
};
|
||||
|
||||
$this->packages = new PackageLocator($registry);
|
||||
|
||||
$this->formatters = new FormatterLocator([
|
||||
'mock' => function () {
|
||||
return new MockFormatter;
|
||||
},
|
||||
]);
|
||||
|
||||
$this->factory = new TranslatorFactory;
|
||||
|
||||
$this->translators = new TranslatorLocator(
|
||||
$this->packages,
|
||||
$this->formatters,
|
||||
$this->factory,
|
||||
'en_US'
|
||||
);
|
||||
}
|
||||
|
||||
public function testSetAndGetLocale()
|
||||
{
|
||||
$expect = 'pt_BR';
|
||||
$this->translators->setLocale($expect);
|
||||
$actual = $this->translators->getLocale();
|
||||
$this->assertSame($expect, $actual);
|
||||
}
|
||||
|
||||
public function testGetFactory()
|
||||
{
|
||||
$actual = $this->translators->getFactory();
|
||||
$this->assertSame($this->factory, $actual);
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
$actual = $this->translators->get('Vendor.Package');
|
||||
$this->assertInstanceOf('Aura\Intl\Translator', $actual);
|
||||
}
|
||||
|
||||
public function testGetPackages()
|
||||
{
|
||||
$actual = $this->translators->getPackages();
|
||||
$this->assertSame($this->packages, $actual);
|
||||
}
|
||||
|
||||
public function testGetFormatterLocator()
|
||||
{
|
||||
$actual = $this->translators->getFormatters();
|
||||
$this->assertSame($this->formatters, $actual);
|
||||
}
|
||||
|
||||
public function testIssue9()
|
||||
{
|
||||
$this->packages->set('Vendor.Package', 'en_UK', function () {
|
||||
$package = new Package('mock');
|
||||
$package->setMessages([
|
||||
'FOO' => 'The text for "foo."',
|
||||
'BAR' => 'The text for "bar."',
|
||||
]);
|
||||
return $package;
|
||||
});
|
||||
|
||||
$translator = $this->translators->get('Vendor.Package', 'en_UK');
|
||||
$expect = 'The text for "foo."';
|
||||
$this->assertSame($translator->translate('FOO'), $expect);
|
||||
}
|
||||
|
||||
public function testIssue9Failure()
|
||||
{
|
||||
$package = new Package;
|
||||
$package->setMessages([
|
||||
'FOO' => 'The text for "foo."',
|
||||
'BAR' => 'The text for "bar."',
|
||||
]);
|
||||
// $this->packages->set('Vendor.Package', 'en_UK', $package);
|
||||
// $this->setExpectedException('Exception');
|
||||
// $translator = $this->translators->get('Vendor.Package', 'en_UK');
|
||||
}
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
namespace Aura\Intl;
|
||||
|
||||
class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $translator;
|
||||
|
||||
protected $factory;
|
||||
|
||||
protected $package;
|
||||
|
||||
protected $locale = 'en_US';
|
||||
|
||||
protected $messages = [
|
||||
'TEXT_FOO' => 'Foo text',
|
||||
'TEXT_BAR' => 'Bar text',
|
||||
];
|
||||
|
||||
protected $formatter;
|
||||
|
||||
protected function newTranslator(TranslatorInterface $fallback = null)
|
||||
{
|
||||
return $this->factory->newInstance(
|
||||
'en_US',
|
||||
$this->package,
|
||||
$this->formatter,
|
||||
$fallback
|
||||
);
|
||||
}
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->factory = new TranslatorFactory;
|
||||
$this->formatter = new MockFormatter;
|
||||
$this->package = new Package;
|
||||
$this->package->setMessages($this->messages);
|
||||
$this->translator = $this->newTranslator();
|
||||
}
|
||||
|
||||
public function testTranslate()
|
||||
{
|
||||
// key exists
|
||||
$expect = 'Foo text';
|
||||
$actual = $this->translator->translate('TEXT_FOO');
|
||||
$this->assertSame($expect, $actual);
|
||||
|
||||
// key exists, with tokens passed
|
||||
$expect = 'Foo text';
|
||||
$actual = $this->translator->translate('TEXT_FOO', ['foo' => 'bar']);
|
||||
$this->assertSame($expect, $actual);
|
||||
|
||||
// key does not exist
|
||||
$expect = 'TEXT_NONE';
|
||||
$actual = $this->translator->translate('TEXT_NONE');
|
||||
$this->assertSame($expect, $actual);
|
||||
}
|
||||
|
||||
public function testTranslate_fallback()
|
||||
{
|
||||
$package = new Package;
|
||||
$package->setMessages([
|
||||
'TEXT_NONE' => 'Fallback text',
|
||||
]);
|
||||
// create fallback translator
|
||||
$fallback = new Translator(
|
||||
'en_US',
|
||||
$package,
|
||||
$this->formatter
|
||||
);
|
||||
|
||||
// create primary translator with fallback
|
||||
$translator = $this->newTranslator($fallback);
|
||||
|
||||
// key does not exist in primary, but exists in fallback
|
||||
$expect = 'Fallback text';
|
||||
$actual = $translator->translate('TEXT_NONE');
|
||||
$this->assertSame($expect, $actual);
|
||||
}
|
||||
|
||||
public function testTranslateMissingKey()
|
||||
{
|
||||
// using getMockBuilder so we can use with phpunit 4.8 or 5+ without warnings
|
||||
$formatter = $this->getMockBuilder(get_class($this->formatter))
|
||||
->disableOriginalConstructor()
|
||||
->disableOriginalClone()
|
||||
->disableArgumentCloning()
|
||||
->getMock();
|
||||
// create fallback translator
|
||||
$translator = new Translator('en_US', new Package, $formatter);
|
||||
|
||||
$formatter->expects($this->once())
|
||||
->method('format')
|
||||
->with('en_US', 'TEXT', ['var' => 'SOME'])
|
||||
->will($this->returnValue('FORMATTED'));
|
||||
|
||||
// key does not exist, with tokens passed
|
||||
$expect = 'FORMATTED';
|
||||
$actual = $translator->translate('TEXT', ['var' => 'SOME']);
|
||||
$this->assertEquals($expect, $actual);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user