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: /**
29: * The base path for output comparisons
30: *
31: * Must be initialized before use
32: *
33: * @var string
34: */
35: protected $_compareBasePath = '';
36:
37: /**
38: * Update comparisons to match test changes
39: *
40: * Initialized with the env variable UPDATE_TEST_COMPARISON_FILES
41: *
42: * @var bool
43: */
44: protected $_updateComparisons;
45:
46: /**
47: * Compare the result to the contents of the file
48: *
49: * @param string $path partial path to test comparison file
50: * @param string $result test result as a string
51: * @return void
52: */
53: public function assertSameAsFile($path, $result)
54: {
55: if (!file_exists($path)) {
56: $path = $this->_compareBasePath . $path;
57: }
58:
59: if ($this->_updateComparisons === null) {
60: $this->_updateComparisons = env('UPDATE_TEST_COMPARISON_FILES');
61: }
62:
63: if ($this->_updateComparisons) {
64: $file = new File($path, true);
65: $file->write($result);
66: }
67:
68: $expected = file_get_contents($path);
69: $this->assertTextEquals($expected, $result);
70: }
71: }
72: