Skip to content

Commit

Permalink
Correct deprecation warnings for viewBuilder properties.
Browse files Browse the repository at this point in the history
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 c464f07
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
2 changes: 0 additions & 2 deletions src/Controller/Controller.php
Expand Up @@ -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');
Expand Down
18 changes: 10 additions & 8 deletions src/View/ViewVarsTrait.php
Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
});
}

Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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());
}

Expand Down Expand Up @@ -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'));
Expand Down
1 change: 0 additions & 1 deletion tests/TestCase/View/CellTest.php
Expand Up @@ -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());
}

/**
Expand Down
9 changes: 6 additions & 3 deletions tests/TestCase/View/ViewVarsTraitTest.php
Expand Up @@ -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);
});
}

/**
Expand Down
24 changes: 12 additions & 12 deletions tests/TestCase/View/XmlViewTest.php
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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.');
Expand Down Expand Up @@ -131,7 +131,7 @@ public function testRenderSerializeWithOptions()
]
];
$Controller->set($data);
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$result = $View->render();

Expand Down Expand Up @@ -168,7 +168,7 @@ public function testRenderSerializeWithString()
]
];
$Controller->set($data);
$Controller->viewClass = 'Xml';
$Controller->viewBuilder()->setClassName('Xml');
$View = $Controller->createView();
$result = $View->render();

Expand All @@ -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);
Expand All @@ -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 = [
Expand All @@ -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);
Expand All @@ -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 = [
Expand All @@ -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();

Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit c464f07

Please sign in to comment.