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 #164
[#163] Added support for translation extractors for JS #164
Conversation
|
||
class Filesystem extends BaseFilesystem | ||
{ | ||
public function getContents($filepath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main purpose for this is to mock the usage of getContents
in the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once you stop mocking the finder (which should not be done if you want the test to actually test that things are working), you won't need to mock getting the file content either
retest this please |
|
||
public function __construct( | ||
Filesystem $filesystem, | ||
Finder $finder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the signature should stay on 1 line
There is a problem when dealing with more than one bundle. I will fix it and the checks failure in upcoming commits. |
|
||
final class JsExtractor extends Extractor | ||
{ | ||
protected $sequence = '\\.trans[Choice]*\\('; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should rather be '\\.trans(?:Choice)?\\('
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't see the implementation difference. Can @cmfcmf explain it to me?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
your regex matches transoichie
too, as it allows to put any string containing the letters C
, h
, o
, i
, c
and e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure: Compare your regex:
https://regex101.com/r/9Ng6xR/1
with mine
https://regex101.com/r/wrJ9WE/1
Your regular expression does also match trans... where ... is any combination of the letters C
, h
, o
, i
, c
, e
. Mine looks specifically for Choice
, in this order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Changing it now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should also cover this in tests btw
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Tests for this are added.
Is there any way one could help with this? I would love to be able to see this merged... |
…Coffeescript and the inclusion of phpunit for the tests.
…urces/config/services.xml.
…he extractors stateless.
a2fef88
to
629aa7b
Compare
…r to not having broken inheritances.
629aa7b
to
413c12d
Compare
@willdurand is there any possibility we give up with PHP 5.3? Since now one constraint is Symfony 2.7 which only works with >5.3.9. What do you think? |
@jcchavezs I don't understand how the fact that Symfony 2.7 dropped support for PHP 5.3.8 has anything to do with the PHP 5.4 support. |
@stof sorry, I meant PHP 5.3. I just fixed my comment. |
well, dropping support for initial PHP 5.3 releases (which were recommended against them since Symfony 2.2 already as they are totally buggy) does not change the fact that Symfony 2 supports PHP 5.3 (the uptodate 5.3 version is 5.3.29). anyway, dropping support for old PHP versions is out of the scope of this PR |
@@ -11,6 +11,7 @@ | |||
} | |||
], | |||
"require": { | |||
"symfony/translation": "~2.7|~3.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong indentation
@@ -20,7 +21,8 @@ | |||
"symfony/yaml": "~2.7|~3.1", | |||
"symfony/browser-kit": "~2.7|~3.1", | |||
"symfony/twig-bundle": "~2.7|~3.1", | |||
"symfony/phpunit-bridge": "~2.7|~3.1" | |||
"symfony/phpunit-bridge": "~2.7|~3.1", | |||
"phpunit/phpunit": "~4.8" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should allow both 4.8 and 5.x if you install it as a dependency, because PHPUnit 4.8 does not officially support PHP 7.
/** | ||
* @var JsExtractor | ||
*/ | ||
private $sut; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extractor
would be a better name IMO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sut
stands for subject under testing
which makes it more clear that this is what we are testing.
$finder = $this->prophesize(Finder::class); | ||
|
||
$finder | ||
->files() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please don't mock the finder. This does not allow you to ensure that it is configured properly, as you never ensure that the way you configured it does way you expect it to be. You should not mock what you don't own (if Symfony changes the behavior of the Finder, this test will not warn you about it, as nothing ensures that your mock describes the real behavior)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a unit test and Finder
is very tight to the implementation and it does not implement any interface and if I don't mock it then I need to provide actual files to test it (why should I do it in a unit test?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But there is nothing in your test ensuring that the way you configured the finder is actually the right one to find the files you want to find. so your test currently says "if the code calls the Finder in the way it currently calls it, things are working". This is wrong. I does not actually ensure that configuring the Finder this way is what makes things work.
And it does not protect you from changes in the Finder behavior either.
What you need here is an integration test, which could use the real Finder. You should not mock the finder as it is outside your domain boundary. Using a unit test to cover this code is precisely the issue.
|
||
use Symfony\Component\Finder\Finder; | ||
|
||
class FinderFactory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, this class is not necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finder
is stateful
so on every call to AbstractFileExtractor::extractFiles
which calls to AbstractFileExtractor::extractFromDirectory
i need to create a new finder because I can not reuse an existing one and the only one way to do that is to use a factory otherwise I need to call the constructor of Finder
inside the class which is so bad and make it untestable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
having such a factory is only useful to allow you mocking the Finder in tests, which is already something your test should not do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It allows me to avoid the instantiation of classes inside my class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instantiating a class which is not a service is fine.
and btw, you don't avoid instantiating in your class. You just moved it to a different class of yours
@@ -22,5 +22,17 @@ | |||
<argument></argument> <!-- active locales --> | |||
<argument></argument> <!-- active domains --> | |||
</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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
such services should be private
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. I am changing it.
$this->prefix = $prefix; | ||
} | ||
|
||
protected function parseMessagesFromContent($fileContent, MessageCatalogue $catalogue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all these should be private. There is no reason to introduce extension points needing to be maintained for BC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. I am changing it.
return $matches[1]; | ||
} | ||
|
||
return []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no short array syntax
*/ | ||
public function extract($resource, MessageCatalogue $catalogue) | ||
{ | ||
$assetsPath = dirname($resource) . '/public'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks weird to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This extractor is meant to search for assets (which in 99% of the cases lies on the Resources/public folder). Since this checks every single file, restricting to the public folder improves the performance dramatically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$resource
will already be the path to Appbundle/Resources
when Symfony calls the extractor. So using dirname()
actually breaks it
@stof agree with the fact that the removal of the support to PHP 5.3 is in the scope of another PR. Before changing the incompatible code I would try to talk with @willdurand about it. |
@jcchavezs fixes for PHP 5.3 compat were done by @monteiro (new maintainer of the bundle) a few days ago, so I guess this is not yet planned |
@stof I just added fixes for making it compatible with PHP 5.3 but the CI hasn't triggered yet. |
@monteiro can we merge this? |
What's going on with this PR? |
@jcchavezs thanks a lot for this PR. Looks very good. Can you address @stof 's comments so we can move forward with this? |
@jcchavezs Did you abandon this PR? |
Nope. I've been busy but I will take some time to work on this by this week.
|
This feature is awesome. Do you need help to release it ? |
@jeremy-richard hi there, honestly I did not have time to fix @stof comments but if you can do it I am more than happy to merge this. |
@jcchavezs OK, I forked BazingaJsTranslationBundle and checkout your branch. Can you give me a hint about what is done, what needs to be done please? |
$this->givenASourceFolder(); | ||
|
||
$this->fileContent = <<<STRING | ||
Translator.tras 'test-key-1' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jcchavezs Is there a typo? Should this be trans
instaed of tras
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is. Sorry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my bad in fact it is not a typo as this test NotValidTransFunctionUsage
@jeremy-richard everything was working but there are three comments that must be addressed:
|
OK, I pushed some commits to my branch https://github.com/jeremy-richard/BazingaJsTranslationBundle/tree/163_addition_of_js_and_coffee_extractor. How do we proceed? Should I open a PR on @jcchavezs repository or should I open a new PR here? |
@jeremy-richard I'd say that you open your own PR against master so you can easily fix the comments people might have. Link this one so we keep context. Really thanks for doing this! |
Closes #163.
Ping @willdurand, @JANorman, @monteiro and @stof