Просмотр исходного кода

Fix ZF-12113: Zend_Test error messages should be more verbose. Patch by Martin Hujer.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24851 44c647ce-9c0f-0410-b52a-842ac1e357ba
rob 13 лет назад
Родитель
Сommit
3e31b32fdb

+ 22 - 0
library/Zend/Test/PHPUnit/Constraint/Redirect.php

@@ -64,6 +64,11 @@ class Zend_Test_PHPUnit_Constraint_Redirect extends PHPUnit_Framework_Constraint
      * @var string
      */
     protected $_match             = null;
+    
+    /**
+     * What is actual redirect
+     */
+    protected $_actual            = null;
 
     /**
      * Whether or not assertion is negated
@@ -142,6 +147,10 @@ class Zend_Test_PHPUnit_Constraint_Redirect extends PHPUnit_Framework_Constraint
                     : $this->_regex($response, $match);
             case self::ASSERT_REDIRECT:
             default:
+                $headers  = $response->sendHeaders();
+                $redirect = $headers['location'];
+                $redirect = str_replace('Location: ', '', $redirect);
+                $this->_actual = $redirect;
                 return ($this->_negate) ? !$response->isRedirect() : $response->isRedirect();
         }
     }
@@ -166,6 +175,9 @@ class Zend_Test_PHPUnit_Constraint_Redirect extends PHPUnit_Framework_Constraint
                     $failure = 'Failed asserting response DOES NOT redirect to "%s"';
                 }
                 $failure = sprintf($failure, $this->_match);
+                if (!$this->_negate && $this->_actual) {
+                    $failure .= sprintf(PHP_EOL . 'It redirects to "%s".', $this->_actual);
+                }
                 break;
             case self::ASSERT_REDIRECT_REGEX:
                 $failure = 'Failed asserting response redirects to URL MATCHING "%s"';
@@ -173,12 +185,18 @@ class Zend_Test_PHPUnit_Constraint_Redirect extends PHPUnit_Framework_Constraint
                     $failure = 'Failed asserting response DOES NOT redirect to URL MATCHING "%s"';
                 }
                 $failure = sprintf($failure, $this->_match);
+                if ($this->_actual) {
+                    $failure .= sprintf(PHP_EOL . 'It redirects to "%s".', $this->_actual);
+                }
                 break;
             case self::ASSERT_REDIRECT:
             default:
                 $failure = 'Failed asserting response is a redirect';
                 if ($this->_negate) {
                     $failure = 'Failed asserting response is NOT a redirect';
+                    if ($this->_actual) {
+                        $failure .= sprintf(PHP_EOL . 'It redirects to "%s"', $this->_actual);
+                    }
                 }
                 break;
         }
@@ -216,6 +234,7 @@ class Zend_Test_PHPUnit_Constraint_Redirect extends PHPUnit_Framework_Constraint
         $headers  = $response->sendHeaders();
         $redirect = $headers['location'];
         $redirect = str_replace('Location: ', '', $redirect);
+        $this->_actual = $redirect;
 
         return ($redirect == $match);
     }
@@ -236,6 +255,7 @@ class Zend_Test_PHPUnit_Constraint_Redirect extends PHPUnit_Framework_Constraint
         $headers  = $response->sendHeaders();
         $redirect = $headers['location'];
         $redirect = str_replace('Location: ', '', $redirect);
+        $this->_actual = $redirect;
 
         return ($redirect != $match);
     }
@@ -256,6 +276,7 @@ class Zend_Test_PHPUnit_Constraint_Redirect extends PHPUnit_Framework_Constraint
         $headers  = $response->sendHeaders();
         $redirect = $headers['location'];
         $redirect = str_replace('Location: ', '', $redirect);
+        $this->_actual = $redirect;
 
         return preg_match($pattern, $redirect);
     }
@@ -276,6 +297,7 @@ class Zend_Test_PHPUnit_Constraint_Redirect extends PHPUnit_Framework_Constraint
         $headers  = $response->sendHeaders();
         $redirect = $headers['location'];
         $redirect = str_replace('Location: ', '', $redirect);
+        $this->_actual = $redirect;
 
         return !preg_match($pattern, $redirect);
     }

+ 9 - 0
library/Zend/Test/PHPUnit/Constraint/ResponseHeader.php

@@ -65,6 +65,11 @@ class Zend_Test_PHPUnit_Constraint_ResponseHeader extends PHPUnit_Framework_Cons
      * @var int Response code
      */
     protected $_code              = 200;
+    
+    /**
+     * @var int Actual response code
+     */
+    protected $_actualCode        = null;
 
     /**
      * @var string Header
@@ -197,6 +202,9 @@ class Zend_Test_PHPUnit_Constraint_ResponseHeader extends PHPUnit_Framework_Cons
                     $failure = 'Failed asserting response code IS NOT "%s"';
                 }
                 $failure = sprintf($failure, $this->_code);
+                if (!$this->_negate && $this->_actualCode) {
+                    $failure .= sprintf(PHP_EOL . 'Was "%s"', $this->_actualCode);
+                }
                 break;
             case self::ASSERT_HEADER:
                 $failure = 'Failed asserting response header "%s" found';
@@ -250,6 +258,7 @@ class Zend_Test_PHPUnit_Constraint_ResponseHeader extends PHPUnit_Framework_Cons
     protected function _code(Zend_Controller_Response_Abstract $response, $code)
     {
         $test = $this->_getCode($response);
+        $this->_actualCode = $test;
         return ($test == $code);
     }