Explorar el Código

ZF-10405: Fix exceptions in Zend_Uri::__toString(): Applying patch

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23375 44c647ce-9c0f-0410-b52a-842ac1e357ba
bittarman hace 15 años
padre
commit
782bc13647
Se han modificado 2 ficheros con 48 adiciones y 1 borrados
  1. 6 1
      library/Zend/Uri.php
  2. 42 0
      tests/Zend/UriTest.php

+ 6 - 1
library/Zend/Uri.php

@@ -53,7 +53,12 @@ abstract class Zend_Uri
      */
      */
     public function __toString()
     public function __toString()
     {
     {
-        return $this->getUri();
+        try {
+            return $this->getUri();
+        } catch (Exception $e) {
+            trigger_error($e->getMessage(), E_USER_WARNING);
+            return '';
+        }
     }
     }
 
 
     /**
     /**

+ 42 - 0
tests/Zend/UriTest.php

@@ -134,6 +134,39 @@ class Zend_UriTest extends PHPUnit_Framework_TestCase
     }
     }
 
 
     /**
     /**
+     * Tests that if an exception is thrown when calling the __toString()
+     * method an empty string is returned and a Warning is triggered, instead 
+     * of a Fatal Error being triggered.
+     *
+     * @group ZF-10405
+     */
+    public function testToStringRaisesWarningWhenExceptionCaught()
+    {
+        $uri = Zend_Uri::factory('http://example.com', 'Zend_Uri_ExceptionCausing');
+        
+        set_error_handler(array($this, 'handleErrors'), E_USER_WARNING);
+
+        $text = sprintf('%s', $uri);
+
+        restore_error_handler();
+
+        $this->assertTrue(empty($text));
+        $this->assertTrue(isset($this->error));
+        $this->assertContains('Exception in getUri()', $this->error);
+
+    }
+
+    /**
+     * Error handler for testExceptionThrownInToString()
+     * 
+     * @group ZF-10405
+     */
+    public function handleErrors($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array())
+    {
+        $this->error = $errstr;
+    }
+
+    /**
      * Tests that an invalid $uri throws an exception and that the
      * Tests that an invalid $uri throws an exception and that the
      * message of that exception matches $regex.
      * message of that exception matches $regex.
      *
      *
@@ -189,6 +222,15 @@ class Zend_Uri_Mock extends Zend_Uri
     public function getUri() { }
     public function getUri() { }
     public function valid() { }
     public function valid() { }
 }
 }
+class Zend_Uri_ExceptionCausing extends Zend_Uri
+{
+    protected function __construct($scheme, $schemeSpecific = '') { }
+    public function valid() { }
+    public function getUri()
+    {
+        throw new Exception('Exception in getUri()');
+    }
+}
 class Fake_Zend_Uri
 class Fake_Zend_Uri
 {
 {
 }
 }