Skip to content

Commit

Permalink
[willdurand#163] Fixes JsExtractorTest based on PR comment
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-richard committed Jan 29, 2018
1 parent 4137824 commit 4b1f6c4
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 207 deletions.
28 changes: 10 additions & 18 deletions Extractor/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Bazinga\Bundle\JsTranslationBundle\Extractor;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\Extractor\AbstractFileExtractor;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Bazinga\Bundle\JsTranslationBundle\Finder\FinderFactory;
use Bazinga\Bundle\JsTranslationBundle\Filesystem\Filesystem;

abstract class Extractor extends AbstractFileExtractor implements ExtractorInterface
{
Expand All @@ -17,28 +17,22 @@ abstract class Extractor extends AbstractFileExtractor implements ExtractorInter

private $filesystem;

private $finderFactory;
private $finder;

public function __construct(Filesystem $filesystem, FinderFactory $finderFactory) {
public function __construct(Filesystem $filesystem, Finder $finder) {
$this->filesystem = $filesystem;
$this->finderFactory = $finderFactory;
$this->finder = $finder;
}

/**
* {@inheritdoc}
*/
public function extract($resource, MessageCatalogue $catalogue)
{
$assetsPath = dirname($resource) . '/public';

if (!$this->filesystem->exists($assetsPath)) {
return;
}

$files = $this->extractFiles($assetsPath);
$files = $this->extractFiles($resource);

foreach ($files as $file) {
$this->parseMessagesFromContent($this->filesystem->getContents($file), $catalogue);
$this->parseMessagesFromContent(file_get_contents($file), $catalogue);
}
}

Expand Down Expand Up @@ -79,15 +73,13 @@ protected function isFile($file)

protected function extractFromDirectory($directory)
{
$finder = $this->finderFactory->createNewFinder();

$finder->files();
$this->finder->files();

foreach ($this->getSupportedFileExtensions() as $supportedExtension) {
$finder->name(sprintf('*.%s', $supportedExtension));
$this->finder->name(sprintf('*.%s', $supportedExtension));
}

return $finder->in($directory);
return $this->finder->in($directory);
}

private function getMessagesForSequence($fileContent, $sequence)
Expand Down
22 changes: 0 additions & 22 deletions Filesystem/Filesystem.php

This file was deleted.

13 changes: 0 additions & 13 deletions Finder/FinderFactory.php

This file was deleted.

2 changes: 0 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
<argument>%kernel.root_dir%</argument>
<tag name="console.command" command="bazinga:js-translation:dump" />
</service>
<service id="bazinga.jstranslation.filesystem" class="Bazinga\Bundle\JsTranslationBundle\Filesystem\Filesystem"></service>
<service id="bazinga.jstranslation.finder_factory" class="Bazinga\Bundle\JsTranslationBundle\Finder\FinderFactory"></service>
<service id="bazinga.jstranslation.translation_js_extractor" class="Bazinga\Bundle\JsTranslationBundle\Extractor\JsExtractor">
<argument type="service" id="bazinga.jstranslation.filesystem"></argument>
<argument type="service" id="bazinga.jstranslation.finder_factory"></argument>
Expand Down
210 changes: 58 additions & 152 deletions Tests/Extractor/JsExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,193 +2,99 @@

namespace Bazinga\JsTranslationBundle\Tests\Extractor;

use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Translation\MessageCatalogue;
use Bazinga\Bundle\JsTranslationBundle\Finder\FinderFactory;
use Bazinga\Bundle\JsTranslationBundle\Filesystem\Filesystem;
use Bazinga\Bundle\JsTranslationBundle\Extractor\JsExtractor;

final class JsExtractorTest extends PHPUnit_Framework_TestCase
final class JsExtractorTest extends TestCase
{
const TEST_LOCALE = 'en';
const TEST_KEY_1 = 'test-key-1';
const TRANSLATION_PATH_PUBLIC = '/translation-path/public';
const TRANSLATION_PATH_VIEWS = '/translation-path/views';

/**
* @var JsExtractor
*/
private $extractor;

/**
* @var Filesystem
*/
private $filesystem;

/**
* @var FinderFactory
*/
private $finderFactory;

/**
* @var MessageCatalogue
*/
private $messageCatalogue;

private $folder;
private $fileName;
private $fileContent;

public function setUp()
{
$this->filesystem = $this->prophesize('Bazinga\Bundle\JsTranslationBundle\Filesystem\Filesystem');
$this->finderFactory = $this->prophesize('Bazinga\Bundle\JsTranslationBundle\Finder\FinderFactory');
$this->messageCatalogue = new MessageCatalogue(self::TEST_LOCALE);

$this->extractor = new JsExtractor(
$this->filesystem->reveal(),
$this->finderFactory->reveal()
new Filesystem(),
new Finder()
);
}

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

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

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

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

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

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


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

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

private function givenASourceFolderWithNotValidTransChoiceFunctionUsage()
/**
* @dataProvider resourcesWithNotValidTransFunctionUsage
*/
public function testExtractShouldNotRetrieveTransKey($resources)
{
$this->givenASourceFolder();

$this->fileContent = <<<STRING
Translator.transChice('test-key-1', 5);
Translator.transCohie();
Translator.transChoce(variable, 5);
STRING;
$catalogue = new MessageCatalogue(self::TEST_LOCALE);
$this->extractor->extract($resources, $catalogue);
$this->assertEmpty($catalogue->all());
}

private function givenASourceFolderWithATransChoiceFunctionUsage()
/**
* @dataProvider resourcesWithATransFunctionUsage
*/
public function testExtractShouldRetrieveTransKey($resources)
{
$this->givenASourceFolder();

$this->fileContent = <<<STRING
Translator.transChoice('test-key-1', 5);
Translator.transChoice();
Translator.transChoice(variable, 5);
STRING;
$catalogue = new MessageCatalogue(self::TEST_LOCALE);
$this->extractor->extract($resources, $catalogue);
$this->assertTrue($catalogue->has(self::TEST_KEY_1));
}

private function givenASourceFolder()
/**
* @dataProvider resourcesWithNotValidTransChoiceFunctionUsage
*/
public function testExtractShouldNotRetrieveTransChoiceKey($resources)
{
$this->folder = self::TRANSLATION_PATH_VIEWS;
$this->fileName = 'test.js';
$this->filesystem
->exists(self::TRANSLATION_PATH_PUBLIC)
->willReturn(true);
$catalogue = new MessageCatalogue(self::TEST_LOCALE);
$this->extractor->extract($resources, $catalogue);
$this->assertEmpty($catalogue->all());
}

private function thenTheFinderWillFindAJsFile()
{
$finder = $this->prophesize('Symfony\Component\Finder\Finder');

$finder
->files()
->shouldBeCalled();

$finder
->name('*.js')
->shouldBeCalled();

$finder
->name('*.jsx')
->shouldBeCalled();

$finder
->in(self::TRANSLATION_PATH_PUBLIC)
->shouldBeCalled()
->willReturn(array($this->fileName));

$this->finderFactory->createNewFinder()->willReturn($finder->reveal());
}

private function andTheFilesystemWillGrabItsContent()
/**
* @dataProvider resourcesWithATransChoiceFunctionUsage
*/
public function testExtractShouldRetrieveTransChoiceKey($resources)
{
$this->filesystem
->getContents($this->fileName)
->willReturn($this->fileContent);
$catalogue = new MessageCatalogue(self::TEST_LOCALE);
$this->extractor->extract($resources, $catalogue);
$this->assertTrue($catalogue->has(self::TEST_KEY_1));
}

private function whenUsingTheExtractFromTheSut()
public function resourcesWithNotValidTransFunctionUsage()
{
$this->extractor->extract($this->folder, $this->messageCatalogue);
return array(
array(__DIR__.'/../Fixtures/Extractor/NotValidTransFunctionUsage'),
array(__DIR__.'/../Fixtures/Extractor/NotValidTransFunctionUsage/test.js'),
array(new \SplFileInfo(__DIR__.'/../Fixtures/Extractor/NotValidTransFunctionUsage/test.js')),
);
}

private function assertTheMessageCatalogueIsEmpty()
public function resourcesWithATransFunctionUsage()
{
$this->assertEmpty($this->messageCatalogue->all());
return array(
array(__DIR__.'/../Fixtures/Extractor/ATransFunctionUsage'),
array(__DIR__.'/../Fixtures/Extractor/ATransFunctionUsage/test.js'),
array(new \SplFileInfo(__DIR__.'/../Fixtures/Extractor/ATransFunctionUsage/test.js')),
);
}

private function assertTheTransKeyIsInMessageCatalogue()
public function resourcesWithNotValidTransChoiceFunctionUsage()
{
$this->assertTrue($this->messageCatalogue->has(self::TEST_KEY_1));
return array(
array(__DIR__.'/../Fixtures/Extractor/NotValidTransChoiceFunctionUsage'),
array(__DIR__.'/../Fixtures/Extractor/NotValidTransChoiceFunctionUsage/test.js'),
array(new \SplFileInfo(__DIR__.'/../Fixtures/Extractor/NotValidTransChoiceFunctionUsage/test.js')),
);
}

private function assertTheTransChoiceKeyIsInMessageCatalogue()
public function resourcesWithATransChoiceFunctionUsage()
{
$this->assertTrue($this->messageCatalogue->has(self::TEST_KEY_1));
return array(
array(__DIR__.'/../Fixtures/Extractor/ATransChoiceFunctionUsage'),
array(__DIR__.'/../Fixtures/Extractor/ATransChoiceFunctionUsage/test.js'),
array(new \SplFileInfo(__DIR__.'/../Fixtures/Extractor/ATransChoiceFunctionUsage/test.js')),
);
}
}
3 changes: 3 additions & 0 deletions Tests/Fixtures/Extractor/ATransChoiceFunctionUsage/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Translator.transChoice('test-key-1', 5);
Translator.transChoice();
Translator.transChoice(variable, 5);
3 changes: 3 additions & 0 deletions Tests/Fixtures/Extractor/ATransFunctionUsage/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Translator.trans('test-key-1');
Translator.trans();
Translator.trans(variable);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Translator.transChice('test-key-1', 5);
Translator.transCohie();
Translator.transChoce(variable, 5);
3 changes: 3 additions & 0 deletions Tests/Fixtures/Extractor/NotValidTransFunctionUsage/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Translator.tras('test-key-1');
Translator.trns();
Translator.tans(variable);

0 comments on commit 4b1f6c4

Please sign in to comment.