Skip to content

Commit 17b09c5

Browse files
authored
Added some more tests (#11)
* Adding tests for FileStorage * Moved around fixture file * Removed code not used * Added tests for XliffLoader * Applied changes from StyleCI * Allow PHP 5.5 * Removed PHP7 * Fixed sf 2.7 tests * Fixed HHVM build
1 parent 659b147 commit 17b09c5

File tree

9 files changed

+240
-44
lines changed

9 files changed

+240
-44
lines changed

.travis.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,24 @@ php:
1414
- 5.6
1515
- 7.0
1616
- 7.1
17-
- hhvm
17+
1818
env:
1919
global:
2020
- TEST_COMMAND="composer test"
2121
matrix:
22-
- SYMFONY_VERSION=3.2.*
22+
- SYMFONY_VERSION=3.3.*
2323
- SYMFONY_VERSION=2.7.*
2424

2525
matrix:
2626
fast_finish: true
2727
include:
2828
- php: 5.5
29-
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci" SYMFONY_VERSION=2.7.*
29+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci" SYMFONY_VERSION=2.8.*
30+
- php: 7.0
31+
env: SYMFONY_VERSION=3.0.*
32+
- php: hhvm
33+
dist: trusty
34+
env: SYMFONY_VERSION=2.8.*
3035

3136
install:
3237
- composer require symfony/symfony:${SYMFONY_VERSION} --no-update

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
"php": "^5.5 || ^7.0",
1313
"php-translation/common": "^0.2.1",
1414
"symfony/translation": "^2.7 || ^3.0",
15-
"nyholm/nsa": "^1.0.1"
15+
"nyholm/nsa": "^1.1"
1616
},
1717
"require-dev": {
18-
"phpunit/phpunit": "^4.5 || ^5.4",
18+
"phpunit/phpunit": "^4.8.36 || ^5.5 || ^6.2",
1919
"symfony/framework-bundle": "^2.7 || ^3.0"
2020
},
2121
"autoload": {

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<directory suffix=".php">./</directory>
2626
<exclude>
2727
<directory>vendor</directory>
28-
<directory>Tests</directory>
28+
<directory>tests</directory>
2929
</exclude>
3030
</whitelist>
3131
</filter>

src/Loader/XliffLoader.php

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,13 @@ public function extractFromContent($content, MessageCatalogue $catalogue, $domai
6161
* @author Fabien Potencier <fabien@symfony.com>
6262
* @author Martin Hasoň <martin.hason@gmail.com>
6363
*
64-
* @param string $content An XML file path
65-
* @param string|callable|null $schemaOrCallable An XSD schema file path, a callable, or null to disable validation
64+
* @param string $content An XML file path
6665
*
6766
* @return \DOMDocument
6867
*
6968
* @throws \InvalidArgumentException When loading of XML file returns error
7069
*/
71-
private function loadFileContent($content, $schemaOrCallable = null)
70+
private function loadFileContent($content)
7271
{
7372
if ('' === trim($content)) {
7473
throw new \InvalidArgumentException('Content does not contain valid XML, it is empty.');
@@ -97,35 +96,6 @@ private function loadFileContent($content, $schemaOrCallable = null)
9796
}
9897
}
9998

100-
if (null !== $schemaOrCallable) {
101-
$internalErrors = libxml_use_internal_errors(true);
102-
libxml_clear_errors();
103-
104-
$e = null;
105-
if (is_callable($schemaOrCallable)) {
106-
try {
107-
$valid = call_user_func($schemaOrCallable, $dom, $internalErrors);
108-
} catch (\Exception $e) {
109-
$valid = false;
110-
}
111-
} elseif (!is_array($schemaOrCallable) && is_file((string) $schemaOrCallable)) {
112-
$schemaSource = file_get_contents((string) $schemaOrCallable);
113-
$valid = @$dom->schemaValidateSource($schemaSource);
114-
} else {
115-
libxml_use_internal_errors($internalErrors);
116-
117-
throw new \InvalidArgumentException('The schemaOrCallable argument has to be a valid path to XSD file or callable.');
118-
}
119-
120-
if (!$valid) {
121-
$messages = $this->getXmlErrors($internalErrors);
122-
if (empty($messages)) {
123-
$messages = ['The XML is not valid.'];
124-
}
125-
throw new \InvalidArgumentException(implode("\n", $messages), 0, $e);
126-
}
127-
}
128-
12999
libxml_clear_errors();
130100
libxml_use_internal_errors($internalErrors);
131101

tests/.gitkeep

Whitespace-only changes.
File renamed without changes.

tests/Unit/FileStorageTest.php

Lines changed: 129 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Translation\SymfonyStorage\Tests\Unit;
1313

14+
use PHPUnit\Framework\TestCase;
1415
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
16+
use Symfony\Component\Translation\MessageCatalogue;
1517
use Symfony\Component\Translation\MessageCatalogueInterface;
1618
use Symfony\Component\Translation\Writer\TranslationWriter;
1719
use Translation\Common\Model\Message;
@@ -21,11 +23,12 @@
2123
/**
2224
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2325
*/
24-
class FileStorageTest extends \PHPUnit_Framework_TestCase
26+
class FileStorageTest extends TestCase
2527
{
2628
public function testConstructor()
2729
{
28-
new FileStorage(new TranslationWriter(), new TranslationLoader(), ['foo']);
30+
$storage = new FileStorage(new TranslationWriter(), new TranslationLoader(), ['foo']);
31+
$this->assertInstanceOf(FileStorage::class, $storage);
2932
}
3033

3134
/**
@@ -88,13 +91,135 @@ public function testCreateExistingCatalogue()
8891
->with(
8992
$this->isInstanceOf(MessageCatalogueInterface::class),
9093
'xlf',
91-
['path' => __DIR__]
94+
['path' => $this->getFixturePath()]
9295
);
9396

9497
$loader = new TranslationLoader();
9598
$loader->addLoader('xlf', new XliffLoader());
96-
$storage = new FileStorage($writer, $loader, ['foo', __DIR__]);
99+
$storage = new FileStorage($writer, $loader, ['foo', $this->getFixturePath()]);
97100

98101
$storage->create(new Message('key', 'messages', 'en', 'Translation'));
99102
}
103+
104+
public function testGet()
105+
{
106+
$writer = $this->getMockBuilder(TranslationWriter::class)
107+
->disableOriginalConstructor()
108+
->getMock();
109+
110+
$loader = new TranslationLoader();
111+
$loader->addLoader('xlf', new XliffLoader());
112+
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]);
113+
114+
$this->assertEquals('Bazbar', $storage->get('en', 'messages', 'test_1')->getTranslation());
115+
116+
// Missing locale
117+
$this->assertEquals('test_1', $storage->get('sv', 'messages', 'test_1')->getTranslation());
118+
119+
// Missing domain
120+
$this->assertEquals('test_1', $storage->get('en', 'xx', 'test_1')->getTranslation());
121+
122+
// Missing key
123+
$this->assertEquals('miss', $storage->get('en', 'messages', 'miss')->getTranslation());
124+
}
125+
126+
public function testUpdate()
127+
{
128+
$writer = $this->getMockBuilder(TranslationWriter::class)
129+
->setMethods(['writeTranslations'])
130+
->disableOriginalConstructor()
131+
->getMock();
132+
$writer->expects($this->exactly(2))
133+
->method('writeTranslations')
134+
->with(
135+
$this->isInstanceOf(MessageCatalogueInterface::class),
136+
'xlf',
137+
['path' => $this->getFixturePath()]
138+
);
139+
140+
$loader = new TranslationLoader();
141+
$loader->addLoader('xlf', new XliffLoader());
142+
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]);
143+
144+
$storage->update(new Message('key', 'messages', 'en', 'Translation'));
145+
$storage->update(new Message('test_1', 'messages', 'en', 'Translation'));
146+
}
147+
148+
public function testDelete()
149+
{
150+
$writer = $this->getMockBuilder(TranslationWriter::class)
151+
->setMethods(['writeTranslations'])
152+
->disableOriginalConstructor()
153+
->getMock();
154+
155+
$writer->expects($this->once())
156+
->method('writeTranslations')
157+
->with(
158+
$this->callback(function (MessageCatalogueInterface $catalogue) {
159+
return !$catalogue->defines('test_0', 'messages');
160+
}),
161+
'xlf',
162+
['path' => $this->getFixturePath()]
163+
);
164+
165+
$loader = new TranslationLoader();
166+
$loader->addLoader('xlf', new XliffLoader());
167+
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]);
168+
169+
$storage->delete('en', 'messages', 'test_0');
170+
}
171+
172+
public function testImport()
173+
{
174+
$writer = $this->getMockBuilder(TranslationWriter::class)
175+
->setMethods(['writeTranslations'])
176+
->disableOriginalConstructor()
177+
->getMock();
178+
179+
$writer->expects($this->once())
180+
->method('writeTranslations')
181+
->with(
182+
$this->callback(function (MessageCatalogueInterface $catalogue) {
183+
return $catalogue->defines('test_4711', 'messages');
184+
}),
185+
'xlf',
186+
['path' => $this->getFixturePath()]
187+
);
188+
189+
$loader = new TranslationLoader();
190+
$loader->addLoader('xlf', new XliffLoader());
191+
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]);
192+
$catalogue = new MessageCatalogue('en', ['messages' => ['test_4711' => 'foobar']]);
193+
194+
$storage->import($catalogue);
195+
}
196+
197+
public function testExport()
198+
{
199+
$writer = $this->getMockBuilder(TranslationWriter::class)
200+
->disableOriginalConstructor()
201+
->getMock();
202+
203+
$loader = new TranslationLoader();
204+
$loader->addLoader('xlf', new XliffLoader());
205+
$storage = new FileStorage($writer, $loader, [$this->getFixturePath()]);
206+
207+
$catalogue = new MessageCatalogue('en');
208+
$storage->export($catalogue);
209+
$this->assertTrue($catalogue->defines('test_0', 'messages'));
210+
$this->assertTrue($catalogue->defines('test_1', 'messages'));
211+
212+
// Wrong locale
213+
$catalogue = new MessageCatalogue('sv');
214+
$storage->export($catalogue);
215+
$this->assertFalse($catalogue->defines('test_0', 'messages'));
216+
}
217+
218+
/**
219+
* @return string
220+
*/
221+
private function getFixturePath()
222+
{
223+
return realpath(__DIR__.'/../Fixtures/single-file');
224+
}
100225
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\SymfonyStorage\Tests\Unit\Loader;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpKernel\Kernel;
16+
use Symfony\Component\Translation\Exception\InvalidResourceException;
17+
use Symfony\Component\Translation\MessageCatalogue;
18+
use Translation\SymfonyStorage\Loader\XliffLoader;
19+
20+
/**
21+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
22+
*/
23+
class XliffLoaderTest extends TestCase
24+
{
25+
/**
26+
* @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
27+
*/
28+
public function testEmptyContent()
29+
{
30+
$loader = new XliffLoader();
31+
$loader->extractFromContent(' ', new MessageCatalogue('en'), 'messages');
32+
}
33+
34+
public function testInvalidContent()
35+
{
36+
$loader = new XliffLoader();
37+
38+
try {
39+
$loader->extractFromContent('Foobar', new MessageCatalogue('en'), 'messages');
40+
} catch (InvalidResourceException $e) {
41+
$invalidArgument = $e->getPrevious();
42+
$this->assertNotNull($invalidArgument);
43+
$this->assertContains('[ERROR 4] Start tag expected', $invalidArgument->getMessage());
44+
45+
return;
46+
}
47+
$this->fail('XliffLoader must throw exception on invalid XML');
48+
}
49+
50+
public function testXliff12()
51+
{
52+
if (Kernel::VERSION_ID < 20800) {
53+
$this->markTestSkipped('Symfony <2.8 is not supported. ');
54+
}
55+
56+
$content = file_get_contents(__DIR__.'/../../Fixtures/single-file/messages.en.xlf');
57+
$catalogue = new MessageCatalogue('en');
58+
(new XliffLoader())->extractFromContent($content, $catalogue, 'messages');
59+
$this->assertTrue($catalogue->defines('test_0'));
60+
$this->assertTrue($catalogue->defines('test_1'));
61+
}
62+
63+
public function testXliff20()
64+
{
65+
if (Kernel::VERSION_ID < 20800) {
66+
$this->markTestSkipped('Symfony <2.8 is not supported. ');
67+
}
68+
69+
$content = <<<'XML'
70+
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0"
71+
srcLang="en-US" trgLang="sv">
72+
<file id="f1" original="Example">
73+
<skeleton href="Example"/>
74+
<unit id="1">
75+
<segment>
76+
<source>key0</source>
77+
<target>Foo</target>
78+
</segment>
79+
</unit>
80+
<unit id="2">
81+
<segment>
82+
<source>key1</source>
83+
<target>Bar</target>
84+
</segment>
85+
</unit>
86+
</file>
87+
</xliff>
88+
XML;
89+
90+
$catalogue = new MessageCatalogue('en');
91+
(new XliffLoader())->extractFromContent($content, $catalogue, 'messages');
92+
$this->assertTrue($catalogue->defines('key0'));
93+
$this->assertTrue($catalogue->defines('key1'));
94+
}
95+
}

tests/Unit/XliffConverterTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@
1111

1212
namespace Translation\SymfonyStorage\Tests\Unit;
1313

14+
use PHPUnit\Framework\TestCase;
1415
use Symfony\Component\HttpKernel\Kernel;
1516
use Symfony\Component\Translation\MessageCatalogue;
1617
use Translation\SymfonyStorage\XliffConverter;
1718

1819
/**
1920
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2021
*/
21-
class XliffConverterTest extends \PHPUnit_Framework_TestCase
22+
class XliffConverterTest extends TestCase
2223
{
2324
public function testContentToCatalogue()
2425
{
2526
if (Kernel::VERSION_ID < 20800) {
2627
$this->markTestSkipped('Symfony <2.8 is not supported. ');
2728
}
2829

29-
$content = file_get_contents(__DIR__.'/messages.en.xlf');
30+
$content = file_get_contents(__DIR__.'/../Fixtures/single-file/messages.en.xlf');
3031
$catalogue = XliffConverter::contentToCatalogue($content, 'en', 'messages');
3132

3233
$this->assertEquals('en', $catalogue->getLocale());

0 commit comments

Comments
 (0)