Browse Source

Make View::$helpers protected.

ADmad 7 years ago
parent
commit
71b862dddd
3 changed files with 30 additions and 7 deletions
  1. 19 1
      src/View/View.php
  2. 8 3
      tests/TestCase/View/CellTest.php
  3. 3 3
      tests/TestCase/View/ViewTest.php

+ 19 - 1
src/View/View.php

@@ -123,7 +123,7 @@ class View implements EventDispatcherInterface
      *
      * @var array
      */
-    public $helpers = [];
+    protected $helpers = [];
 
     /**
      * The name of the subfolder containing templates for this View.
@@ -1221,6 +1221,15 @@ class View implements EventDispatcherInterface
             return $this->Blocks;
         }
 
+        if ($name === 'helpers') {
+            deprecationWarning(
+                'View::$helpers is protected now. ' .
+                'Use the helper registry through View::helpers() to manage helpers.'
+            );
+
+            return $this->helpers;
+        }
+
         $registry = $this->helpers();
         if (isset($registry->{$name})) {
             $this->{$name} = $registry->{$name};
@@ -1282,6 +1291,15 @@ class View implements EventDispatcherInterface
             return;
         }
 
+        if ($name === 'helpers') {
+            deprecationWarning(
+                'View::$helpers is protected now. ' .
+                'Use the helper registry through View::helpers() to manage helpers.'
+            );
+
+            return $this->helpers = $value;
+        }
+
         $this->{$name} = $value;
     }
 

+ 8 - 3
tests/TestCase/View/CellTest.php

@@ -347,9 +347,14 @@ class CellTest extends TestCase
      */
     public function testCellInheritsHelperConfig()
     {
-        $this->View->helpers = ['Url', 'Form', 'Banana'];
-        $cell = $this->View->cell('Articles');
-        $this->assertSame($this->View->helpers, $cell->helpers);
+        $request = $this->getMockBuilder('Cake\Http\ServerRequest')->getMock();
+        $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
+        $helpers = ['Url', 'Form', 'Banana'];
+
+        $view = new View($request, $response, null, ['helpers' => $helpers]);
+
+        $cell = $view->cell('Articles');
+        $this->assertSame($helpers, $cell->helpers);
     }
 
     /**

+ 3 - 3
tests/TestCase/View/ViewTest.php

@@ -1139,9 +1139,10 @@ class ViewTest extends TestCase
      */
     public function testLoadHelpers()
     {
-        $View = new View();
+        $View = new View(null, null, null, [
+            'helpers' => ['Html' => ['foo' => 'bar'], 'Form' => ['foo' => 'baz']]
+        ]);
 
-        $View->helpers = ['Html' => ['foo' => 'bar'], 'Form' => ['foo' => 'baz']];
         $result = $View->loadHelpers();
         $this->assertSame($View, $result);
 
@@ -1164,7 +1165,6 @@ class ViewTest extends TestCase
     {
         $View = new View();
 
-        $View->helpers = [];
         $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
         $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
     }