Skip to content
Permalink
Browse files

Correct deprecation warnings for viewBuilder properties.

Fix warnings for view builder related properties. The old deprecation
warnings recommended using mehods that are also deprecated. I've added
an unset() for viewClass to avoid a permanent deprecation warning when
RequestHandlerComponent is used. I wasn't brave enough to remove that
property set as there is probably userland code hanging on it.
  • Loading branch information...
markstory committed May 2, 2018
1 parent 9ff2776 commit c464f07c2512544f5bd1ab467339d4db8ba01fa8
@@ -757,8 +757,6 @@ public function render($view = null, $layout = null)
if ($this->getRequest()->getParam('bare')) {
$builder->enableAutoLayout(false);
}
$builder->getClassName($this->viewClass);
$this->autoRender = false;
$event = $this->dispatchEvent('Controller.beforeRender');
@@ -74,6 +74,7 @@ public function createView($viewClass = null)
$builder = $this->viewBuilder();
if ($viewClass === null && $builder->getClassName() === null) {
$builder->setClassName($this->viewClass);
unset($this->viewClass);
}
if ($viewClass) {
$builder->setClassName($viewClass);
@@ -88,21 +89,22 @@ public function createView($viewClass = null)
}
$deprecatedOptions = [
'layout' => 'layout',
'view' => 'template',
'theme' => 'theme',
'autoLayout' => 'autoLayout',
'viewPath' => 'templatePath',
'layoutPath' => 'layoutPath',
'layout' => 'setLayout',
'view' => 'setTemplate',
'theme' => 'setTheme',
'autoLayout' => 'enableAutoLayout',
'viewPath' => 'setTemplatePath',
'layoutPath' => 'setLayoutPath',
];
foreach ($deprecatedOptions as $old => $new) {
if (property_exists($this, $old)) {
$builder->{$new}($this->{$old});
trigger_error(sprintf(
$message = sprintf(
'Property $%s is deprecated. Use $this->viewBuilder()->%s() instead in beforeRender().',
$old,
$new
), E_USER_DEPRECATED);
);
deprecationWarning($message);
}
}
@@ -22,6 +22,9 @@
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
use Cake\View\AjaxView;
use Cake\View\JsonView;
use Cake\View\XmlView;
use TestApp\Controller\RequestHandlerTestController;
use Zend\Diactoros\Stream;
@@ -399,7 +402,7 @@ public function testViewClassMapMethod()
$this->assertEquals($expected, $result);
$this->RequestHandler->renderAs($this->Controller, 'json');
$this->assertEquals('TestApp\View\CustomJsonView', $this->Controller->viewClass);
$this->assertEquals('TestApp\View\CustomJsonView', $this->Controller->viewBuilder()->getClassName());
});
}
@@ -434,15 +437,15 @@ public function testAutoAjaxLayout()
$event = new Event('Controller.beforeRender', $this->Controller);
$this->RequestHandler->beforeRender($event);
$this->assertEquals($this->Controller->viewClass, 'Cake\View\AjaxView');
$view = $this->Controller->createView();
$this->assertInstanceOf(AjaxView::class, $view);
$this->assertEquals('ajax', $view->getLayout());
$this->_init();
$this->Controller->request = $this->Controller->request->withParam('_ext', 'js');
$this->RequestHandler->initialize([]);
$this->RequestHandler->startup($event);
$this->assertNotEquals($this->Controller->viewClass, 'Cake\View\AjaxView');
$this->assertNotEquals(AjaxView::class, $this->Controller->viewBuilder()->getClassName());
}
/**
@@ -460,8 +463,8 @@ public function testJsonViewLoaded()
$this->RequestHandler->startup($event);
$event = new Event('Controller.beforeRender', $this->Controller);
$this->RequestHandler->beforeRender($event);
$this->assertEquals('Cake\View\JsonView', $this->Controller->viewClass);
$view = $this->Controller->createView();
$this->assertInstanceOf(JsonView::class, $view);
$this->assertEquals('json', $view->getLayoutPath());
$this->assertEquals('json', $view->subDir);
}
@@ -481,8 +484,8 @@ public function testXmlViewLoaded()
$this->RequestHandler->startup($event);
$event = new Event('Controller.beforeRender', $this->Controller);
$this->RequestHandler->beforeRender($event);
$this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
$view = $this->Controller->createView();
$this->assertInstanceOf(XmlView::class, $view);
$this->assertEquals('xml', $view->getLayoutPath());
$this->assertEquals('xml', $view->subDir);
}
@@ -502,8 +505,8 @@ public function testAjaxViewLoaded()
$this->RequestHandler->startup($event);
$event = new Event('Controller.beforeRender', $this->Controller);
$this->RequestHandler->beforeRender($event);
$this->assertEquals('Cake\View\AjaxView', $this->Controller->viewClass);
$view = $this->Controller->createView();
$this->assertInstanceOf(AjaxView::class, $view);
$this->assertEquals('ajax', $view->getLayout());
}
@@ -866,7 +869,7 @@ public function testRenderAsWithAttachment()
$this->Controller->request = $this->request->withHeader('Accept', 'application/xml;q=1.0');
$this->RequestHandler->renderAs($this->Controller, 'xml', ['attachment' => 'myfile.xml']);
$this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
$this->assertEquals(XmlView::class, $this->Controller->viewBuilder()->getClassName());
$this->assertEquals('application/xml', $this->Controller->response->getType());
$this->assertEquals('UTF-8', $this->Controller->response->getCharset());
$this->assertContains('myfile.xml', $this->Controller->response->getHeaderLine('Content-Disposition'));
@@ -223,7 +223,6 @@ public function testCellRenderThemed()
$this->assertEquals($this->View->theme, $cell->viewBuilder()->getTheme());
$this->assertContains('Themed cell content.', $cell->render());
$this->assertEquals($cell->View->theme, $cell->viewBuilder()->getTheme());
}
/**
@@ -195,13 +195,16 @@ public function testViewOptionsGetsToBuilder()
/**
* test that viewClass is used to create the view
*
* @deprecated
* @return void
*/
public function testCreateViewViewClass()
{
$this->subject->viewClass = 'Json';
$view = $this->subject->createView();
$this->assertInstanceOf('Cake\View\JsonView', $view);
$this->deprecated(function () {
$this->subject->viewClass = 'Json';
$view = $this->subject->createView();
$this->assertInstanceOf('Cake\View\JsonView', $view);
});
}
/**
@@ -47,7 +47,7 @@ public function testRenderWithoutView()
$Controller = new Controller($Request, $Response);
$data = ['users' => ['user' => ['user1', 'user2']]];
$Controller->set(['users' => $data, '_serialize' => 'users']);
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$output = $View->render(false);
@@ -67,15 +67,15 @@ public function testRenderWithoutView()
]
];
$Controller->set(['users' => $data, '_serialize' => 'users']);
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$output = $View->render(false);
$expected = Xml::build(['response' => ['users' => $data]])->asXML();
$this->assertSame($expected, $output);
$Controller->set('_rootNode', 'custom_name');
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$output = $View->render(false);
@@ -98,7 +98,7 @@ public function testRenderSerializeNoHelpers()
'_serialize' => 'tags',
'tags' => ['cakephp', 'framework']
]);
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$View->render();
$this->assertFalse(isset($View->Html), 'No helper loaded.');
@@ -131,7 +131,7 @@ public function testRenderSerializeWithOptions()
]
];
$Controller->set($data);
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$result = $View->render();
@@ -168,7 +168,7 @@ public function testRenderSerializeWithString()
]
];
$Controller->set($data);
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$result = $View->render();
@@ -189,7 +189,7 @@ public function testRenderWithoutViewMultiple()
$data = ['no' => 'nope', 'user' => 'fake', 'list' => ['item1', 'item2']];
$Controller->set($data);
$Controller->set('_serialize', ['no', 'user']);
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$this->assertSame('application/xml', $View->response->getType());
$output = $View->render(false);
@@ -199,7 +199,7 @@ public function testRenderWithoutViewMultiple()
$this->assertSame(Xml::build($expected)->asXML(), $output);
$Controller->set('_rootNode', 'custom_name');
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$output = $View->render(false);
$expected = [
@@ -221,7 +221,7 @@ public function testRenderWithoutViewMultipleAndAlias()
$data = ['original_name' => 'my epic name', 'user' => 'fake', 'list' => ['item1', 'item2']];
$Controller->set($data);
$Controller->set('_serialize', ['new_name' => 'original_name', 'user']);
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$this->assertSame('application/xml', $View->response->getType());
$output = $View->render(false);
@@ -231,7 +231,7 @@ public function testRenderWithoutViewMultipleAndAlias()
$this->assertSame(Xml::build($expected)->asXML(), $output);
$Controller->set('_rootNode', 'custom_name');
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$output = $View->render(false);
$expected = [
@@ -252,7 +252,7 @@ public function testRenderWithSerializeTrue()
$Controller = new Controller($Request, $Response);
$data = ['users' => ['user' => ['user1', 'user2']]];
$Controller->set(['users' => $data, '_serialize' => true]);
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$output = $View->render();
@@ -296,7 +296,7 @@ public function testRenderWithView()
]
];
$Controller->set('users', $data);
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$View->setTemplatePath('Posts');
$output = $View->render('index');

0 comments on commit c464f07

Please sign in to comment.
You can’t perform that action at this time.