Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[TwigBridge] fix BC for FormExtension if renderer is FormRenderer #24535

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

Closed
wants to merge 8 commits into from

Conversation

dmaicher
Copy link
Contributor

Q A
Branch? 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #24533
License MIT
Doc PR -

This fixes some issues within FormExtension since the renderer is now a FormRenderer.

@@ -54,6 +56,8 @@ public function initRuntime(Environment $environment)
{
if ($this->renderer instanceof TwigRendererInterface) {
$this->renderer->setEnvironment($environment);
} elseif ($this->renderer instanceof FormRendererInterface && $this->renderer->getEngine() instanceof TwigRendererEngineInterface) {
$this->renderer->getEngine()->setEnvironment($environment);
Copy link
Member

Choose a reason for hiding this comment

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

you should do nothing in this case instead (it already has the environment injected properly)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah ok. Done 😉

$renderer->setEnvironment($this->renderer[2]);
if ($renderer instanceof TwigRendererInterface) {
$renderer->setEnvironment($this->renderer[2]);
} elseif ($renderer instanceof FormRendererInterface && $renderer->getEngine() instanceof TwigRendererEngineInterface) {
Copy link
Member

Choose a reason for hiding this comment

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

this case should never happen

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can happen if you call ->initRuntime before accessing ->renderer on 3.4?

$twig = $this->get('twig');

$twig
    ->getExtension('Symfony\Bridge\Twig\Extension\FormExtension')
    ->initRuntime($twig);

$twig
    ->getExtension('Symfony\Bridge\Twig\Extension\FormExtension')
    ->renderer;

Results in:

Attempted to call an undefined method named "setEnvironment" of class "Symfony\Component\Form\FormRenderer".

This works fine on 3.3.

@xabbuh
Copy link
Member

xabbuh commented Oct 12, 2017

Can't we create a test case that would fail without these changes?

use Symfony\Component\Form\FormRendererInterface;
use Twig\Environment;

class FormExtensionTest extends TestCase
Copy link
Contributor Author

Choose a reason for hiding this comment

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

should I mark this test somehow as deprecated or similar? There is no need to merge it into master for example as there is nothing to test anymore really

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Oct 12, 2017
@dmaicher
Copy link
Contributor Author

I don't quite understand this test fail:

1) Warning
The data provider specified for Symfony\Bridge\Twig\Tests\Extension\FormExtensionTest::testInitRuntimeAndAccessRenderer is invalid.
Trying to configure method "get" which cannot be configured because it does not exist, has not been specified, is final, or is static
2) Warning
The data provider specified for Symfony\Bridge\Twig\Tests\Extension\FormExtensionTest::testAccessRendererAndInitRuntime is invalid.
Trying to configure method "get" which cannot be configured because it does not exist, has not been specified, is final, or is static

I'm mocking Symfony\Component\DependencyInjection\ContainerInterface.

@xabbuh
Copy link
Member

xabbuh commented Oct 12, 2017

Looks like you need to add your tests to the legacy group.

@dmaicher
Copy link
Contributor Author

@xabbuh thanks looks better now. One test fail left: https://travis-ci.org/symfony/symfony/jobs/287174522

@xabbuh
Copy link
Member

xabbuh commented Oct 13, 2017

You need to add symfony/dependency-injection to the require-dev section of TwigBridge's composer.json file.

@dmaicher
Copy link
Contributor Author

thanks again @xabbuh 👍 Can be reviewed then

@@ -54,7 +54,7 @@ public function initRuntime(Environment $environment)
{
if ($this->renderer instanceof TwigRendererInterface) {
$this->renderer->setEnvironment($environment);
} elseif (null !== $this->renderer) {
} elseif (is_array($this->renderer)) {
Copy link
Member

Choose a reason for hiding this comment

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

This was not needed to fix the bug, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is needed:

Testing Symfony\Bridge\Twig\Tests\Extension\FormExtensionTest
.....E                                                              6 / 6 (100%)

Time: 132 ms, Memory: 4.00MB

There was 1 error:

1) Symfony\Bridge\Twig\Tests\Extension\FormExtensionTest::testAccessRendererAndInitRuntime with data set #2 (array(Mock_ContainerInterface_3ca94d5e Object (...), 'service_id'), Mock_FormRendererInterface_d5edefef Object (...))
Error: Cannot use object of type Mock_FormRendererInterface_d5edefef as array

/var/www/symfony/src/Symfony/Bridge/Twig/Extension/FormExtension.php:58
/var/www/symfony/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTest.php:43

ERRORS!
Tests: 6, Assertions: 15, Errors: 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which is the problem reported here: sonata-project/SonataAdminBundle#4652

Copy link
Member

Choose a reason for hiding this comment

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

For anyone else wondering: The array stored in $this->renderer is replaced by the FormRenderInterface instance that is fetched from the container in __get(): https://github.com/dmaicher/symfony/blob/6813a9f1f1251412bf6161dd732e65e853680736/src/Symfony/Bridge/Twig/Extension/FormExtension.php#L124

@@ -22,6 +22,7 @@
"require-dev": {
"fig/link-util": "^1.0",
"symfony/asset": "~2.8|~3.0|~4.0",
"symfony/dependency-injection": "~2.8|~3.0|~4.0",
Copy link
Member

Choose a reason for hiding this comment

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

this targets 3.4, should this be ~3.4|~4.0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just need the ContainerInterface for the tests. So I guess 2.8 is also fine? Not sure what to do here tbh 😊

@fabpot
Copy link
Member

fabpot commented Oct 13, 2017

Thank you @dmaicher.

fabpot added a commit that referenced this pull request Oct 13, 2017
…enderer (dmaicher)

This PR was squashed before being merged into the 3.4 branch (closes #24535).

Discussion
----------

[TwigBridge] fix BC for FormExtension if renderer is FormRenderer

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #24533
| License       | MIT
| Doc PR        | -

This fixes some issues within FormExtension since the renderer is now a `FormRenderer`.

Commits
-------

4a2f608 [TwigBridge] fix BC for FormExtension if renderer is FormRenderer
@fabpot fabpot closed this Oct 13, 2017
@dmaicher dmaicher deleted the fix-24533 branch October 13, 2017 13:29
@xabbuh
Copy link
Member

xabbuh commented Oct 13, 2017

Just noticed: Shouldn't we have merged this into the 3.3 branch?

@dmaicher
Copy link
Contributor Author

@xabbuh this bug did not exist on 3.3 though as the rendering service there was an instance of TwigRendererInterface and no FormRendererInterface yet as on 3.4

chalasr added a commit that referenced this pull request Oct 18, 2017
This PR was merged into the 4.0-dev branch.

Discussion
----------

[TwigBridge] fix merge of 3.4 into master

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes/no
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Within #24535 I added `FormExtensionTest` which is completely useless now on master 😊

So this PR just removes the test after it has been merged from 3.4 into master.

Commits
-------

5c36609 [TwigBridge] fix merge of 3.4 into master
@nicolas-grekas
Copy link
Member

@dmaicher could you please check sonata-project/SonataAdminBundle#4758, is it somehow related to this one?

@dmaicher
Copy link
Contributor Author

@nicolas-grekas seems not related as my fix was for 3.4 but the problem there actually happens on 3.3

@dmaicher
Copy link
Contributor Author

dmaicher commented Nov 15, 2017

But I have the feeling 3.4 broke BC by switching from Symfony\Bridge\Twig\Form\TwigRenderer to Symfony\Component\Form\FormRenderer?

This PR only fixes some symptoms of that BC break imho.

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

Successfully merging this pull request may close these issues.

8 participants