|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Nette Tester. |
| 5 | + * Copyright (c) 2009 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Tester; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * Snapshot testing helper. |
| 15 | + */ |
| 16 | +class Snapshot |
| 17 | +{ |
| 18 | + public static $snapshotDir = 'snapshots'; |
| 19 | + |
| 20 | + public static $updatedSnapshots = []; |
| 21 | + |
| 22 | + |
| 23 | + /** |
| 24 | + * Compares value with a previously created snapshot. |
| 25 | + */ |
| 26 | + public static function match($value, string $snapshotName): void |
| 27 | + { |
| 28 | + $updateSnapshots = (bool) getenv(Environment::UPDATE_SNAPSHOTS); |
| 29 | + |
| 30 | + $testFile = $_SERVER['argv'][0]; |
| 31 | + $snapshotFile = self::getSnapshotFile($testFile, $snapshotName); |
| 32 | + |
| 33 | + if (!file_exists($snapshotFile)) { |
| 34 | + if (!$updateSnapshots) { |
| 35 | + Assert::fail("Missing snapshot file '$snapshotFile', use --update-snapshots option to generate it."); |
| 36 | + } |
| 37 | + |
| 38 | + self::write($snapshotFile, $value); |
| 39 | + } |
| 40 | + |
| 41 | + $snapshot = self::read($snapshotFile); |
| 42 | + |
| 43 | + try { |
| 44 | + Assert::equal($snapshot, $value, "Snapshot $snapshotName"); |
| 45 | + |
| 46 | + } catch (AssertException $e) { |
| 47 | + if (!$updateSnapshots) { |
| 48 | + throw $e; |
| 49 | + } |
| 50 | + |
| 51 | + self::write($snapshotFile, $value); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + private static function getSnapshotFile(string $testFile, string $snapshotName): string |
| 57 | + { |
| 58 | + $path = self::$snapshotDir . DIRECTORY_SEPARATOR . pathinfo($testFile, PATHINFO_FILENAME) . '.' . $snapshotName . '.phps'; |
| 59 | + if (!preg_match('#/|\w:#A', self::$snapshotDir)) { |
| 60 | + $path = dirname($testFile) . DIRECTORY_SEPARATOR . $path; |
| 61 | + } |
| 62 | + return $path; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + private static function read(string $snapshotFile) |
| 67 | + { |
| 68 | + $snapshotContents = @file_get_contents($snapshotFile); |
| 69 | + if ($snapshotContents === false) { |
| 70 | + throw new \Exception("Unable to read snapshot file '$snapshotFile'."); |
| 71 | + } |
| 72 | + |
| 73 | + return eval(substr($snapshotContents, strlen('<?php '))); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + private static function write(string $snapshotFile, $value): void |
| 78 | + { |
| 79 | + $snapshotDirectory = dirname($snapshotFile); |
| 80 | + if (!is_dir($snapshotDirectory) && !mkdir($snapshotDirectory)) { |
| 81 | + throw new \Exception("Unable to create snapshot directory '$snapshotDirectory'."); |
| 82 | + } |
| 83 | + |
| 84 | + $snapshotContents = '<?php return ' . var_export($value, true) . ';' . PHP_EOL; |
| 85 | + if (file_put_contents($snapshotFile, $snapshotContents) === false) { |
| 86 | + throw new \Exception("Unable to write snapshot file '$snapshotFile'."); |
| 87 | + } |
| 88 | + |
| 89 | + self::$updatedSnapshots[] = $snapshotFile; |
| 90 | + } |
| 91 | +} |
0 commit comments