Browse Source

Remove getView()

Its not necessary anymore. Maintaining additional view state isn't
required for either Controller or Cell.
Mark Story 10 years ago
parent
commit
1ed2a63861

+ 5 - 5
src/View/Cell.php

@@ -42,7 +42,7 @@ abstract class Cell
      * Cell::__toString() is called.
      *
      * @var \Cake\View\View
-     * @deprecated 3.1.0 Use getView() instead.
+     * @deprecated 3.1.0 Use createView() instead.
      */
     public $View;
 
@@ -168,22 +168,22 @@ abstract class Cell
         if ($this->_cache) {
             $cache = $this->_cacheConfig($template);
         }
-        $this->View = $this->getView();
+        $this->View = $this->createView();
 
         $render = function () use ($template) {
             $className = substr(strrchr(get_class($this), "\\"), 1);
             $name = substr($className, 0, -4);
-            $this->_view->viewPath('Cell' . DS . $name);
+            $this->View->viewPath('Cell' . DS . $name);
 
             try {
-                return $this->_view->render($template);
+                return $this->View->render($template);
             } catch (MissingTemplateException $e) {
                 throw new MissingCellViewException(['file' => $template, 'name' => $name]);
             }
         };
 
         if ($cache) {
-            return $this->_view->cache(function () use ($render) {
+            return $this->View->cache(function () use ($render) {
                 echo $render();
             }, $cache);
         }

+ 0 - 33
src/View/ViewVarsTrait.php

@@ -35,15 +35,6 @@ trait ViewVarsTrait
     public $viewClass = null;
 
     /**
-     * View instance.
-     *
-     * Won't be set until after ViewVarsTrait::createView() is called.
-     *
-     * @var \Cake\View\View
-     */
-    public $_view;
-
-    /**
      * Variables for the view
      *
      * @var array
@@ -71,30 +62,6 @@ trait ViewVarsTrait
     }
 
     /**
-     * Get a view instance.
-     *
-     * @param string|null $viewClass View class name or null to use $viewClass
-     * @return \Cake\View\View
-     * @throws \Cake\View\Exception\MissingViewException If view class was not found.
-     * @deprecated 3.1.0 Use the viewBuilder() method to define view properties
-     *   before building a view with createView().
-     */
-    public function getView($viewClass = null)
-    {
-        if ($viewClass === null) {
-            $viewClass = $this->viewClass;
-        }
-        if ($viewClass) {
-            $this->_view = null;
-        }
-        if ($this->_view === null) {
-            $this->_view = $this->createView($viewClass);
-        }
-        $this->_view->viewVars = $this->viewVars;
-        return $this->_view;
-    }
-
-    /**
      * Constructs the view class instance based on the current configuration.
      *
      * @param string|null $viewClass Optional namespaced class name of the View class to instantiate.

+ 5 - 7
tests/TestCase/Controller/ControllerTest.php

@@ -402,12 +402,10 @@ class ControllerTest extends TestCase
         $result = $Controller->render('index');
         $this->assertRegExp('/posts index/', (string)$result);
 
-        $Controller->getView()->view = 'index';
-        $Controller->getView()->hasRendered = false;
+        $Controller->viewBuilder()->template('index');
         $result = $Controller->render();
         $this->assertRegExp('/posts index/', (string)$result);
 
-        $Controller->getView()->hasRendered = false;
         $result = $Controller->render('/Element/test_element');
         $this->assertRegExp('/this is the test element/', (string)$result);
     }
@@ -871,7 +869,7 @@ class ControllerTest extends TestCase
             return $e->subject()->response;
         });
         $Controller->render();
-        $this->assertEquals('Admin' . DS . 'Posts', $Controller->getView()->viewPath);
+        $this->assertEquals('Admin' . DS . 'Posts', $Controller->viewBuilder()->viewPath());
 
         $request->addParams([
             'prefix' => 'admin/super'
@@ -882,7 +880,7 @@ class ControllerTest extends TestCase
             return $e->subject()->response;
         });
         $Controller->render();
-        $this->assertEquals('Admin' . DS . 'Super' . DS . 'Posts', $Controller->getView()->viewPath);
+        $this->assertEquals('Admin' . DS . 'Super' . DS . 'Posts', $Controller->viewBuilder()->viewPath());
 
         $request = new Request('pages/home');
         $request->addParams([
@@ -893,7 +891,7 @@ class ControllerTest extends TestCase
             return $e->subject()->response;
         });
         $Controller->render();
-        $this->assertEquals('Pages', $Controller->getView()->viewPath);
+        $this->assertEquals('Pages', $Controller->viewBuilder()->viewPath());
     }
 
     /**
@@ -979,7 +977,7 @@ class ControllerTest extends TestCase
         $theme = $controller->theme;
 
         // @codingStandardsIgnoreStart
-        $this->assertEquals($theme, @$controller->getView()->theme);
+        $this->assertEquals($theme, @$controller->createView()->theme);
         // @codingStandardsIgnoreEnd
     }
 

+ 0 - 1
tests/TestCase/Error/DebuggerTest.php

@@ -279,7 +279,6 @@ object(Cake\View\View) {
 	response => object(Cake\Network\Response) {}
 	elementCache => 'default'
 	viewClass => null
-	_view => null
 	viewVars => []
 	Html => object(Cake\View\Helper\HtmlHelper) {}
 	Form => object(Cake\View\Helper\FormHelper) {}

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

@@ -1319,7 +1319,7 @@ class ViewTest extends TestCase
         $Controller = new ViewPostsController();
         $Controller->helpers = ['Html'];
         $Controller->set('html', 'I am some test html');
-        $View = $Controller->getView();
+        $View = $Controller->createView();
         $View->viewPath = $Controller->name;
         $result = $View->render('helper_overwrite', false);
 
@@ -1334,7 +1334,7 @@ class ViewTest extends TestCase
      */
     public function testViewFileName()
     {
-        $View = $this->PostsController->getView('Cake\Test\TestCase\View\TestView');
+        $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
         $View->viewPath = 'Posts';
 
         $result = $View->getViewFileName('index');

+ 4 - 16
tests/TestCase/View/ViewVarsTraitTest.php

@@ -161,7 +161,7 @@ class ViewVarsTraitTest extends TestCase
     }
 
     /**
-     * test that getView() updates viewVars of View instance on each call.
+     * test that createView() updates viewVars of View instance on each call.
      *
      * @return void
      */
@@ -169,23 +169,11 @@ class ViewVarsTraitTest extends TestCase
     {
         $expected = ['one' => 'one'];
         $this->subject->set($expected);
-        $this->assertEquals($expected, $this->subject->getView()->viewVars);
+        $this->assertEquals($expected, $this->subject->createView()->viewVars);
 
         $expected = ['one' => 'one', 'two' => 'two'];
         $this->subject->set($expected);
-        $this->assertEquals($expected, $this->subject->getView()->viewVars);
-    }
-
-    /**
-     * test getView() throws exception if view class cannot be found
-     *
-     * @expectedException \Cake\View\Exception\MissingViewException
-     * @expectedExceptionMessage View class "Foo" is missing.
-     * @return void
-     */
-    public function testGetViewException()
-    {
-        $this->subject->getView('Foo');
+        $this->assertEquals($expected, $this->subject->createView()->viewVars);
     }
 
     /**
@@ -197,6 +185,6 @@ class ViewVarsTraitTest extends TestCase
      */
     public function testCreateViewException()
     {
-        $this->subject->getView('Foo');
+        $this->subject->createView('Foo');
     }
 }