Browse code

improved a deprecation notice

Fabien Potencier authored on 19/11/2016 18:48:18
Showing 6 changed files
... ...
@@ -30,7 +30,7 @@
30 30
         "php": ">=5.2.7"
31 31
     },
32 32
     "require-dev": {
33
-        "symfony/phpunit-bridge": "~2.7",
33
+        "symfony/phpunit-bridge": "~3.2@dev",
34 34
         "symfony/debug": "~2.7"
35 35
     },
36 36
     "autoload": {
... ...
@@ -314,8 +314,8 @@ abstract class Twig_Template implements Twig_TemplateInterface
314 314
      * This method checks blocks defined in the current template
315 315
      * or defined in "used" traits or defined in parent templates.
316 316
      *
317
-     * @param array  $context The context
318
-     * @param array  $blocks  The current set of blocks
317
+     * @param array $context The context
318
+     * @param array $blocks  The current set of blocks
319 319
      *
320 320
      * @return array An array of block names
321 321
      *
... ...
@@ -675,7 +675,16 @@ abstract class Twig_Template implements Twig_TemplateInterface
675 675
 
676 676
         // @deprecated in 1.28
677 677
         if ($object instanceof Twig_TemplateInterface) {
678
-            @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);
678
+            $self = $object->getTemplateName() === $this->getTemplateName();
679
+            $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());
680
+            if ('renderBlock' === $method || 'displayBlock' === $method) {
681
+                $message .= sprintf(' Use block("%s"%s) instead).', $arguments[0], $self ? '' : ', template');
682
+            } elseif ('hasBlock' === $method) {
683
+                $message .= sprintf(' Use block("%s"%s) is defined instead).', $arguments[0], $self ? '' : ', template');
684
+            } elseif ('render' === $method || 'display' === $method) {
685
+                $message .= sprintf(' Use include("%s") instead).', $object->getTemplateName());
686
+            }
687
+            @trigger_error($message, E_USER_DEPRECATED);
679 688
 
680 689
             return $ret === '' ? '' : new Twig_Markup($ret, $this->env->getCharset());
681 690
         }
... ...
@@ -17,6 +17,14 @@
17 17
     </testsuite>
18 18
   </testsuites>
19 19
 
20
+  <php>
21
+    <ini name="error_reporting" value="-1" />
22
+  </php>
23
+
24
+  <listeners>
25
+    <listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
26
+  </listeners>
27
+
20 28
   <filter>
21 29
     <whitelist>
22 30
       <directory suffix=".php">./lib/Twig/</directory>
... ...
@@ -646,6 +646,7 @@ class Twig_Tests_EnvironmentTest_Runtime
646 646
     }
647 647
 }
648 648
 
649
+// to be removed in 2.0
649 650
 interface Twig_EnvironmentTestLoaderInterface extends Twig_LoaderInterface, Twig_SourceContextLoaderInterface
650 651
 {
651 652
 }
... ...
@@ -11,6 +11,9 @@
11 11
 
12 12
 class Twig_Tests_NativeExtensionTest extends PHPUnit_Framework_TestCase
13 13
 {
14
+    /**
15
+     * @requires PHP 5.3
16
+     */
14 17
     public function testGetProperties()
15 18
     {
16 19
         if (defined('HHVM_VERSION')) {
... ...
@@ -134,8 +134,12 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
134 134
      */
135 135
     public function testGetAttributeWithTemplateAsObject($useExt)
136 136
     {
137
-        $template = new Twig_TemplateTest(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()), $useExt);
138
-        $template1 = new Twig_TemplateTest(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()), false);
137
+        // to be removed in 2.0
138
+        $twig = new Twig_Environment($this->getMockBuilder('Twig_TemplateTestLoaderInterface')->getMock());
139
+        //$twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface', 'Twig_SourceContextLoaderInterface')->getMock());
140
+
141
+        $template = new Twig_TemplateTest($twig, $useExt, 'index.twig');
142
+        $template1 = new Twig_TemplateTest($twig, false, 'index1.twig');
139 143
 
140 144
         $this->assertInstanceof('Twig_Markup', $template->getAttribute($template1, 'string'));
141 145
         $this->assertEquals('some_string', $template->getAttribute($template1, 'string'));
... ...
@@ -169,6 +173,67 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
169 173
     }
170 174
 
171 175
     /**
176
+     * @group legacy
177
+     * @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.
178
+     * @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.
179
+     * @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.
180
+     * @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.
181
+     * @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.
182
+     * @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.
183
+     * @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.
184
+     * @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.
185
+     * @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).
186
+     * @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).
187
+     * @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).
188
+     * @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).
189
+     * @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).
190
+     * @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).
191
+     * @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).
192
+     * @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).
193
+     * @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).
194
+     * @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).
195
+     */
196
+    public function testGetAttributeWithTemplateAsObjectForDeprecations()
197
+    {
198
+        // to be removed in 2.0
199
+        $twig = new Twig_Environment($this->getMockBuilder('Twig_TemplateTestLoaderInterface')->getMock());
200
+        //$twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface', 'Twig_SourceContextLoaderInterface')->getMock());
201
+
202
+        $template = new Twig_TemplateTest($twig, false, 'index.twig');
203
+        $template1 = new Twig_TemplateTest($twig, false, 'index1.twig');
204
+
205
+        $this->assertInstanceof('Twig_Markup', $template->getAttribute($template1, 'string'));
206
+        $this->assertEquals('some_string', $template->getAttribute($template1, 'string'));
207
+
208
+        $this->assertInstanceof('Twig_Markup', $template->getAttribute($template1, 'true'));
209
+        $this->assertEquals('1', $template->getAttribute($template1, 'true'));
210
+
211
+        $this->assertInstanceof('Twig_Markup', $template->getAttribute($template1, 'zero'));
212
+        $this->assertEquals('0', $template->getAttribute($template1, 'zero'));
213
+
214
+        $this->assertNotInstanceof('Twig_Markup', $template->getAttribute($template1, 'empty'));
215
+        $this->assertSame('', $template->getAttribute($template1, 'empty'));
216
+
217
+        // trigger some deprecation notice messages to check them with @expectedDeprecation
218
+        $template->getAttribute($template, 'renderBlock', array('name', array()));
219
+        $template->getAttribute($template, 'displayBlock', array('name', array()));
220
+        $template->getAttribute($template, 'hasBlock', array('name', array()));
221
+        $template->getAttribute($template, 'render', array(array()));
222
+        $template->getAttribute($template, 'display', array(array()));
223
+
224
+        $template->getAttribute($template1, 'renderBlock', array('name', array()));
225
+        $template->getAttribute($template1, 'displayBlock', array('name', array()));
226
+        $template->getAttribute($template1, 'hasBlock', array('name', array()));
227
+        $template->getAttribute($template1, 'render', array(array()));
228
+        $template->getAttribute($template1, 'display', array(array()));
229
+
230
+        $this->assertFalse($template->getAttribute($template1, 'env', array(), Twig_Template::ANY_CALL, true));
231
+        $this->assertFalse($template->getAttribute($template1, 'environment', array(), Twig_Template::ANY_CALL, true));
232
+        $this->assertFalse($template->getAttribute($template1, 'getEnvironment', array(), Twig_Template::METHOD_CALL, true));
233
+        $this->assertFalse($template->getAttribute($template1, 'displayWithErrorHandling', array(), Twig_Template::METHOD_CALL, true));
234
+    }
235
+
236
+    /**
172 237
      * @dataProvider getTestsDependingOnExtensionAvailability
173 238
      */
174 239
     public function testGetAttributeOnArrayWithConfusableKey($useExt = false)
... ...
@@ -388,9 +453,9 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
388 453
 
389 454
         // tests when input is not an array or object
390 455
         $tests = array_merge($tests, array(
391
-            array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a integer variable ("42").'),
392
-            array(false, null, 'string', 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a string variable ("string").'),
393
-            array(false, null, array(), 'a', array(), $anyType, false, 'Key "a" does not exist as the array is empty.'),
456
+            array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a integer variable ("42") in "index.twig".'),
457
+            array(false, null, 'string', 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a string variable ("string") in "index.twig".'),
458
+            array(false, null, array(), 'a', array(), $anyType, false, 'Key "a" does not exist as the array is empty in "index.twig".'),
394 459
         ));
395 460
 
396 461
         // add twig_template_get_attributes tests
... ...
@@ -410,12 +475,14 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
410 475
 class Twig_TemplateTest extends Twig_Template
411 476
 {
412 477
     protected $useExtGetAttribute = false;
478
+    private $name;
413 479
 
414
-    public function __construct(Twig_Environment $env, $useExtGetAttribute = false)
480
+    public function __construct(Twig_Environment $env, $useExtGetAttribute = false, $name = 'index.twig')
415 481
     {
416 482
         parent::__construct($env);
417 483
         $this->useExtGetAttribute = $useExtGetAttribute;
418 484
         self::$cache = array();
485
+        $this->name = $name;
419 486
     }
420 487
 
421 488
     public function getZero()
... ...
@@ -440,6 +507,7 @@ class Twig_TemplateTest extends Twig_Template
440 507
 
441 508
     public function getTemplateName()
442 509
     {
510
+        return $this->name;
443 511
     }
444 512
 
445 513
     public function getDebugInfo()
... ...
@@ -447,13 +515,9 @@ class Twig_TemplateTest extends Twig_Template
447 515
         return array();
448 516
     }
449 517
 
450
-    public function getSource()
451
-    {
452
-        return '';
453
-    }
454
-
455 518
     protected function doGetParent(array $context)
456 519
     {
520
+        return false;
457 521
     }
458 522
 
459 523
     protected function doDisplay(array $context, array $blocks = array())
... ...
@@ -688,3 +752,8 @@ class CExtDisablingNodeVisitor implements Twig_NodeVisitorInterface
688 752
         return 0;
689 753
     }
690 754
 }
755
+
756
+// to be removed in 2.0
757
+interface Twig_TemplateTestLoaderInterface extends Twig_LoaderInterface, Twig_SourceContextLoaderInterface
758
+{
759
+}