From 77df8b46e3f41865549fffae02cfdabb81521cc6 Mon Sep 17 00:00:00 2001 From: Yves P Date: Thu, 4 Feb 2016 22:43:20 +0100 Subject: [PATCH] It is now possible to set the template a cell uses using the ViewBuilder --- src/View/Cell.php | 2 +- tests/TestCase/View/CellTest.php | 46 ++++++++++++++++++- .../TestApp/View/Cell/ArticlesCell.php | 21 ++++++--- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/View/Cell.php b/src/View/Cell.php index 6d83c39eaa3..6279099845d 100644 --- a/src/View/Cell.php +++ b/src/View/Cell.php @@ -194,7 +194,7 @@ public function render($template = null) $template = Inflector::underscore($template); } if ($template === null) { - $template = $this->template; + $template = $this->viewBuilder()->template() ?: $this->template; } $builder = $this->viewBuilder(); diff --git a/tests/TestCase/View/CellTest.php b/tests/TestCase/View/CellTest.php index 2f773016227..6efd21f554c 100644 --- a/tests/TestCase/View/CellTest.php +++ b/tests/TestCase/View/CellTest.php @@ -133,7 +133,7 @@ public function testDefaultCellAction() } /** - * Tests that cell action setting the template renders the correct template + * Tests that cell action setting the template using the property renders the correct template * * @return void */ @@ -143,11 +143,31 @@ public function testSettingCellTemplateFromAction() $this->assertContains('This is the alternate template', "{$appCell}"); $this->assertEquals('alternate_teaser_list', $appCell->template); + $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template()); $appCell = $this->View->cell('Articles::customTemplate'); $this->assertContains('This is the alternate template', $appCell->render()); $this->assertEquals('alternate_teaser_list', $appCell->template); + $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template()); + } + + /** + * Tests that cell action setting the template using the ViewBuilder renders the correct template + * + * @return void + */ + public function testSettingCellTemplateFromActionViewBuilder() + { + $appCell = $this->View->cell('Articles::customTemplateViewBuilder'); + + $this->assertContains('This is the alternate template', "{$appCell}"); + $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template()); + + $appCell = $this->View->cell('Articles::customTemplateViewBuilder'); + + $this->assertContains('This is the alternate template', $appCell->render()); + $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template()); } /** @@ -403,4 +423,28 @@ public function testCachedRenderSimpleCustomTemplate() Cache::drop('default'); } + + /** + * Test cached render. + * + * @return void + */ + public function testCachedRenderSimpleCustomTemplateViewBuilder() + { + $mock = $this->getMock('Cake\Cache\CacheEngine'); + $mock->method('init') + ->will($this->returnValue(true)); + $mock->method('read') + ->will($this->returnValue(false)); + $mock->expects($this->once()) + ->method('write') + ->with('cell_test_app_view_cell_articles_cell_customTemplateViewBuilder', "

This is the alternate template

\n"); + Cache::config('default', $mock); + + $cell = $this->View->cell('Articles::customTemplateViewBuilder', [], ['cache' => true]); + $result = $cell->render(); + $this->assertContains('This is the alternate template', $result); + + Cache::drop('default'); + } } diff --git a/tests/test_app/TestApp/View/Cell/ArticlesCell.php b/tests/test_app/TestApp/View/Cell/ArticlesCell.php index 7fcdd0802c0..f5c0cc97821 100644 --- a/tests/test_app/TestApp/View/Cell/ArticlesCell.php +++ b/tests/test_app/TestApp/View/Cell/ArticlesCell.php @@ -52,19 +52,26 @@ public function teaserList() } /** - * Renders articles in teaser view mode. + * Renders a view using a different template than the action name + * The template is set using the ``Cell::$template`` property * * @return void */ public function customTemplate() { $this->template = 'alternate_teaser_list'; - $this->set('articles', [ - ['title' => 'Lorem ipsum', 'body' => 'dolorem sit amet'], - ['title' => 'Usectetur adipiscing eli', 'body' => 'tortor, in tincidunt sem dictum vel'], - ['title' => 'Topis semper blandit eu non', 'body' => 'alvinar diam convallis non. Nullam pu'], - ['title' => 'Suspendisse gravida neque', 'body' => 'pellentesque sed scelerisque libero'], - ]); + } + + /** + * Renders a view using a different template than the action name + * The template is set using the ViewBuilder bound to the Cell + * + * @return void + */ + public function customTemplateViewBuilder() + { + $this->template = 'derp'; + $this->viewBuilder()->template('alternate_teaser_list'); } /**