Add test examples for include to render#327
Add test examples for include to render#327alexander-schranz wants to merge 3 commits intoVincentLanglet:mainfrom
Conversation
VincentLanglet
left a comment
There was a problem hiding this comment.
You should add example for
{{ include('template.html', with_context = false) }}
| {{ render('template.html', {'foo': 'bar'}|merge(_context)) }} | ||
| {{ render('template.html', {'foo': 'bar'}|merge(_context), true) }} | ||
| {{ render('template.html', {'foo': 'bar'}|merge(_context), true, true) }} | ||
| {{ render('template.html', with_context = true) }} |
There was a problem hiding this comment.
If I understand the render method, there is no with_context param.
There was a problem hiding this comment.
Yes fixed it and added with_context = false.
There was a problem hiding this comment.
The changes are still in discussion but wanted to show example how fixer could make a upgrade possible.
7ef08e1 to
b7999c2
Compare
tests/Rules/Function/IncludeToRender/IncludeToRenderFunctionRule.fixed.twig
Outdated
Show resolved
Hide resolved
…le.fixed.twig Co-authored-by: Simon André <smn.andre@gmail.com>
|
If you want to bootstrap the rule, this should be a good start:
<?php
declare(strict_types=1);
namespace TwigCsFixer\Rules\Function;
use TwigCsFixer\Rules\AbstractFixableRule;
use TwigCsFixer\Token\Token;
use TwigCsFixer\Token\Tokens;
/**
* Ensures that render function is used instead of deprecated include function.
*/
final class IncludeToRenderFunctionRule extends AbstractFixableRule
{
protected function process(int $tokenIndex, Tokens $tokens): void
{
$token = $tokens->get($tokenIndex);
if (!$token->isMatching(Token::FUNCTION_NAME_TYPE, 'include')) {
return;
}
$fixer = $this->addFixableError(
'Render function must be used instead of include function.',
$token
);
if (null === $fixer) {
return;
}
// ...
$fixer->replaceToken($tokenIndex, 'render');
// ...
}
} |
There was a problem hiding this comment.
Minor detail, but this file shoud be named IncludeToRenderFunctionRuleTest.fixed.twig (the Test suffix is missing)
tests/Rules/Function/IncludeToRender/IncludeToRenderFunctionRuleTest.fixed.twig
|
Not sure if all would be necessary, but here are the list of tests to ensure named arguments (with {{ include('template.html', ignore_missing: true) }}
{{ include('template.html', ignore_missing: true, with_context: false) }}
{{ include('template.html', ignore_missing: true, with_context: true) }}
{{ include('template.html', ignore_missing: true, with_context: false, sandboxed: true) }}
{{ include('template.html', ignore_missing: true, with_context: true, sandboxed: true) }}
{{ include('template.html', ignore_missing: true, sandboxed: true) }}
{{ include('template.html', ignore_missing: true, sandboxed: true, with_context: true) }}
{{ include('template.html', ignore_missing: true, sandboxed: true, with_context: false) }}
{{ include('template.html', sandboxed: true) }}
{{ include('template.html', sandboxed: true, with_context: true) }}
{{ include('template.html', sandboxed: true, with_context: false) }}
{{ include('template.html', sandboxed: true, with_context: true, ignore_missing: true) }}
{{ include('template.html', sandboxed: true, with_context: false, ignore_missing: true) }}
{{ include('template.html', sandboxed: true, ignore_missing: true) }}
{{ include('template.html', sandboxed: true, ignore_missing: true, with_context: true) }}
{{ include('template.html', sandboxed: true, ignore_missing: true, with_context: false) }}
{{ include('template.html', with_context: true) }}
{{ include('template.html', with_context: true, sandboxed: true) }}
{{ include('template.html', with_context: true, sandboxed: true, ignore_missing: true) }}
{{ include('template.html', with_context: true, ignore_missing: true) }}
{{ include('template.html', with_context: true, ignore_missing: true, sandboxed: true) }}
{{ include('template.html', with_context: false) }}
{{ include('template.html', with_context: false, sandboxed: true) }}
{{ include('template.html', with_context: false, sandboxed: true, ignore_missing: true) }}
{{ include('template.html', with_context: false, ignore_missing: true) }}
{{ include('template.html', with_context: false, ignore_missing: true, sandboxed: true) }}And the fixed equivalent {{ render('template.html', _context, ignore_missing: true) }}
{{ render('template.html', ignore_missing: true) }}
{{ render('template.html', _context, ignore_missing: true) }}
{{ render('template.html', _context, ignore_missing: true, sandboxed: true) }}
{{ render('template.html', _context, ignore_missing: true, sandboxed: true) }}
{{ render('template.html', _context, ignore_missing: true, sandboxed: true) }}
{{ render('template.html', _context, ignore_missing: true, sandboxed: true) }}
{{ render('template.html', ignore_missing: true, sandboxed: true) }}
{{ render('template.html', _context, sandboxed: true) }}
{{ render('template.html', _context, sandboxed: true) }}
{{ render('template.html', sandboxed: true) }}
{{ render('template.html', _context, sandboxed: true, ignore_missing: true) }}
{{ render('template.html', sandboxed: true, ignore_missing: true) }}
{{ render('template.html', _context, sandboxed: true, ignore_missing: true) }}
{{ render('template.html', _context, sandboxed: true, ignore_missing: true) }}
{{ render('template.html', sandboxed: true, ignore_missing: true) }}
{{ render('template.html', _context) }}
{{ render('template.html', _context, sandboxed: true) }}
{{ render('template.html', _context, sandboxed: true, ignore_missing: true) }}
{{ render('template.html', _context, ignore_missing: true) }}
{{ render('template.html', _context, ignore_missing: true, sandboxed: true) }}
{{ render('template.html') }}
{{ render('template.html', sandboxed: true) }}
{{ render('template.html', sandboxed: true, ignore_missing: true) }}
{{ render('template.html', ignore_missing: true) }}
{{ render('template.html', ignore_missing: true, sandboxed: true) }} |
|
And what should be the test i think to merge context with args Arguments {# from #}
{{ include('template.html', foobar }}
{{ include('template.html', {foo: bar}) }}
{{ include('template.html', {foobar} }}
{# to #}
{{ render('template.html', {..._context, ...foobar}) }}
{{ render('template.html', {..._context, foo: bar}) }}
{{ render('template.html', {..._context, foobar: foobar}) }}Variable with_context {# from #}
{{ include('template.html', foobar, with_context: var_name) }}
{{ include('template.html', {foo: bar}, with_context: var_name) }}
{{ include('template.html', {foobar}, with_context: var_name) }}
{# to #}
{{ render('template.html', {...(var_name ? _context : []), ...foobar}) }}
{{ render('template.html', {...(var_name ? _context : []), foo: bar}) }}
{{ render('template.html', {...(var_name ? _context : []), foobar: foobar}) }}(need other opinions here... but these syntaxes may cover most of the needs) |
|
This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This is just some example test case how we may could migrate current include to a new render method or whatever it will be called.
twigphp/Twig#4434 (still in discussion)