Skip to content

Commit

Permalink
Merge pull request #8169 from cakephp/ad-allow-updating-test-file-com…
Browse files Browse the repository at this point in the history
…parisons

assertSameAsFile - Make it easy to update test expectations
  • Loading branch information
markstory committed Feb 4, 2016
2 parents dc2d623 + 7e788bd commit 7cb7c1a
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/TestSuite/StringCompareTrait.php
Expand Up @@ -14,6 +14,8 @@
*/
namespace Cake\TestSuite;

use Cake\Filesystem\File;

/**
* Compare a string to the contents of a file
*
Expand All @@ -32,6 +34,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
*
Expand All @@ -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);
Expand Down

0 comments on commit 7cb7c1a

Please sign in to comment.