Add unit tests for LangParser

This commit is contained in:
Kijin Sung 2020-07-08 00:00:27 +09:00
parent 716b0e19bd
commit 592c8041cb
2 changed files with 91 additions and 0 deletions

23
tests/_data/lang/lang.xml Normal file
View file

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<lang>
<item name="testlang">
<value xml:lang="ko"><![CDATA[테스트 언어]]></value>
<value xml:lang="en"><![CDATA[Test Lang]]></value>
<value xml:lang="ja"><![CDATA[テスト言語]]></value>
</item>
<item name="testhtml">
<value xml:lang="ko"><![CDATA[<p>HTML<br>내용</p>]]></value>
<value xml:lang="en"><![CDATA[<p>HTML<br>Content</p>]]></value>
<value xml:lang="ja"><![CDATA[<p>HTML&nbsp;コンテンツ</p>]]></value>
</item>
<item name="testarray" type="array">
<item name="foo">
<value xml:lang="ko"><![CDATA[푸]]></value>
<value xml:lang="en"><![CDATA[FOO]]></value>
</item>
<item name="bar">
<value xml:lang="ko"><![CDATA[바]]></value>
<value xml:lang="en"><![CDATA[BAR]]></value>
</item>
</item>
</lang>

View file

@ -0,0 +1,68 @@
<?php
class LangParserTest extends \Codeception\TestCase\Test
{
protected $_dir = 'tests/_data/lang';
public function _before()
{
$files = Rhymix\Framework\Storage::readDirectory(\RX_BASEDIR . $this->_dir);
foreach ($files as $file)
{
if (preg_match('/\.php$/', $file))
{
Rhymix\Framework\Storage::delete($file);
}
}
}
public function _after()
{
$files = Rhymix\Framework\Storage::readDirectory(\RX_BASEDIR . $this->_dir);
foreach ($files as $file)
{
if (preg_match('/\.php$/', $file))
{
Rhymix\Framework\Storage::delete($file);
}
}
}
public function testConvertDirectory()
{
Rhymix\Framework\Parsers\LangParser::convertDirectory(\RX_BASEDIR . $this->_dir, ['ko', 'en']);
$this->assertTrue(file_exists(\RX_BASEDIR . $this->_dir . '/ko.php'));
$this->assertTrue(file_exists(\RX_BASEDIR . $this->_dir . '/en.php'));
$this->assertFalse(file_exists(\RX_BASEDIR . $this->_dir . '/ja.php'));
$this->assertFalse(file_exists(\RX_BASEDIR . $this->_dir . '/fr.php'));
$lang = new stdClass;
include \RX_BASEDIR . $this->_dir . '/ko.php';
$this->assertEquals('테스트 언어', $lang->testlang);
$this->assertEquals('<p>HTML<br>내용</p>', $lang->testhtml);
$this->assertEquals(['foo' => '푸', 'bar' => '바'], $lang->testarray);
$lang = new stdClass;
include \RX_BASEDIR . $this->_dir . '/en.php';
$this->assertEquals('Test Lang', $lang->testlang);
$this->assertEquals('<p>HTML<br>Content</p>', $lang->testhtml);
$this->assertEquals(['foo' => 'FOO', 'bar' => 'BAR'], $lang->testarray);
}
public function testCompileXMLtoPHP()
{
$in = \RX_BASEDIR . $this->_dir . '/lang.xml';
$out = \RX_BASEDIR . $this->_dir . '/ja.php';
$noout = \RX_BASEDIR . $this->_dir . '/en.php';
$result = Rhymix\Framework\Parsers\LangParser::compileXMLtoPHP($in, 'ja', $out);
$this->assertEquals($out, $result);
$this->assertTrue(file_exists($result));
$this->assertFalse(file_exists($noout));
$lang = new stdClass;
include \RX_BASEDIR . $this->_dir . '/ja.php';
$this->assertEquals('テスト言語', $lang->testlang);
$this->assertEquals('<p>HTML&nbsp;コンテンツ</p>', $lang->testhtml);
$this->assertNull($lang->testarray);
}
}