Browse Source

Merge pull request #8802 from cakephp/issue-8770

Fix viewBuilder() config not taking precedence.
Mark Story 10 years ago
parent
commit
39b5ae7ac6
2 changed files with 40 additions and 2 deletions
  1. 2 2
      src/View/ViewVarsTrait.php
  2. 38 0
      tests/TestCase/View/ViewVarsTraitTest.php

+ 2 - 2
src/View/ViewVarsTrait.php

@@ -70,8 +70,8 @@ trait ViewVarsTrait
     public function createView($viewClass = null)
     {
         $builder = $this->viewBuilder();
-        if ($viewClass === null) {
-            $viewClass = $this->viewClass;
+        if ($viewClass === null && $builder->className() === null) {
+            $builder->className($this->viewClass);
         }
         if ($viewClass) {
             $builder->className($viewClass);

+ 38 - 0
tests/TestCase/View/ViewVarsTraitTest.php

@@ -189,6 +189,44 @@ class ViewVarsTraitTest extends TestCase
     }
 
     /**
+     * test that viewClass is used to create the view
+     *
+     * @return void
+     */
+    public function testCreateViewViewClass()
+    {
+        $this->subject->viewClass = 'Json';
+        $view = $this->subject->createView();
+        $this->assertInstanceof('Cake\View\JsonView', $view);
+    }
+
+    /**
+     * test that viewBuilder settings override viewClass
+     *
+     * @return void
+     */
+    public function testCreateViewViewBuilder()
+    {
+        $this->subject->viewBuilder()->className('Xml');
+        $this->subject->viewClass = 'Json';
+        $view = $this->subject->createView();
+        $this->assertInstanceof('Cake\View\XmlView', $view);
+    }
+
+    /**
+     * test that parameters beats viewBuilder() and viewClass
+     *
+     * @return void
+     */
+    public function testCreateViewParameter()
+    {
+        $this->subject->viewBuilder()->className('View');
+        $this->subject->viewClass = 'Json';
+        $view = $this->subject->createView('Xml');
+        $this->assertInstanceof('Cake\View\XmlView', $view);
+    }
+
+    /**
      * test createView() throws exception if view class cannot be found
      *
      * @expectedException \Cake\View\Exception\MissingViewException