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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
<?php
declare(strict_types=1);
namespace Giggsey\Locale\Tests;
use Giggsey\Locale\Build\DataBuilder;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Filesystem\Filesystem;
use RuntimeException;
class DataBuilderTest extends TestCase
{
protected static $outputDir;
public static function setUpBeforeClass(): void
{
static::$outputDir = self::createTempDirectory();
}
public static function tearDownAfterClass(): void
{
if (static::$outputDir) {
$fileSystem = new Filesystem();
$fileSystem->remove(static::$outputDir);
}
}
public function testGeneratingData(): string
{
$dataBuilder = new DataBuilder();
$outputDir = static::$outputDir;
$dataBuilder->generate('1', __DIR__ . '/testData/', $outputDir, new NullOutput());
$listFile = $outputDir . '_list.php';
$this->assertFileExists($listFile);
$list = require $listFile;
$this->assertArrayHasKey('en', $list);
$this->assertArrayHasKey('en-gb', $list);
$this->assertArrayNotHasKey('en-150', $list, 'en-150 is part of the ignored locale list, so should be ignored');
$this->assertArrayNotHasKey('empty', $list, 'empty has no territories defined, so should be ignored');
$versionFile = $outputDir . '_version.php';
$this->assertFileExists($versionFile);
$version = require $versionFile;
$this->assertSame('1', $version);
return $outputDir;
}
/**
* @param string $outputDir Output directory
*/
#[Depends('testGeneratingData')]
public function testEnglishData(string $outputDir): void
{
$englishFile = $outputDir . 'en.php';
$this->assertFileExists($englishFile);
$data = require $englishFile;
$this->assertArrayHasKey('MFC', $data);
$this->assertSame('My First Country', $data['MFC']);
$this->assertArrayHasKey('MSC', $data);
$this->assertSame('My Second Country', $data['MSC']);
$this->assertArrayNotHasKey('EU', $data, 'EU is part of the ignored region list, so should be ignored');
$this->assertArrayNotHasKey('GB-alt-short', $data, 'GB-alt-short is an alternative name, so should be ignored');
$this->assertArrayNotHasKey('001', $data, '001 is a numerical territory name, so should be ignored');
$this->assertArrayNotHasKey('CH', $data, 'CH has the same name as the key, so should be ignored');
}
/**
* @param string $outputDir Output directory
*/
#[Depends('testGeneratingData')]
public function testGBData(string $outputDir): void
{
$englishFile = $outputDir . 'en-gb.php';
$this->assertFileExists($englishFile);
$data = require $englishFile;
$this->assertArrayNotHasKey('MFC', $data);
$this->assertArrayHasKey('MSC', $data);
$this->assertSame('My Second Country is different here', $data['MSC']);
}
/**
* Create a temp directory
* @link http://stackoverflow.com/a/1707859
*/
private static function createTempDirectory(): string
{
$tempFile = tempnam(sys_get_temp_dir(), 'LocaleTest');
if (file_exists($tempFile)) {
unlink($tempFile);
}
mkdir($tempFile);
if (is_dir($tempFile)) {
return $tempFile . DIRECTORY_SEPARATOR;
}
throw new RuntimeException('Unable to create temp directory');
}
}
|