Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,345 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\Tests\Data\Bundle\Reader;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReader;
use Symfony\Component\Intl\Exception\ResourceBundleNotFoundException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class BundleEntryReaderTest extends TestCase
{
const RES_DIR = '/res/dir';
/**
* @var BundleEntryReader
*/
private $reader;
/**
* @var MockObject
*/
private $readerImpl;
private static $data = [
'Entries' => [
'Foo' => 'Bar',
'Bar' => 'Baz',
],
'Foo' => 'Bar',
'Version' => '2.0',
];
private static $fallbackData = [
'Entries' => [
'Foo' => 'Foo',
'Bam' => 'Lah',
],
'Baz' => 'Foo',
'Version' => '1.0',
];
private static $mergedData = [
// no recursive merging -> too complicated
'Entries' => [
'Foo' => 'Bar',
'Bar' => 'Baz',
],
'Baz' => 'Foo',
'Version' => '2.0',
'Foo' => 'Bar',
];
protected function setUp()
{
$this->readerImpl = $this->getMockBuilder('Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface')->getMock();
$this->reader = new BundleEntryReader($this->readerImpl);
}
public function testForwardCallToRead()
{
$this->readerImpl->expects($this->once())
->method('read')
->with(self::RES_DIR, 'root')
->willReturn(self::$data);
$this->assertSame(self::$data, $this->reader->read(self::RES_DIR, 'root'));
}
public function testReadEntireDataFileIfNoIndicesGiven()
{
$this->readerImpl->expects($this->exactly(2))
->method('read')
->withConsecutive(
[self::RES_DIR, 'en'],
[self::RES_DIR, 'root']
)
->willReturnOnConsecutiveCalls(self::$data, self::$fallbackData);
$this->assertSame(self::$mergedData, $this->reader->readEntry(self::RES_DIR, 'en', []));
}
public function testReadExistingEntry()
{
$this->readerImpl->expects($this->once())
->method('read')
->with(self::RES_DIR, 'root')
->willReturn(self::$data);
$this->assertSame('Bar', $this->reader->readEntry(self::RES_DIR, 'root', ['Entries', 'Foo']));
}
public function testReadNonExistingEntry()
{
$this->expectException('Symfony\Component\Intl\Exception\MissingResourceException');
$this->readerImpl->expects($this->once())
->method('read')
->with(self::RES_DIR, 'root')
->willReturn(self::$data);
$this->reader->readEntry(self::RES_DIR, 'root', ['Entries', 'NonExisting']);
}
public function testFallbackIfEntryDoesNotExist()
{
$this->readerImpl->expects($this->exactly(2))
->method('read')
->withConsecutive(
[self::RES_DIR, 'en_GB'],
[self::RES_DIR, 'en']
)
->willReturnOnConsecutiveCalls(self::$data, self::$fallbackData);
$this->assertSame('Lah', $this->reader->readEntry(self::RES_DIR, 'en_GB', ['Entries', 'Bam']));
}
public function testDontFallbackIfEntryDoesNotExistAndFallbackDisabled()
{
$this->expectException('Symfony\Component\Intl\Exception\MissingResourceException');
$this->readerImpl->expects($this->once())
->method('read')
->with(self::RES_DIR, 'en_GB')
->willReturn(self::$data);
$this->reader->readEntry(self::RES_DIR, 'en_GB', ['Entries', 'Bam'], false);
}
public function testFallbackIfLocaleDoesNotExist()
{
$this->readerImpl->expects($this->exactly(2))
->method('read')
->withConsecutive(
[self::RES_DIR, 'en_GB'],
[self::RES_DIR, 'en']
)
->willReturnOnConsecutiveCalls(
$this->throwException(new ResourceBundleNotFoundException()),
self::$fallbackData
);
$this->assertSame('Lah', $this->reader->readEntry(self::RES_DIR, 'en_GB', ['Entries', 'Bam']));
}
public function testDontFallbackIfLocaleDoesNotExistAndFallbackDisabled()
{
$this->expectException('Symfony\Component\Intl\Exception\MissingResourceException');
$this->readerImpl->expects($this->once())
->method('read')
->with(self::RES_DIR, 'en_GB')
->willThrowException(new ResourceBundleNotFoundException());
$this->reader->readEntry(self::RES_DIR, 'en_GB', ['Entries', 'Bam'], false);
}
public function provideMergeableValues()
{
return [
['foo', null, 'foo'],
[null, 'foo', 'foo'],
[['foo', 'bar'], null, ['foo', 'bar']],
[['foo', 'bar'], [], ['foo', 'bar']],
[null, ['baz'], ['baz']],
[[], ['baz'], ['baz']],
[['foo', 'bar'], ['baz'], ['baz', 'foo', 'bar']],
];
}
/**
* @dataProvider provideMergeableValues
*/
public function testMergeDataWithFallbackData($childData, $parentData, $result)
{
if (null === $childData || \is_array($childData)) {
$this->readerImpl->expects($this->exactly(2))
->method('read')
->withConsecutive(
[self::RES_DIR, 'en'],
[self::RES_DIR, 'root']
)
->willReturnOnConsecutiveCalls($childData, $parentData);
} else {
$this->readerImpl->expects($this->once())
->method('read')
->with(self::RES_DIR, 'en')
->willReturn($childData);
}
$this->assertSame($result, $this->reader->readEntry(self::RES_DIR, 'en', [], true));
}
/**
* @dataProvider provideMergeableValues
*/
public function testDontMergeDataIfFallbackDisabled($childData, $parentData, $result)
{
$this->readerImpl->expects($this->once())
->method('read')
->with(self::RES_DIR, 'en_GB')
->willReturn($childData);
$this->assertSame($childData, $this->reader->readEntry(self::RES_DIR, 'en_GB', [], false));
}
/**
* @dataProvider provideMergeableValues
*/
public function testMergeExistingEntryWithExistingFallbackEntry($childData, $parentData, $result)
{
if (null === $childData || \is_array($childData)) {
$this->readerImpl->expects($this->exactly(2))
->method('read')
->withConsecutive(
[self::RES_DIR, 'en'],
[self::RES_DIR, 'root']
)
->willReturnOnConsecutiveCalls(
['Foo' => ['Bar' => $childData]],
['Foo' => ['Bar' => $parentData]]
);
} else {
$this->readerImpl->expects($this->once())
->method('read')
->with(self::RES_DIR, 'en')
->willReturn(['Foo' => ['Bar' => $childData]]);
}
$this->assertSame($result, $this->reader->readEntry(self::RES_DIR, 'en', ['Foo', 'Bar'], true));
}
/**
* @dataProvider provideMergeableValues
*/
public function testMergeNonExistingEntryWithExistingFallbackEntry($childData, $parentData, $result)
{
$this->readerImpl
->method('read')
->withConsecutive(
[self::RES_DIR, 'en_GB'],
[self::RES_DIR, 'en']
)
->willReturnOnConsecutiveCalls(['Foo' => 'Baz'], ['Foo' => ['Bar' => $parentData]]);
$this->assertSame($parentData, $this->reader->readEntry(self::RES_DIR, 'en_GB', ['Foo', 'Bar'], true));
}
/**
* @dataProvider provideMergeableValues
*/
public function testMergeExistingEntryWithNonExistingFallbackEntry($childData, $parentData, $result)
{
if (null === $childData || \is_array($childData)) {
$this->readerImpl
->method('read')
->withConsecutive(
[self::RES_DIR, 'en_GB'],
[self::RES_DIR, 'en']
)
->willReturnOnConsecutiveCalls(['Foo' => ['Bar' => $childData]], ['Foo' => 'Bar']);
} else {
$this->readerImpl->expects($this->once())
->method('read')
->with(self::RES_DIR, 'en_GB')
->willReturn(['Foo' => ['Bar' => $childData]]);
}
$this->assertSame($childData, $this->reader->readEntry(self::RES_DIR, 'en_GB', ['Foo', 'Bar'], true));
}
public function testFailIfEntryFoundNeitherInParentNorChild()
{
$this->expectException('Symfony\Component\Intl\Exception\MissingResourceException');
$this->readerImpl
->method('read')
->withConsecutive(
[self::RES_DIR, 'en_GB'],
[self::RES_DIR, 'en']
)
->willReturnOnConsecutiveCalls(['Foo' => 'Baz'], ['Foo' => 'Bar']);
$this->reader->readEntry(self::RES_DIR, 'en_GB', ['Foo', 'Bar'], true);
}
/**
* @dataProvider provideMergeableValues
*/
public function testMergeTraversables($childData, $parentData, $result)
{
$parentData = \is_array($parentData) ? new \ArrayObject($parentData) : $parentData;
$childData = \is_array($childData) ? new \ArrayObject($childData) : $childData;
if (null === $childData || $childData instanceof \ArrayObject) {
$this->readerImpl
->method('read')
->withConsecutive(
[self::RES_DIR, 'en_GB'],
[self::RES_DIR, 'en']
)
->willReturnOnConsecutiveCalls(['Foo' => ['Bar' => $childData]], ['Foo' => ['Bar' => $parentData]]);
} else {
$this->readerImpl->expects($this->once())
->method('read')
->with(self::RES_DIR, 'en_GB')
->willReturn(['Foo' => ['Bar' => $childData]]);
}
$this->assertSame($result, $this->reader->readEntry(self::RES_DIR, 'en_GB', ['Foo', 'Bar'], true));
}
/**
* @dataProvider provideMergeableValues
*/
public function testFollowLocaleAliases($childData, $parentData, $result)
{
$this->reader->setLocaleAliases(['mo' => 'ro_MD']);
if (null === $childData || \is_array($childData)) {
$this->readerImpl
->method('read')
->withConsecutive(
[self::RES_DIR, 'ro_MD'],
// Read fallback locale of aliased locale ("ro_MD" -> "ro")
[self::RES_DIR, 'ro']
)
->willReturnOnConsecutiveCalls(['Foo' => ['Bar' => $childData]], ['Foo' => ['Bar' => $parentData]]);
} else {
$this->readerImpl->expects($this->once())
->method('read')
->with(self::RES_DIR, 'ro_MD')
->willReturn(['Foo' => ['Bar' => $childData]]);
}
$this->assertSame($result, $this->reader->readEntry(self::RES_DIR, 'mo', ['Foo', 'Bar'], true));
}
}

View File

@@ -0,0 +1,17 @@
#!/bin/bash
if [ -z "$ICU_BUILD_DIR" ]; then
echo "Please set the ICU_BUILD_DIR environment variable"
exit
fi
if [ ! -d "$ICU_BUILD_DIR" ]; then
echo "The directory $ICU_BUILD_DIR pointed at by ICU_BUILD_DIR does not exist"
exit
fi
DIR=`dirname $0`
rm $DIR/res/*.res
LD_LIBRARY_PATH=$ICU_BUILD_DIR/lib $ICU_BUILD_DIR/bin/genrb -d $DIR/res $DIR/txt/*.txt

View File

@@ -0,0 +1,14 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
return [
'Foo' => 'Bar',
];

Binary file not shown.

View File

@@ -0,0 +1,3 @@
en{
Foo{"Bar"}
}

View File

@@ -0,0 +1 @@
{"Foo":"Bar"}

View File

@@ -0,0 +1,14 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
return [
'Foo' => 'Bar',
];

View File

@@ -0,0 +1 @@
{"Foo":"Bar"}

View File

@@ -0,0 +1 @@
{"Foobar"}

View File

@@ -0,0 +1,14 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
return [
'Foo' => 'Bar',
];

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,3 @@
alias{
"%%ALIAS"{"ro"}
}

View File

@@ -0,0 +1,3 @@
mo{
"%%ALIAS"{"ro_MD"}
}

View File

@@ -0,0 +1,3 @@
ro{
Foo{"Bar"}
}

View File

@@ -0,0 +1,3 @@
ro_MD{
Baz{"Bam"}
}

View File

@@ -0,0 +1,6 @@
root{
/**
* so genrb doesn't issue warnings
*/
___{""}
}

View File

@@ -0,0 +1,101 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\Tests\Data\Bundle\Reader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Intl\Data\Bundle\Reader\IntlBundleReader;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
* @requires extension intl
*/
class IntlBundleReaderTest extends TestCase
{
/**
* @var IntlBundleReader
*/
private $reader;
protected function setUp()
{
$this->reader = new IntlBundleReader();
}
public function testReadReturnsArrayAccess()
{
$data = $this->reader->read(__DIR__.'/Fixtures/res', 'ro');
$this->assertInstanceOf('\ArrayAccess', $data);
$this->assertSame('Bar', $data['Foo']);
$this->assertArrayNotHasKey('ExistsNot', $data);
}
public function testReadFollowsAlias()
{
// "alias" = "ro"
$data = $this->reader->read(__DIR__.'/Fixtures/res', 'alias');
$this->assertInstanceOf('\ArrayAccess', $data);
$this->assertSame('Bar', $data['Foo']);
$this->assertArrayNotHasKey('ExistsNot', $data);
}
public function testReadDoesNotFollowFallback()
{
if (\defined('HHVM_VERSION')) {
$this->markTestSkipped('ResourceBundle does not support disabling fallback properly on HHVM.');
}
// "ro_MD" -> "ro"
$data = $this->reader->read(__DIR__.'/Fixtures/res', 'ro_MD');
$this->assertInstanceOf('\ArrayAccess', $data);
$this->assertSame('Bam', $data['Baz']);
$this->assertArrayNotHasKey('Foo', $data);
$this->assertNull($data['Foo']);
$this->assertArrayNotHasKey('ExistsNot', $data);
}
public function testReadDoesNotFollowFallbackAlias()
{
if (\defined('HHVM_VERSION')) {
$this->markTestSkipped('ResourceBundle does not support disabling fallback properly on HHVM.');
}
// "mo" = "ro_MD" -> "ro"
$data = $this->reader->read(__DIR__.'/Fixtures/res', 'mo');
$this->assertInstanceOf('\ArrayAccess', $data);
$this->assertSame('Bam', $data['Baz'], 'data from the aliased locale can be accessed');
$this->assertArrayNotHasKey('Foo', $data);
$this->assertNull($data['Foo']);
$this->assertArrayNotHasKey('ExistsNot', $data);
}
public function testReadFailsIfNonExistingLocale()
{
$this->expectException('Symfony\Component\Intl\Exception\ResourceBundleNotFoundException');
$this->reader->read(__DIR__.'/Fixtures/res', 'foo');
}
public function testReadFailsIfNonExistingFallbackLocale()
{
$this->expectException('Symfony\Component\Intl\Exception\ResourceBundleNotFoundException');
$this->reader->read(__DIR__.'/Fixtures/res', 'ro_AT');
}
public function testReadFailsIfNonExistingDirectory()
{
$this->expectException('Symfony\Component\Intl\Exception\RuntimeException');
$this->reader->read(__DIR__.'/foo', 'ro');
}
}

View File

@@ -0,0 +1,70 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\Tests\Data\Bundle\Reader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Intl\Data\Bundle\Reader\JsonBundleReader;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class JsonBundleReaderTest extends TestCase
{
/**
* @var JsonBundleReader
*/
private $reader;
protected function setUp()
{
$this->reader = new JsonBundleReader();
}
public function testReadReturnsArray()
{
$data = $this->reader->read(__DIR__.'/Fixtures/json', 'en');
$this->assertIsArray($data);
$this->assertSame('Bar', $data['Foo']);
$this->assertArrayNotHasKey('ExistsNot', $data);
}
public function testReadFailsIfNonExistingLocale()
{
$this->expectException('Symfony\Component\Intl\Exception\ResourceBundleNotFoundException');
$this->reader->read(__DIR__.'/Fixtures/json', 'foo');
}
public function testReadFailsIfNonExistingDirectory()
{
$this->expectException('Symfony\Component\Intl\Exception\RuntimeException');
$this->reader->read(__DIR__.'/foo', 'en');
}
public function testReadFailsIfNotAFile()
{
$this->expectException('Symfony\Component\Intl\Exception\RuntimeException');
$this->reader->read(__DIR__.'/Fixtures/NotAFile', 'en');
}
public function testReadFailsIfInvalidJson()
{
$this->expectException('Symfony\Component\Intl\Exception\RuntimeException');
$this->reader->read(__DIR__.'/Fixtures/json', 'en_Invalid');
}
public function testReaderDoesNotBreakOutOfGivenPath()
{
$this->expectException('Symfony\Component\Intl\Exception\ResourceBundleNotFoundException');
$this->reader->read(__DIR__.'/Fixtures/json', '../invalid_directory/en');
}
}

View File

@@ -0,0 +1,64 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\Tests\Data\Bundle\Reader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Intl\Data\Bundle\Reader\PhpBundleReader;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class PhpBundleReaderTest extends TestCase
{
/**
* @var PhpBundleReader
*/
private $reader;
protected function setUp()
{
$this->reader = new PhpBundleReader();
}
public function testReadReturnsArray()
{
$data = $this->reader->read(__DIR__.'/Fixtures/php', 'en');
$this->assertIsArray($data);
$this->assertSame('Bar', $data['Foo']);
$this->assertArrayNotHasKey('ExistsNot', $data);
}
public function testReadFailsIfNonExistingLocale()
{
$this->expectException('Symfony\Component\Intl\Exception\ResourceBundleNotFoundException');
$this->reader->read(__DIR__.'/Fixtures/php', 'foo');
}
public function testReadFailsIfNonExistingDirectory()
{
$this->expectException('Symfony\Component\Intl\Exception\RuntimeException');
$this->reader->read(__DIR__.'/foo', 'en');
}
public function testReadFailsIfNotAFile()
{
$this->expectException('Symfony\Component\Intl\Exception\RuntimeException');
$this->reader->read(__DIR__.'/Fixtures/NotAFile', 'en');
}
public function testReaderDoesNotBreakOutOfGivenPath()
{
$this->expectException('Symfony\Component\Intl\Exception\ResourceBundleNotFoundException');
$this->reader->read(__DIR__.'/Fixtures/php', '../invalid_directory/en');
}
}

View File

@@ -0,0 +1,15 @@
{
"Entry1": {
"Array": [
"foo",
"bar"
],
"Integer": 5,
"Boolean": false,
"Float": 1.23
},
"Entry2": "String",
"Traversable": {
"Foo": "Bar"
}
}

View File

@@ -0,0 +1,17 @@
<?php
return [
'Entry1' => [
'Array' => [
0 => 'foo',
1 => 'bar',
],
'Integer' => 5,
'Boolean' => false,
'Float' => 1.23,
],
'Entry2' => 'String',
'Traversable' => [
'Foo' => 'Bar',
],
];

Binary file not shown.

View File

@@ -0,0 +1,39 @@
en{
Entry1{
Array{
"foo",
"bar",
{
Key{"value"}
},
}
Integer:int{5}
IntVector:intvector{
0,
1,
2,
3,
}
NotAnIntVector{
0:int{0}
2:int{1}
1:int{2}
3:int{3}
}
IntVectorWithStringKeys{
a:int{0}
b:int{1}
c:int{2}
}
TableWithIntKeys{
0:int{0}
1:int{1}
3:int{3}
}
FalseBoolean{"false"}
TrueBoolean{"true"}
Null{""}
Float{"1.23"}
}
Entry2{"String"}
}

View File

@@ -0,0 +1,3 @@
en_nofallback:table(nofallback){
Entry{"Value"}
}

View File

@@ -0,0 +1,4 @@
escaped{
"EntryWith:Colon"{"Value"}
"Entry With Spaces"{"Value"}
}

View File

@@ -0,0 +1,5 @@
{
"Entry": {
"NestedEntry": "Value"
}
}

View File

@@ -0,0 +1,7 @@
<?php
return [
'Entry' => [
'NestedEntry' => 'Value',
],
];

Binary file not shown.

View File

@@ -0,0 +1,5 @@
rb{
Entry{
NestedEntry{"Value"}
}
}

View File

@@ -0,0 +1,78 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\Tests\Data\Bundle\Writer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Intl\Data\Bundle\Writer\JsonBundleWriter;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class JsonBundleWriterTest extends TestCase
{
/**
* @var JsonBundleWriter
*/
private $writer;
private $directory;
/**
* @var Filesystem
*/
private $filesystem;
protected function setUp()
{
$this->writer = new JsonBundleWriter();
$this->directory = sys_get_temp_dir().'/JsonBundleWriterTest/'.mt_rand(1000, 9999);
$this->filesystem = new Filesystem();
$this->filesystem->mkdir($this->directory);
}
protected function tearDown()
{
$this->filesystem->remove($this->directory);
}
public function testWrite()
{
$this->writer->write($this->directory, 'en', [
'Entry1' => [
'Array' => ['foo', 'bar'],
'Integer' => 5,
'Boolean' => false,
'Float' => 1.23,
],
'Entry2' => 'String',
'Traversable' => new \ArrayIterator([
'Foo' => 'Bar',
]),
]);
$this->assertFileEquals(__DIR__.'/Fixtures/en.json', $this->directory.'/en.json');
}
/**
* @requires extension intl
*/
public function testWriteResourceBundle()
{
$bundle = new \ResourceBundle('rb', __DIR__.'/Fixtures', false);
$this->writer->write($this->directory, 'en', $bundle);
$this->assertFileEquals(__DIR__.'/Fixtures/rb.json', $this->directory.'/en.json');
}
}

View File

@@ -0,0 +1,78 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\Tests\Data\Bundle\Writer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Intl\Data\Bundle\Writer\PhpBundleWriter;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class PhpBundleWriterTest extends TestCase
{
/**
* @var PhpBundleWriter
*/
private $writer;
private $directory;
/**
* @var Filesystem
*/
private $filesystem;
protected function setUp()
{
$this->writer = new PhpBundleWriter();
$this->directory = sys_get_temp_dir().'/PhpBundleWriterTest/'.mt_rand(1000, 9999);
$this->filesystem = new Filesystem();
$this->filesystem->mkdir($this->directory);
}
protected function tearDown()
{
$this->filesystem->remove($this->directory);
}
public function testWrite()
{
$this->writer->write($this->directory, 'en', [
'Entry1' => [
'Array' => ['foo', 'bar'],
'Integer' => 5,
'Boolean' => false,
'Float' => 1.23,
],
'Entry2' => 'String',
'Traversable' => new \ArrayIterator([
'Foo' => 'Bar',
]),
]);
$this->assertFileEquals(__DIR__.'/Fixtures/en.php', $this->directory.'/en.php');
}
/**
* @requires extension intl
*/
public function testWriteResourceBundle()
{
$bundle = new \ResourceBundle('rb', __DIR__.'/Fixtures', false);
$this->writer->write($this->directory, 'en', $bundle);
$this->assertFileEquals(__DIR__.'/Fixtures/rb.php', $this->directory.'/en.php');
}
}

View File

@@ -0,0 +1,116 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\Tests\Data\Bundle\Writer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Intl\Data\Bundle\Writer\TextBundleWriter;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
*/
class TextBundleWriterTest extends TestCase
{
/**
* @var TextBundleWriter
*/
private $writer;
private $directory;
/**
* @var Filesystem
*/
private $filesystem;
protected function setUp()
{
$this->writer = new TextBundleWriter();
$this->directory = sys_get_temp_dir().'/TextBundleWriterTest/'.mt_rand(1000, 9999);
$this->filesystem = new Filesystem();
$this->filesystem->mkdir($this->directory);
}
protected function tearDown()
{
$this->filesystem->remove($this->directory);
}
public function testWrite()
{
$this->writer->write($this->directory, 'en', [
'Entry1' => [
'Array' => ['foo', 'bar', ['Key' => 'value']],
'Integer' => 5,
'IntVector' => [0, 1, 2, 3],
'NotAnIntVector' => [0 => 0, 2 => 1, 1 => 2, 3 => 3],
'IntVectorWithStringKeys' => ['a' => 0, 'b' => 1, 'c' => 2],
'TableWithIntKeys' => [0 => 0, 1 => 1, 3 => 3],
'FalseBoolean' => false,
'TrueBoolean' => true,
'Null' => null,
'Float' => 1.23,
],
'Entry2' => 'String',
]);
$this->assertFileEquals(__DIR__.'/Fixtures/en.txt', $this->directory.'/en.txt');
}
public function testWriteTraversable()
{
$this->writer->write($this->directory, 'en', new \ArrayIterator([
'Entry1' => new \ArrayIterator([
'Array' => ['foo', 'bar', ['Key' => 'value']],
'Integer' => 5,
'IntVector' => [0, 1, 2, 3],
'NotAnIntVector' => [0 => 0, 2 => 1, 1 => 2, 3 => 3],
'IntVectorWithStringKeys' => ['a' => 0, 'b' => 1, 'c' => 2],
'TableWithIntKeys' => [0 => 0, 1 => 1, 3 => 3],
'FalseBoolean' => false,
'TrueBoolean' => true,
'Null' => null,
'Float' => 1.23,
]),
'Entry2' => 'String',
]));
$this->assertFileEquals(__DIR__.'/Fixtures/en.txt', $this->directory.'/en.txt');
}
public function testWriteNoFallback()
{
$data = [
'Entry' => 'Value',
];
$this->writer->write($this->directory, 'en_nofallback', $data, $fallback = false);
$this->assertFileEquals(__DIR__.'/Fixtures/en_nofallback.txt', $this->directory.'/en_nofallback.txt');
}
public function testEscapeKeysIfNecessary()
{
$this->writer->write($this->directory, 'escaped', [
// Keys with colons must be escaped, otherwise the part after the
// colon is interpreted as resource type
'EntryWith:Colon' => 'Value',
// Keys with spaces must be escaped
'Entry With Spaces' => 'Value',
]);
$this->assertFileEquals(__DIR__.'/Fixtures/escaped.txt', $this->directory.'/escaped.txt');
}
}