1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\TestSuite;
16:
17: use Cake\Filesystem\File;
18:
19: /**
20: * Compare a string to the contents of a file
21: *
22: * Implementing objects are expected to modify the `$_compareBasePath` property
23: * before use.
24: */
25: trait StringCompareTrait
26: {
27: /**
28: * The base path for output comparisons
29: *
30: * Must be initialized before use
31: *
32: * @var string
33: */
34: protected $_compareBasePath = '';
35:
36: /**
37: * Update comparisons to match test changes
38: *
39: * Initialized with the env variable UPDATE_TEST_COMPARISON_FILES
40: *
41: * @var bool
42: */
43: protected $_updateComparisons;
44:
45: /**
46: * Compare the result to the contents of the file
47: *
48: * @param string $path partial path to test comparison file
49: * @param string $result test result as a string
50: * @return void
51: */
52: public function assertSameAsFile($path, $result)
53: {
54: if (!file_exists($path)) {
55: $path = $this->_compareBasePath . $path;
56: }
57:
58: if ($this->_updateComparisons === null) {
59: $this->_updateComparisons = env('UPDATE_TEST_COMPARISON_FILES');
60: }
61:
62: if ($this->_updateComparisons) {
63: $file = new File($path, true);
64: $file->write($result);
65: }
66:
67: $expected = file_get_contents($path);
68: $this->assertTextEquals($expected, $result);
69: }
70: }
71: