1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
<?php
namespace Gettext\Tests;
use Gettext\Translations;
use PHPUnit\Framework\TestCase;
abstract class AbstractTestCase extends TestCase
{
protected static $ext = [
'Blade' => 'php',
'Csv' => 'csv',
'CsvDictionary' => 'csv',
'Jed' => 'json',
'JsCode' => 'js',
'JsonDictionary' => 'json',
'Json' => 'json',
'Mo' => 'mo',
'PhpArray' => 'php',
'PhpCode' => 'php',
'Po' => 'po',
'Xliff' => 'xlf',
'Yaml' => 'yml',
'YamlDictionary' => 'yml',
'VueJs' => 'vue',
];
protected static function asset($file)
{
return './tests/assets/'.$file;
}
/**
* @param string $file
* @param string|null $format
* @param array $options
* @return Translations
*/
protected static function get($file, $format = null, array $options = [])
{
if ($format === null) {
$format = basename($file);
}
$method = "from{$format}File";
$file = static::asset($file.'.'.static::$ext[$format]);
return Translations::$method($file, $options);
}
protected function assertContent(Translations $translations, $file, $format = null)
{
if ($format === null) {
$format = basename($file);
}
$method = "to{$format}String";
$content = file_get_contents(static::asset($file.'.'.static::$ext[$format]));
// Po reference files are LittleEndian
if ($format !== 'Mo' || self::isLittleEndian()) {
$this->assertSame($content, $translations->$method(), $file);
}
}
protected static function saveContent(Translations $translations, $file, $format = null)
{
if ($format === null) {
$format = basename($file);
}
$method = "to{$format}String";
$file = static::asset($file.'.'.static::$ext[$format]);
file_put_contents($file, $translations->$method());
}
protected function runTestFormat($file, $countTranslations, $countTranslated = 0, $countHeaders = 8)
{
$format = basename($file);
$method = "from{$format}File";
/** @var Translations $translations */
$translations = Translations::$method(static::asset($file.'.'.static::$ext[$format]));
$this->assertCount($countTranslations, $translations);
$this->assertCount($countHeaders, $translations->getHeaders(), json_encode($translations->getHeaders(), JSON_PRETTY_PRINT));
$this->assertSame($countTranslated, $translations->countTranslated());
$this->assertContent($translations, $file);
}
protected function isLittleEndian()
{
return pack("s", 0x3031) === "10";
}
}
|