Skip to content

Instantly share code, notes, and snippets.

@srosato
Created March 2, 2016 19:26
Show Gist options
  • Save srosato/ae79e9be28c8455d8ebf to your computer and use it in GitHub Desktop.
Save srosato/ae79e9be28c8455d8ebf to your computer and use it in GitHub Desktop.
<?php
namespace Tests\Ez\Functional\Data;
use Tests\Codeception\TestCase\FunctionalTest;
use Tests\Ez\Codeception\DataConfiguration;
use Tests\Ez\FunctionalTester;
/**
* System should be translatable through a Web Interface
*
* @group data
* @group data.i18n
* @group ci
*/
class I18nCest extends FunctionalTest
{
/**
* @group legacy
* @group data.i18n.extract
* @param FunctionalTester $tester
*/
public function extractAllTranslations(FunctionalTester $tester)
{
$tester->expectTo("extract all the translations keys for this application");
$this->runExtractCommand($tester);
$tester->seeCommandOutput("Extracting messages from directory");
$tester->dontSeeCommandOutput("Exception");
$tester->expectTo("see the correct translations files dumped");
$tester->seeCommandOutput("Writing translation file");
$tester->seeCommandOutput("App.en.xliff");
$tester->seeCommandOutput("App.fr.xliff");
$tester->seeCommandOutput("done!");
$tester->amOnPage("/_trans");
$tester->see("JMSTranslationBundle UI");
$this->cleanTranslationsFile($tester);
}
/**
* @param FunctionalTester $tester
*/
private function runExtractCommand(FunctionalTester $tester)
{
$tester->runCommand('translation:extract', array(
'-c' => 'eventzen',
'locales' => array('en', 'fr')
));
}
/**
* @param FunctionalTester $tester
*/
private function cleanTranslationsFile(FunctionalTester $tester)
{
$tester->deleteDir(DataConfiguration::translationDir());
}
}
<?php
namespace Tests\Ez\Integration\App;
use Codeception\Configuration;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Model\MessageCollection;
use JMS\TranslationBundle\Translation\Loader\SymfonyLoaderAdapter;
use JMS\TranslationBundle\Translation\Loader\XliffLoader;
use JMS\TranslationBundle\Translation\LoaderManager;
use Symfony\Component\Translation\Loader\YamlFileLoader;
use Symfony\Component\Yaml\Yaml;
use Tests\Codeception\TestCase\IntegrationTest;
/**
* Making sure that all keys are translated for the application, every time. A call to the extraction command without
* translating anything will most likely make that test break. As long as nothing is extracted,
* English strings will be output since this project uses English descriptions to hint translations.
* If they are extracted but saved, this test will pass, so there is still no 100% guarantee that
* everything is translated.
*
* Note: this is not a convenient enough test. Need to find a solution where we should check
* that everything is translated when we need it, but not before that; in order to not bother the developer.
*
* @group data
* @group data.i18n
* @group data.i18n.translation
*/
class TranslationsTest extends IntegrationTest
{
protected $localesToAssert = array('fr');
private $translationDir;
public function _before()
{
$this->translationDir = Configuration::projectDir() . '../../Ez/App/Resources/translations';
}
/**
* @test
* @group legacy
* @group data.i18n
* @group data.i18n.check
*/
public function checkThatAllTranslationKeysAreFullyTranslated()
{
$manager = new LoaderManager(array(
'xliff' => new XliffLoader(),
'yml' => new SymfonyLoaderAdapter(new YamlFileLoader())
));
foreach( $this->localesToAssert as $locale ) {
$catalogue = $manager->loadFromDirectory($this->translationDir, $locale);
/* @var $collection MessageCollection */
foreach( $catalogue->getDomains() as $collection ) {
/* @var $enMessage Message */
foreach( $collection->all() as $enMessage ) {
$this->assertFalse($enMessage->isNew(),
"Message with id {$enMessage->getId()} defined in domain {$enMessage->getDomain()} is not translated for locale {$locale}");
}
}
}
}
/**
* @test
* @group legacy
* @group data.i18n
* @group data.i18n.date
*/
public function checkThatDatesCanBeLocalizedInTwigTemplates()
{
$filter = $this->getContainer()->get('twig')->getFilter('format_date');
$this->checkThat("Cannot localize date, twig filter format_date not available", $filter, not(equalTo(false)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment