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 #164

Conversation

jcchavezs
Copy link

@jcchavezs jcchavezs commented Jun 10, 2016

@jcchavezs jcchavezs mentioned this pull request Jun 10, 2016

class Filesystem extends BaseFilesystem
{
public function getContents($filepath)
Copy link
Author

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.

Copy link
Contributor

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

@jcchavezs
Copy link
Author

retest this please


public function __construct(
Filesystem $filesystem,
Finder $finder
Copy link
Contributor

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

@jcchavezs
Copy link
Author

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]*\\(';
Copy link

@cmfcmf cmfcmf Aug 3, 2016

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)?\\('

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should

Copy link
Author

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?

Copy link
Contributor

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

Copy link

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Changing it now.

Copy link
Contributor

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

Copy link
Author

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.

@fivetide
Copy link

Is there any way one could help with this? I would love to be able to see this merged...

@jcchavezs jcchavezs force-pushed the 163_addition_of_js_and_coffee_extractor branch from a2fef88 to 629aa7b Compare October 21, 2016 08:36
@jcchavezs jcchavezs force-pushed the 163_addition_of_js_and_coffee_extractor branch from 629aa7b to 413c12d Compare October 21, 2016 08:48
@jcchavezs
Copy link
Author

jcchavezs commented Oct 21, 2016

@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?

@stof
Copy link
Contributor

stof commented Oct 21, 2016

@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.

@jcchavezs
Copy link
Author

@stof sorry, I meant PHP 5.3. I just fixed my comment.

@stof
Copy link
Contributor

stof commented Oct 21, 2016

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",
Copy link
Contributor

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"
Copy link
Contributor

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;
Copy link
Contributor

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

Copy link
Author

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()
Copy link
Contributor

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)

Copy link
Author

@jcchavezs jcchavezs Oct 21, 2016

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?).

Copy link
Contributor

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
Copy link
Contributor

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

Copy link
Author

@jcchavezs jcchavezs Oct 21, 2016

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.

Copy link
Contributor

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.

Copy link
Author

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.

Copy link
Contributor

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>
Copy link
Contributor

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

Copy link
Author

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)
Copy link
Contributor

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.

Copy link
Author

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 [];
Copy link
Contributor

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';
Copy link
Contributor

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

Copy link
Author

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.

Copy link
Contributor

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

@jcchavezs
Copy link
Author

@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.

@stof
Copy link
Contributor

stof commented Oct 21, 2016

@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

@jcchavezs
Copy link
Author

@stof I just added fixes for making it compatible with PHP 5.3 but the CI hasn't triggered yet.

@jcchavezs
Copy link
Author

@monteiro can we merge this?

@craigh
Copy link
Contributor

craigh commented Dec 29, 2016

What's going on with this PR?

@monteiro
Copy link
Collaborator

monteiro commented May 2, 2017

@jcchavezs thanks a lot for this PR. Looks very good. Can you address @stof 's comments so we can move forward with this?

@mcorteel
Copy link

mcorteel commented Sep 8, 2017

@jcchavezs Did you abandon this PR?

@jcchavezs
Copy link
Author

jcchavezs commented Sep 11, 2017 via email

@jeremy-richard
Copy link

This feature is awesome. Do you need help to release it ?

@jcchavezs
Copy link
Author

@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.

@jeremy-richard
Copy link

@jcchavezs OK, I forked BazingaJsTranslationBundle and checkout your branch.
First thing first I did a rebase to be up to date with the master 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'

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 ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is. Sorry.

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

@jcchavezs
Copy link
Author

jcchavezs commented Jan 29, 2018

@jeremy-richard
Copy link

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?

@jcchavezs
Copy link
Author

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Js and Coffee Extractor
8 participants