<commit_before><?php

namespace LizardsAndPumpkins\Renderer\Translation;

use LizardsAndPumpkins\Renderer\ThemeLocator;
use LizardsAndPumpkins\Renderer\Translation\Exception\LocaleNotSupportedException;
use LizardsAndPumpkins\TestFileFixtureTrait;

/**
 * @covers \LizardsAndPumpkins\Renderer\Translation\GettextTranslator
 */
class GettextTranslatorTest extends \PHPUnit_Framework_TestCase
{
    use TestFileFixtureTrait;

    /**
     * @var string
     */
    private $testLocaleCode = 'en_US';

    /**
     * @var string
     */
    private $originalLocaleCode;

    /**
     * @var ThemeLocator|\PHPUnit_Framework_MockObject_MockObject
     */
    private $stubThemeLocator;

    /**
     * @return string
     */
    private function getCurrentLocaleCode()
    {
        return setlocale(LC_ALL, 0);
    }

    protected function setUp()
    {
        if (!function_exists('gettext')) {
            $this->markTestSkipped('Gettext is not installed.');
        }

        $this->stubThemeLocator = $this->getMock(ThemeLocator::class, [], [], '', false);

        $this->originalLocaleCode = $this->getCurrentLocaleCode();
        if ($this->originalLocaleCode === $this->testLocaleCode) {
            $this->fail('Test can not be executed because original system locale is identical to test locale.');
        }
    }

    public function testTranslatorInterfaceIsImplemented()
    {
        $result = GettextTranslator::forLocale($this->testLocaleCode, $this->stubThemeLocator);
        $this->assertInstanceOf(Translator::class, $result);
    }

    public function testTranslatorInstantiationIsNotChangingOriginalLocale()
    {
        GettextTranslator::forLocale($this->testLocaleCode, $this->stubThemeLocator);
        $currentLocaleCode = $this->getCurrentLocaleCode();

        $this->assertSame($currentLocaleCode, $this->originalLocaleCode);
    }

    public function testExceptionIsThrownIfLocaleIsNotInstalled()
    {
        $this->setExpectedException(LocaleNotSupportedException::class);
        GettextTranslator::forLocale('foo_BAR', $this->stubThemeLocator);
    }

    public function testTranslationOfAStringIsNotChangingSystemLocale()
    {
        GettextTranslator::forLocale($this->testLocaleCode, $this->stubThemeLocator)->translate('foo');
        $currentLocaleCode = $this->getCurrentLocaleCode();

        $this->assertSame($currentLocaleCode, $this->originalLocaleCode);
    }

    public function testOriginalStringIsReturnedIfTranslationIsMissing()
    {
        $testTranslationSource = 'foo';
        $translator = GettextTranslator::forLocale($this->testLocaleCode, $this->stubThemeLocator);
        $result = $translator->translate($testTranslationSource);

        $this->assertSame($testTranslationSource, $result);
    }

    public function testGivenStringIsTranslated()
    {
        if (shell_exec('which msgfmt') === '') {
            $this->markTestSkipped('The shell command msgfmt from gettext is not installed.');
        }

        $testThemeDirectoryPath = sys_get_temp_dir();
        $testTranslationFilePath = sprintf(
            '%1$s/locale/%2$s/LC_MESSAGES/%2$s.mo',
            $testThemeDirectoryPath,
            $this->testLocaleCode
        );
        $this->createFixtureFile($testTranslationFilePath, '', 0777);

        $testTranslationSource = 'foo';
        $testTranslationResult = 'bar';

        shell_exec(sprintf(
            'printf "msgid \"%s\"\nmsgstr \"%s\"" | msgfmt - -o %s',
            $testTranslationSource,
            $testTranslationResult,
            $testTranslationFilePath
        ));

        $this->stubThemeLocator->method('getThemeDirectory')->willReturn($testThemeDirectoryPath);

        $translator = GettextTranslator::forLocale($this->testLocaleCode, $this->stubThemeLocator);
        $result = $translator->translate($testTranslationSource);

        $this->assertSame($testTranslationResult, $result);
    }
}
<commit_msg>Issue #655: Fix msgfmt detection<commit_after><?php

namespace LizardsAndPumpkins\Renderer\Translation;

use LizardsAndPumpkins\Renderer\ThemeLocator;
use LizardsAndPumpkins\Renderer\Translation\Exception\LocaleNotSupportedException;
use LizardsAndPumpkins\TestFileFixtureTrait;

/**
 * @covers \LizardsAndPumpkins\Renderer\Translation\GettextTranslator
 */
class GettextTranslatorTest extends \PHPUnit_Framework_TestCase
{
    use TestFileFixtureTrait;

    /**
     * @var string
     */
    private $testLocaleCode = 'en_US';

    /**
     * @var string
     */
    private $originalLocaleCode;

    /**
     * @var ThemeLocator|\PHPUnit_Framework_MockObject_MockObject
     */
    private $stubThemeLocator;

    /**
     * @return string
     */
    private function getCurrentLocaleCode()
    {
        return setlocale(LC_ALL, 0);
    }

    protected function setUp()
    {
        if (!function_exists('gettext')) {
            $this->markTestSkipped('Gettext is not installed.');
        }

        $this->stubThemeLocator = $this->getMock(ThemeLocator::class, [], [], '', false);

        $this->originalLocaleCode = $this->getCurrentLocaleCode();
        if ($this->originalLocaleCode === $this->testLocaleCode) {
            $this->fail('Test can not be executed because original system locale is identical to test locale.');
        }
    }

    public function testTranslatorInterfaceIsImplemented()
    {
        $result = GettextTranslator::forLocale($this->testLocaleCode, $this->stubThemeLocator);
        $this->assertInstanceOf(Translator::class, $result);
    }

    public function testTranslatorInstantiationIsNotChangingOriginalLocale()
    {
        GettextTranslator::forLocale($this->testLocaleCode, $this->stubThemeLocator);
        $currentLocaleCode = $this->getCurrentLocaleCode();

        $this->assertSame($currentLocaleCode, $this->originalLocaleCode);
    }

    public function testExceptionIsThrownIfLocaleIsNotInstalled()
    {
        $this->setExpectedException(LocaleNotSupportedException::class);
        GettextTranslator::forLocale('foo_BAR', $this->stubThemeLocator);
    }

    public function testTranslationOfAStringIsNotChangingSystemLocale()
    {
        GettextTranslator::forLocale($this->testLocaleCode, $this->stubThemeLocator)->translate('foo');
        $currentLocaleCode = $this->getCurrentLocaleCode();

        $this->assertSame($currentLocaleCode, $this->originalLocaleCode);
    }

    public function testOriginalStringIsReturnedIfTranslationIsMissing()
    {
        $testTranslationSource = 'foo';
        $translator = GettextTranslator::forLocale($this->testLocaleCode, $this->stubThemeLocator);
        $result = $translator->translate($testTranslationSource);

        $this->assertSame($testTranslationSource, $result);
    }

    public function testGivenStringIsTranslated()
    {
        if (!$this->isMsgfmtInstalled()) {
            $this->markTestSkipped('The shell command msgfmt from gettext is not installed.');
        }

        $testThemeDirectoryPath = sys_get_temp_dir();
        $testTranslationFilePath = sprintf(
            '%1$s/locale/%2$s/LC_MESSAGES/%2$s.mo',
            $testThemeDirectoryPath,
            $this->testLocaleCode
        );
        $this->createFixtureFile($testTranslationFilePath, '', 0777);

        $testTranslationSource = 'foo';
        $testTranslationResult = 'bar';

        shell_exec(sprintf(
            'printf "msgid \"%s\"\nmsgstr \"%s\"" | msgfmt - -o %s',
            $testTranslationSource,
            $testTranslationResult,
            $testTranslationFilePath
        ));

        $this->stubThemeLocator->method('getThemeDirectory')->willReturn($testThemeDirectoryPath);

        $translator = GettextTranslator::forLocale($this->testLocaleCode, $this->stubThemeLocator);
        $result = $translator->translate($testTranslationSource);

        $this->assertSame($testTranslationResult, $result);
    }

    /**
     * @return bool
     */
    private function isMsgfmtInstalled()
    {
        exec('which msgfmt', $output, $returnVar);
        return 0 === $returnVar;
    }
}

{'fraction_non_alphanumeric': 0.12156619018023618, 'fraction_numerical': 0.002486016159105034, 'mean_word_length': 3.321160042964554, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3108951', 'n_tokens_mistral': 2263, 'n_tokens_neox': 2234, 'n_words': 335}