Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#163] Added support for translation extractors for JS #238

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[#163] Fixed a bug in the regex for extracting transChoice.
jcchavezs authored and jeremy-richard committed Jan 29, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 32f738c61cf4eb7331624bd2d48f103bf5f4f4c9
2 changes: 1 addition & 1 deletion Extractor/CoffeeExtractor.php
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

final class CoffeeExtractor extends Extractor
{
protected $sequence = '\\.trans[Choice]*\\s';
protected $sequence = '\\.trans(?:Choice)?\\s';

protected $supportedFileExtensions = [
'coffee',
2 changes: 1 addition & 1 deletion Extractor/JsExtractor.php
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

final class JsExtractor extends Extractor
{
protected $sequence = '\\.trans[Choice]*\\(';
protected $sequence = '\\.trans(?:Choice)?\\(';

protected $supportedFileExtensions = [
'js',
54 changes: 50 additions & 4 deletions Tests/Extractor/CoffeeExtractorTest.php
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ final class CoffeeExtractorTest extends PHPUnit_Framework_TestCase
const TRANSLATION_PATH_PUBLIC = '/translation-path/public';

/**
* @var JsExtractor
* @var CoffeeExtractor
*/
private $sut;

@@ -52,24 +52,53 @@ public function setUp()
);
}

public function testExtractShouldNotRetrieveTransKey()
{
$this->givenASourceFolderWithNotValidTransFunctionUsage();
$this->thenTheFinderWillFindACoffeeFile();
$this->andTheFilesystemWillGrabItsContent();
$this->whenUsingTheExtractFromTheSut();
$this->assertTheMessageCatalogueIsEmpty();
}

public function testExtractShouldRetrieveTransKey()
{
$this->givenASourceFolderWithATransFunctionUsage();
$this->thenTheFinderWillFindAJsFile();
$this->thenTheFinderWillFindACoffeeFile();
$this->andTheFilesystemWillGrabItsContent();
$this->whenUsingTheExtractFromTheSut();
$this->assertTheTransKeyIsInMessageCatalogue();
}

public function testExtractShouldNotRetrieveTransChoiceKey()
{
$this->givenASourceFolderWithNotValidTransChoiceFunctionUsage();
$this->thenTheFinderWillFindACoffeeFile();
$this->andTheFilesystemWillGrabItsContent();
$this->whenUsingTheExtractFromTheSut();
$this->assertTheMessageCatalogueIsEmpty();
}

public function testExtractShouldRetrieveTransChoiceKey()
{
$this->givenASourceFolderWithATransChoiceFunctionUsage();
$this->thenTheFinderWillFindAJsFile();
$this->thenTheFinderWillFindACoffeeFile();
$this->andTheFilesystemWillGrabItsContent();
$this->whenUsingTheExtractFromTheSut();
$this->assertTheTransChoiceKeyIsInMessageCatalogue();
}

private function givenASourceFolderWithNotValidTransFunctionUsage()
{
$this->givenASourceFolder();

$this->fileContent = <<<STRING
Translator.tras 'test-key-1'
Translator.tans
Translator.tran variable
STRING;
}

private function givenASourceFolderWithATransFunctionUsage()
{
$this->givenASourceFolder();
@@ -81,6 +110,7 @@ private function givenASourceFolderWithATransFunctionUsage()
STRING;
}


private function givenASourceFolderWithATransChoiceFunctionUsage()
{
$this->givenASourceFolder();
@@ -92,6 +122,17 @@ private function givenASourceFolderWithATransChoiceFunctionUsage()
STRING;
}

private function givenASourceFolderWithNotValidTransChoiceFunctionUsage()
{
$this->givenASourceFolder();

$this->fileContent = <<<STRING
Translator.transChice 'test-key-1', 5
Translator.transChoce
Translator.transCoice variable, 5
STRING;
}

private function givenASourceFolder()
{
$this->folder = self::TRANSLATION_PATH_VIEWS;
@@ -101,7 +142,7 @@ private function givenASourceFolder()
->willReturn(true);
}

private function thenTheFinderWillFindAJsFile()
private function thenTheFinderWillFindACoffeeFile()
{
$finder = $this->prophesize(Finder::class);

@@ -133,6 +174,11 @@ private function whenUsingTheExtractFromTheSut()
$this->sut->extract($this->folder, $this->messageCatalogue);
}

private function assertTheMessageCatalogueIsEmpty()
{
$this->assertEmpty($this->messageCatalogue->all());
}

private function assertTheTransKeyIsInMessageCatalogue()
{
$this->assertTrue($this->messageCatalogue->has(self::TEST_KEY_1));
46 changes: 46 additions & 0 deletions Tests/Extractor/JsExtractorTest.php
Original file line number Diff line number Diff line change
@@ -52,6 +52,15 @@ public function setUp()
);
}

public function testExtractShouldNotRetrieveTransKey()
{
$this->givenASourceFolderWithNotValidTransFunctionUsage();
$this->thenTheFinderWillFindAJsFile();
$this->andTheFilesystemWillGrabItsContent();
$this->whenUsingTheExtractFromTheSut();
$this->assertTheMessageCatalogueIsEmpty();
}

public function testExtractShouldRetrieveTransKey()
{
$this->givenASourceFolderWithATransFunctionUsage();
@@ -61,6 +70,15 @@ public function testExtractShouldRetrieveTransKey()
$this->assertTheTransKeyIsInMessageCatalogue();
}

public function testExtractShouldNotRetrieveTransChoiceKey()
{
$this->givenASourceFolderWithNotValidTransChoiceFunctionUsage();
$this->thenTheFinderWillFindAJsFile();
$this->andTheFilesystemWillGrabItsContent();
$this->whenUsingTheExtractFromTheSut();
$this->assertTheMessageCatalogueIsEmpty();
}

public function testExtractShouldRetrieveTransChoiceKey()
{
$this->givenASourceFolderWithATransChoiceFunctionUsage();
@@ -81,6 +99,29 @@ private function givenASourceFolderWithATransFunctionUsage()
STRING;
}


private function givenASourceFolderWithNotValidTransFunctionUsage()
{
$this->givenASourceFolder();

$this->fileContent = <<<STRING
Translator.tras('test-key-1');
Translator.trns();
Translator.tans(variable);
STRING;
}

private function givenASourceFolderWithNotValidTransChoiceFunctionUsage()
{
$this->givenASourceFolder();

$this->fileContent = <<<STRING
Translator.transChice('test-key-1', 5);
Translator.transCohie();
Translator.transChoce(variable, 5);
STRING;
}

private function givenASourceFolderWithATransChoiceFunctionUsage()
{
$this->givenASourceFolder();
@@ -137,6 +178,11 @@ private function whenUsingTheExtractFromTheSut()
$this->sut->extract($this->folder, $this->messageCatalogue);
}

private function assertTheMessageCatalogueIsEmpty()
{
$this->assertEmpty($this->messageCatalogue->all());
}

private function assertTheTransKeyIsInMessageCatalogue()
{
$this->assertTrue($this->messageCatalogue->has(self::TEST_KEY_1));