From d60bf8571cd3d11cce35a22ef7b3d886597971b8 Mon Sep 17 00:00:00 2001 From: Lucas CORBEAUX Date: Mon, 14 Oct 2013 13:46:15 +0200 Subject: [PATCH 1/3] Throw an exception in PhpRenderer when the resolved file path is not readable or is a directory. --- .../Exception/UnexpectedValueException.php | 19 ++++++++++++++++ library/Zend/View/Renderer/PhpRenderer.php | 7 ++++++ tests/ZendTest/View/PhpRendererTest.php | 22 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 library/Zend/View/Exception/UnexpectedValueException.php diff --git a/library/Zend/View/Exception/UnexpectedValueException.php b/library/Zend/View/Exception/UnexpectedValueException.php new file mode 100644 index 00000000000..3968d8d8732 --- /dev/null +++ b/library/Zend/View/Exception/UnexpectedValueException.php @@ -0,0 +1,19 @@ +__template )); } + if (!is_readable($this->__file) || is_dir($this->__file)) { + throw new Exception\UnexpectedValueException(sprintf( + '%s: Unable to open template "%s"; File does not exists, is not readable or is a directory', + __METHOD__, + $this->__file + )); + } try { ob_start(); include $this->__file; diff --git a/tests/ZendTest/View/PhpRendererTest.php b/tests/ZendTest/View/PhpRendererTest.php index e7a163474ee..520bae120f8 100644 --- a/tests/ZendTest/View/PhpRendererTest.php +++ b/tests/ZendTest/View/PhpRendererTest.php @@ -352,6 +352,28 @@ public function testRendererRaisesExceptionIfResolverCannotResolveTemplate() $test = $this->renderer->render('should-not-find-this'); } + public function testRendererRaisesExceptionIfResolvedTemplateDoesNotExists() + { + $this->setExpectedException('Zend\View\Exception\UnexpectedValueException', 'File does not exists'); + $resolver = new TemplateMapResolver(array( + 'not-exists' => '/does/not/exists', + )); + + $this->renderer->setResolver($resolver); + $this->renderer->render('not-exists'); + } + + public function testRendererRaisesExceptionIfResolvedTemplateIsDirectory() + { + $this->setExpectedException('Zend\View\Exception\UnexpectedValueException', 'File does not exists'); + $resolver = new TemplateMapResolver(array( + 'is-directory' => '.', + )); + + $this->renderer->setResolver($resolver); + $this->renderer->render('is-directory'); + } + /** * @group view-model */ From d55a594ab2da4656662597077cb2642672c647ae Mon Sep 17 00:00:00 2001 From: Lucas CORBEAUX Date: Fri, 18 Oct 2013 09:28:34 +0200 Subject: [PATCH 2/3] Testing for include failure and empty buffer instead of file existence. Slightly changed exception message and hide warnings in tests. --- library/Zend/View/Renderer/PhpRenderer.php | 16 ++++++++-------- tests/ZendTest/View/PhpRendererTest.php | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/library/Zend/View/Renderer/PhpRenderer.php b/library/Zend/View/Renderer/PhpRenderer.php index 8272e924509..a380cd6f7b7 100644 --- a/library/Zend/View/Renderer/PhpRenderer.php +++ b/library/Zend/View/Renderer/PhpRenderer.php @@ -502,21 +502,21 @@ public function render($nameOrModel, $values = null) $this->__template )); } - if (!is_readable($this->__file) || is_dir($this->__file)) { - throw new Exception\UnexpectedValueException(sprintf( - '%s: Unable to open template "%s"; File does not exists, is not readable or is a directory', - __METHOD__, - $this->__file - )); - } try { ob_start(); - include $this->__file; + $includeReturn = include $this->__file; $this->__content = ob_get_clean(); } catch (\Exception $ex) { ob_end_clean(); throw $ex; } + if ($includeReturn === false && empty($this->__content)) { + throw new Exception\UnexpectedValueException(sprintf( + '%s: Unable to render template "%s"; file include failed', + __METHOD__, + $this->__file + )); + } } $this->setVars(array_pop($this->__varsCache)); diff --git a/tests/ZendTest/View/PhpRendererTest.php b/tests/ZendTest/View/PhpRendererTest.php index 520bae120f8..a760d66c41e 100644 --- a/tests/ZendTest/View/PhpRendererTest.php +++ b/tests/ZendTest/View/PhpRendererTest.php @@ -354,24 +354,24 @@ public function testRendererRaisesExceptionIfResolverCannotResolveTemplate() public function testRendererRaisesExceptionIfResolvedTemplateDoesNotExists() { - $this->setExpectedException('Zend\View\Exception\UnexpectedValueException', 'File does not exists'); + $this->setExpectedException('Zend\View\Exception\UnexpectedValueException', 'file include failed'); $resolver = new TemplateMapResolver(array( 'not-exists' => '/does/not/exists', )); $this->renderer->setResolver($resolver); - $this->renderer->render('not-exists'); + @$this->renderer->render('not-exists'); } public function testRendererRaisesExceptionIfResolvedTemplateIsDirectory() { - $this->setExpectedException('Zend\View\Exception\UnexpectedValueException', 'File does not exists'); + $this->setExpectedException('Zend\View\Exception\UnexpectedValueException', 'file include failed'); $resolver = new TemplateMapResolver(array( 'is-directory' => '.', )); $this->renderer->setResolver($resolver); - $this->renderer->render('is-directory'); + @$this->renderer->render('is-directory'); } /** From 6475df7dad7b824f75abbf508a23984c0aaa5751 Mon Sep 17 00:00:00 2001 From: Lucas CORBEAUX Date: Thu, 24 Oct 2013 21:44:52 +0200 Subject: [PATCH 3/3] Fix a code standard issue in the UnexpectedValueException, change the testing strategy to avoid the use of the @ operator. --- .../Exception/UnexpectedValueException.php | 3 +- tests/ZendTest/View/PhpRendererTest.php | 35 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/library/Zend/View/Exception/UnexpectedValueException.php b/library/Zend/View/Exception/UnexpectedValueException.php index 3968d8d8732..9b9ec9080dc 100644 --- a/library/Zend/View/Exception/UnexpectedValueException.php +++ b/library/Zend/View/Exception/UnexpectedValueException.php @@ -12,8 +12,7 @@ /** * Unexpected value exception */ -class UnexpectedValueException - extends \UnexpectedValueException +class UnexpectedValueException extends \UnexpectedValueException implements ExceptionInterface { } diff --git a/tests/ZendTest/View/PhpRendererTest.php b/tests/ZendTest/View/PhpRendererTest.php index a760d66c41e..78612dd0587 100644 --- a/tests/ZendTest/View/PhpRendererTest.php +++ b/tests/ZendTest/View/PhpRendererTest.php @@ -352,26 +352,37 @@ public function testRendererRaisesExceptionIfResolverCannotResolveTemplate() $test = $this->renderer->render('should-not-find-this'); } - public function testRendererRaisesExceptionIfResolvedTemplateDoesNotExists() + public function invalidTemplateFiles() { - $this->setExpectedException('Zend\View\Exception\UnexpectedValueException', 'file include failed'); - $resolver = new TemplateMapResolver(array( - 'not-exists' => '/does/not/exists', - )); - - $this->renderer->setResolver($resolver); - @$this->renderer->render('not-exists'); + return array( + array('/does/not/exists'), + array('.') + ); } - public function testRendererRaisesExceptionIfResolvedTemplateIsDirectory() + /** + * @dataProvider invalidTemplateFiles + */ + public function testRendererRaisesExceptionIfResolvedTemplateIsInvalid($template) { - $this->setExpectedException('Zend\View\Exception\UnexpectedValueException', 'file include failed'); $resolver = new TemplateMapResolver(array( - 'is-directory' => '.', + 'invalid' => $template, )); + set_error_handler(function ($errno, $errstr) { return true; }, E_WARNING); + $this->renderer->setResolver($resolver); - @$this->renderer->render('is-directory'); + + try { + $this->renderer->render('invalid'); + $caught = false; + } catch(\Exception $e) { + $caught = $e; + } + + restore_error_handler(); + $this->assertInstanceOf('Zend\View\Exception\UnexpectedValueException', $caught); + $this->assertContains('file include failed', $caught->getMessage()); } /**