From f9e7b235198739fd0f0afcfef487ddaab019029c Mon Sep 17 00:00:00 2001 From: Tang Rufus Date: Thu, 26 Oct 2017 22:40:16 +0800 Subject: [PATCH] Add `ViewAwareTrait::toHtml()` --- src/ViewAwareTrait.php | 10 ++++++++++ src/ViewAwareTraitInterface.php | 7 +++++++ src/ViewInterface.php | 4 ++-- tests/_data/ViewAwareTraitDummy.php | 14 ++++++++++++++ tests/wpunit/ViewAwareTraitTest.php | 17 +++++++++++++++++ 5 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/ViewAwareTrait.php b/src/ViewAwareTrait.php index b694209..4e155ef 100644 --- a/src/ViewAwareTrait.php +++ b/src/ViewAwareTrait.php @@ -40,6 +40,16 @@ public function render() $this->getView()->render($this); } + /** + * Convert the view to HTML with self as context object. + * + * @return string + */ + public function toHtml(): string + { + return $this->getView()->toHtml($this); + } + /** * Returns a closure which render the view with self as the context. * diff --git a/src/ViewAwareTraitInterface.php b/src/ViewAwareTraitInterface.php index 791817a..ddbca0e 100644 --- a/src/ViewAwareTraitInterface.php +++ b/src/ViewAwareTraitInterface.php @@ -29,6 +29,13 @@ interface ViewAwareTraitInterface */ public function render(); + /** + * Convert the view to HTML with self as context object. + * + * @return string + */ + public function toHtml(): string; + /** * Returns a closure which render the view with self as the context. * diff --git a/src/ViewInterface.php b/src/ViewInterface.php index 8f6aa57..0b26cec 100644 --- a/src/ViewInterface.php +++ b/src/ViewInterface.php @@ -34,7 +34,7 @@ public function render($context = null); * * @param mixed $context Optional. Context object for which to render the view. * - * @return void + * @return string */ - public function toHtml($context = null); + public function toHtml($context = null): string; } diff --git a/tests/_data/ViewAwareTraitDummy.php b/tests/_data/ViewAwareTraitDummy.php index 3662570..9e79771 100644 --- a/tests/_data/ViewAwareTraitDummy.php +++ b/tests/_data/ViewAwareTraitDummy.php @@ -8,6 +8,20 @@ class ViewAwareTraitDummy implements ViewAwareTraitInterface { use ViewAwareTrait; + /** + * Dummy data. + * + * @var string + */ + public $name = 'Daenerys Targaryen'; + + /** + * Dummy data. + * + * @var int + */ + public $dragons = 3; + public function forceSetView($view) { $this->view = $view; diff --git a/tests/wpunit/ViewAwareTraitTest.php b/tests/wpunit/ViewAwareTraitTest.php index a6a0036..b5bcd61 100644 --- a/tests/wpunit/ViewAwareTraitTest.php +++ b/tests/wpunit/ViewAwareTraitTest.php @@ -105,4 +105,21 @@ public function it_renders_the_view_with_self_as_the_context() $view->verifyInvokedOnce('render', [$this->subject]); } + + /** @test */ + public function it_converts_the_view_to_html_with_self_as_the_context() + { + $view = new View( + codecept_data_dir('dummy-template-with-context.php'), + wp_kses_allowed_html('post') + ); + $this->subject->setView($view); + + $actual = $this->subject->toHtml(); + + $this->assertSame( + $view->toHtml($this->subject), + $actual + ); + } }