|
|
@@ -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);
|