Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Closes #514: Better readable diff for assertStringMatches* calls

  • Loading branch information...
commit 963ece1073d4133ade3a33c5b132cf1939f55f52 1 parent 107f36c
Volker Dusch authored April 14, 2012
1  ChangeLog.markdown
Source Rendered
@@ -13,6 +13,7 @@ PHPUnit 3.7.0
13 13
 * Implemented #333: Improved reporting when there are unused CLI arguments to avoid misconceptions.
14 14
 * Implemented #377: Show messages and stracktraces in JSON output for skipped and incomplete tests.
15 15
 * Implemented #504: Expanded the `@requires` annotation to allow for checking the existence of functions and extensions using multiple `@requires function name` statements.
  16
+* Implemented #514: Failed `assertStringMatchesFormat()` calls now produce a better readable diff by only marking lines as different that don't match the format specifiers.
16 17
 * The `@requires` annotation can now be used on the class DocBlock. Required versions can be overridden in the methods annotation, required functions and extensions will be merged.
17 18
 * Implemented #86 #508: `@expectedExceptionCode` and `@expectedExceptionMessage` can now use constants like `Classname::CONST` as their parameters. They will get evaluated if the class constant exists and used for comparison so test authors can avoid duplication.
18 19
 * Implemented #512: Test listeners now trigger one autoload call instead of being silently ignored when the class was not loaded.
49  PHPUnit/Framework/Constraint/StringMatches.php
@@ -69,8 +69,37 @@ class PHPUnit_Framework_Constraint_StringMatches extends PHPUnit_Framework_Const
69 69
      */
70 70
     public function __construct($string)
71 71
     {
72  
-        $this->pattern = preg_quote(preg_replace('/\r\n/', "\n", $string), '/');
73  
-        $this->pattern = str_replace(
  72
+        $this->pattern = $this->createPatternFromFormat(
  73
+            preg_replace('/\r\n/', "\n", $string)
  74
+        );
  75
+        $this->string  = $string;
  76
+    }
  77
+
  78
+    protected function failureDescription($other)
  79
+    {
  80
+        return "format description matches text";
  81
+    }
  82
+
  83
+    protected function additionalFailureDescription($other)
  84
+    {
  85
+        $from = preg_split('(\r\n|\r|\n)', $this->string);
  86
+        $to = preg_split('(\r\n|\r|\n)', $other);
  87
+        foreach($from as $index => $line) {
  88
+            if(isset($to[$index]) && $line !== $to[$index]) {
  89
+                $line = $this->createPatternFromFormat($line);
  90
+                if(preg_match($line, $to[$index]) > 0) {
  91
+                    $from[$index] = $to[$index];
  92
+                }
  93
+            }
  94
+        }
  95
+        $this->string = join("\n", $from);
  96
+        $other = join("\n", $to);
  97
+        return PHPUnit_Util_Diff::diff($this->string, $other);
  98
+    }
  99
+
  100
+    protected function createPatternFromFormat($string)
  101
+    {
  102
+        $string = str_replace(
74 103
           array(
75 104
             '%e',
76 105
             '%s',
@@ -97,21 +126,9 @@ public function __construct($string)
97 126
             '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?',
98 127
             '.'
99 128
           ),
100  
-          $this->pattern
  129
+          preg_quote($string, '/')
101 130
         );
102  
-
103  
-        $this->pattern = '/^' . $this->pattern . '$/s';
104  
-        $this->string  = $string;
105  
-    }
106  
-
107  
-    protected function failureDescription($other)
108  
-    {
109  
-        return "format description matches text";
110  
-    }
111  
-
112  
-    protected function additionalFailureDescription($other)
113  
-    {
114  
-        return PHPUnit_Util_Diff::diff($this->string, $other);
  131
+        return '/^' . $string . '$/s';
115 132
     }
116 133
 
117 134
 }
17  Tests/Framework/AssertTest.php
@@ -3762,16 +3762,15 @@ public function testAssertStringEndsNotWith()
3762 3762
     public function testAssertStringMatchesFormat()
3763 3763
     {
3764 3764
         $this->assertStringMatchesFormat('*%s*', '***');
  3765
+    }
3765 3766
 
3766  
-        try {
3767  
-            $this->assertStringMatchesFormat('*%s*', '**');
3768  
-        }
3769  
-
3770  
-        catch (PHPUnit_Framework_AssertionFailedError $e) {
3771  
-            return;
3772  
-        }
3773  
-
3774  
-        $this->fail();
  3767
+    /**
  3768
+     * @covers PHPUnit_Framework_Assert::assertStringMatchesFormat
  3769
+     * @expectedException PHPUnit_Framework_AssertionFailedError
  3770
+     */
  3771
+    public function testAssertStringMatchesFormatFailure()
  3772
+    {
  3773
+        $this->assertStringMatchesFormat('*%s*', '**');
3775 3774
     }
3776 3775
 
3777 3776
     /**

0 notes on commit 963ece1

Please sign in to comment.
Something went wrong with that request. Please try again.