Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c3eff6a

Browse files
committedFeb 10, 2025·
Adding ability to save UsageChart images
These changes add the ability to, by specifying a env variable, to instruct the UsageChart image export tests to save the actual images to the `/tmp/` directory so that they can be compared to another set of images to determine why these tests may be failing. These changes will also output a metadata file to be used in further determining potential similiarities in test failures.
1 parent f4901bf commit c3eff6a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
 

‎tests/regression/lib/Controllers/UsageChartsTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class UsageChartsTest extends \PHPUnit\Framework\TestCase
2626
// Used when running in hash-generation mode.
2727
protected static $imagehashes = array();
2828

29+
protected static $fileNameToTestNames = array();
30+
2931
/**
3032
* Determine which JSON file to use for expected hash data.
3133
*/
@@ -84,6 +86,14 @@ public static function tearDownAfterClass(): void
8486
print json_encode(self::$imagehashes, JSON_PRETTY_PRINT);
8587
}
8688
}
89+
90+
// If we're saving images then make sure to save the metadata
91+
if (!empty(self::$fileNameToTestNames)) {
92+
echo "Saving Regression Usage Chart Metadata!\n";
93+
$fileName = implode(DIRECTORY_SEPARATOR, array('/tmp', 'reg_usage_chart_metadata.json'));
94+
file_put_contents($fileName, json_encode(self::$fileNameToTestNames, JSON_PRETTY_PRINT));
95+
echo "Saved to $fileName";
96+
}
8797
}
8898

8999
private function phash($type, $imageData)
@@ -134,6 +144,23 @@ public function testChartSettings($testName, $input, $expectedHash)
134144
$imageData = $response[0];
135145
$actualHash = $this->phash($input['format'], $imageData);
136146

147+
if (getenv('REG_TEST_SAVE_IMAGE')) {
148+
$fileHash = md5(var_export($input, true));
149+
$fileExt = 'png';
150+
if (strpos($imageData, 'svg') !== false) {
151+
$fileExt = 'svg';
152+
}
153+
$filePath = implode(DIRECTORY_SEPARATOR, array('/tmp', "$fileHash.$fileExt"));
154+
self::$fileNameToTestNames[] = array(
155+
'test_name' => $testName,
156+
'file_hash' => $fileHash,
157+
'expected_hash' => $expectedHash,
158+
'actual_hash' => $actualHash,
159+
'input' => $input
160+
);
161+
file_put_contents($filePath, $imageData);
162+
}
163+
137164
if ($expectedHash === false || getenv('REG_TEST_FORCE_GENERATION') === '1') {
138165
self::$imagehashes[$testName] = $actualHash;
139166
$this->markTestSkipped('Created Expected output for ' . $testName);

0 commit comments

Comments
 (0)
Please sign in to comment.