Browse Source

Merge pull request #8169 from cakephp/ad-allow-updating-test-file-comparisons

assertSameAsFile - Make it easy to update test expectations
Mark Story 10 years ago
parent
commit
7cb7c1a3d3
1 changed files with 23 additions and 1 deletions
  1. 23 1
      src/TestSuite/StringCompareTrait.php

+ 23 - 1
src/TestSuite/StringCompareTrait.php

@@ -14,6 +14,8 @@
  */
 namespace Cake\TestSuite;
 
+use Cake\Filesystem\File;
+
 /**
  * Compare a string to the contents of a file
  *
@@ -33,6 +35,15 @@ trait StringCompareTrait
     protected $_compareBasePath = '';
 
     /**
+     * Update comparisons to match test changes
+     *
+     * Initialized with the env variable UPDATE_TEST_COMPARISON_FILES
+     *
+     * @var bool
+     */
+    protected $_updateComparisons = null;
+
+    /**
      * Compare the result to the contents of the file
      *
      * @param string $path partial path to test comparison file
@@ -41,7 +52,18 @@ trait StringCompareTrait
      */
     public function assertSameAsFile($path, $result)
     {
-        $path = $this->_compareBasePath . $path;
+        if (!file_exists($path)) {
+            $path = $this->_compareBasePath . $path;
+        }
+
+        if ($this->_updateComparisons === null) {
+            $this->_updateComparisons = env('UPDATE_TEST_COMPARISON_FILES');
+        }
+
+        if ($this->_updateComparisons) {
+            $file = new File($path, true);
+            $file->write($result);
+        }
 
         $expected = file_get_contents($path);
         $this->assertTextEquals($expected, $result);