minor #2270 improved a deprecation notice (fabpot)

This PR was merged into the 1.x branch.

Discussion
----------

improved a deprecation notice

Commits
-------

0595c40 improved a deprecation notice
This commit is contained in:
Fabien Potencier
2016-11-19 11:25:47 -08:00
6 changed files with 105 additions and 15 deletions
+1 -1
View File
@@ -30,7 +30,7 @@
"php": ">=5.2.7"
},
"require-dev": {
"symfony/phpunit-bridge": "~2.7",
"symfony/phpunit-bridge": "~3.2@dev",
"symfony/debug": "~2.7"
},
"autoload": {
+12 -3
View File
@@ -314,8 +314,8 @@ abstract class Twig_Template implements Twig_TemplateInterface
* This method checks blocks defined in the current template
* or defined in "used" traits or defined in parent templates.
*
* @param array $context The context
* @param array $blocks The current set of blocks
* @param array $context The context
* @param array $blocks The current set of blocks
*
* @return array An array of block names
*
@@ -675,7 +675,16 @@ abstract class Twig_Template implements Twig_TemplateInterface
// @deprecated in 1.28
if ($object instanceof Twig_TemplateInterface) {
@trigger_error('Using the dot notation on an instance of '.__CLASS__.' is deprecated since version 1.28 and won\'t be supported anymore in 2.0.', E_USER_DEPRECATED);
$self = $object->getTemplateName() === $this->getTemplateName();
$message = sprintf('Calling "%s" on template "%s" from template "%s" is deprecated since version 1.28 and won\'t be supported anymore in 2.0.', $method, $object->getTemplateName(), $this->getTemplateName());
if ('renderBlock' === $method || 'displayBlock' === $method) {
$message .= sprintf(' Use block("%s"%s) instead).', $arguments[0], $self ? '' : ', template');
} elseif ('hasBlock' === $method) {
$message .= sprintf(' Use block("%s"%s) is defined instead).', $arguments[0], $self ? '' : ', template');
} elseif ('render' === $method || 'display' === $method) {
$message .= sprintf(' Use include("%s") instead).', $object->getTemplateName());
}
@trigger_error($message, E_USER_DEPRECATED);
return $ret === '' ? '' : new Twig_Markup($ret, $this->env->getCharset());
}
+8
View File
@@ -17,6 +17,14 @@
</testsuite>
</testsuites>
<php>
<ini name="error_reporting" value="-1" />
</php>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
<filter>
<whitelist>
<directory suffix=".php">./lib/Twig/</directory>
+1
View File
@@ -646,6 +646,7 @@ class Twig_Tests_EnvironmentTest_Runtime
}
}
// to be removed in 2.0
interface Twig_EnvironmentTestLoaderInterface extends Twig_LoaderInterface, Twig_SourceContextLoaderInterface
{
}
+3
View File
@@ -11,6 +11,9 @@
class Twig_Tests_NativeExtensionTest extends PHPUnit_Framework_TestCase
{
/**
* @requires PHP 5.3
*/
public function testGetProperties()
{
if (defined('HHVM_VERSION')) {
+80 -11
View File
@@ -134,8 +134,12 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
*/
public function testGetAttributeWithTemplateAsObject($useExt)
{
$template = new Twig_TemplateTest(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()), $useExt);
$template1 = new Twig_TemplateTest(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()), false);
// to be removed in 2.0
$twig = new Twig_Environment($this->getMockBuilder('Twig_TemplateTestLoaderInterface')->getMock());
//$twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface', 'Twig_SourceContextLoaderInterface')->getMock());
$template = new Twig_TemplateTest($twig, $useExt, 'index.twig');
$template1 = new Twig_TemplateTest($twig, false, 'index1.twig');
$this->assertInstanceof('Twig_Markup', $template->getAttribute($template1, 'string'));
$this->assertEquals('some_string', $template->getAttribute($template1, 'string'));
@@ -168,6 +172,67 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
return $bools;
}
/**
* @group legacy
* @expectedDeprecation Calling "getString" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0.
* @expectedDeprecation Calling "getString" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0.
* @expectedDeprecation Calling "getTrue" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0.
* @expectedDeprecation Calling "getTrue" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0.
* @expectedDeprecation Calling "getZero" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0.
* @expectedDeprecation Calling "getZero" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0.
* @expectedDeprecation Calling "getEmpty" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0.
* @expectedDeprecation Calling "getEmpty" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0.
* @expectedDeprecation Calling "renderBlock" on template "index.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use block("name") instead).
* @expectedDeprecation Calling "displayBlock" on template "index.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use block("name") instead).
* @expectedDeprecation Calling "hasBlock" on template "index.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use block("name") is defined instead).
* @expectedDeprecation Calling "render" on template "index.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use include("index.twig") instead).
* @expectedDeprecation Calling "display" on template "index.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use include("index.twig") instead).
* @expectedDeprecation Calling "renderBlock" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use block("name", template) instead).
* @expectedDeprecation Calling "displayBlock" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use block("name", template) instead).
* @expectedDeprecation Calling "hasBlock" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use block("name", template) is defined instead).
* @expectedDeprecation Calling "render" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use include("index1.twig") instead).
* @expectedDeprecation Calling "display" on template "index1.twig" from template "index.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0. Use include("index1.twig") instead).
*/
public function testGetAttributeWithTemplateAsObjectForDeprecations()
{
// to be removed in 2.0
$twig = new Twig_Environment($this->getMockBuilder('Twig_TemplateTestLoaderInterface')->getMock());
//$twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface', 'Twig_SourceContextLoaderInterface')->getMock());
$template = new Twig_TemplateTest($twig, false, 'index.twig');
$template1 = new Twig_TemplateTest($twig, false, 'index1.twig');
$this->assertInstanceof('Twig_Markup', $template->getAttribute($template1, 'string'));
$this->assertEquals('some_string', $template->getAttribute($template1, 'string'));
$this->assertInstanceof('Twig_Markup', $template->getAttribute($template1, 'true'));
$this->assertEquals('1', $template->getAttribute($template1, 'true'));
$this->assertInstanceof('Twig_Markup', $template->getAttribute($template1, 'zero'));
$this->assertEquals('0', $template->getAttribute($template1, 'zero'));
$this->assertNotInstanceof('Twig_Markup', $template->getAttribute($template1, 'empty'));
$this->assertSame('', $template->getAttribute($template1, 'empty'));
// trigger some deprecation notice messages to check them with @expectedDeprecation
$template->getAttribute($template, 'renderBlock', array('name', array()));
$template->getAttribute($template, 'displayBlock', array('name', array()));
$template->getAttribute($template, 'hasBlock', array('name', array()));
$template->getAttribute($template, 'render', array(array()));
$template->getAttribute($template, 'display', array(array()));
$template->getAttribute($template1, 'renderBlock', array('name', array()));
$template->getAttribute($template1, 'displayBlock', array('name', array()));
$template->getAttribute($template1, 'hasBlock', array('name', array()));
$template->getAttribute($template1, 'render', array(array()));
$template->getAttribute($template1, 'display', array(array()));
$this->assertFalse($template->getAttribute($template1, 'env', array(), Twig_Template::ANY_CALL, true));
$this->assertFalse($template->getAttribute($template1, 'environment', array(), Twig_Template::ANY_CALL, true));
$this->assertFalse($template->getAttribute($template1, 'getEnvironment', array(), Twig_Template::METHOD_CALL, true));
$this->assertFalse($template->getAttribute($template1, 'displayWithErrorHandling', array(), Twig_Template::METHOD_CALL, true));
}
/**
* @dataProvider getTestsDependingOnExtensionAvailability
*/
@@ -388,9 +453,9 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
// tests when input is not an array or object
$tests = array_merge($tests, array(
array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a integer variable ("42").'),
array(false, null, 'string', 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a string variable ("string").'),
array(false, null, array(), 'a', array(), $anyType, false, 'Key "a" does not exist as the array is empty.'),
array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a integer variable ("42") in "index.twig".'),
array(false, null, 'string', 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a string variable ("string") in "index.twig".'),
array(false, null, array(), 'a', array(), $anyType, false, 'Key "a" does not exist as the array is empty in "index.twig".'),
));
// add twig_template_get_attributes tests
@@ -410,12 +475,14 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
class Twig_TemplateTest extends Twig_Template
{
protected $useExtGetAttribute = false;
private $name;
public function __construct(Twig_Environment $env, $useExtGetAttribute = false)
public function __construct(Twig_Environment $env, $useExtGetAttribute = false, $name = 'index.twig')
{
parent::__construct($env);
$this->useExtGetAttribute = $useExtGetAttribute;
self::$cache = array();
$this->name = $name;
}
public function getZero()
@@ -440,6 +507,7 @@ class Twig_TemplateTest extends Twig_Template
public function getTemplateName()
{
return $this->name;
}
public function getDebugInfo()
@@ -447,13 +515,9 @@ class Twig_TemplateTest extends Twig_Template
return array();
}
public function getSource()
{
return '';
}
protected function doGetParent(array $context)
{
return false;
}
protected function doDisplay(array $context, array $blocks = array())
@@ -688,3 +752,8 @@ class CExtDisablingNodeVisitor implements Twig_NodeVisitorInterface
return 0;
}
}
// to be removed in 2.0
interface Twig_TemplateTestLoaderInterface extends Twig_LoaderInterface, Twig_SourceContextLoaderInterface
{
}