Last active
February 14, 2017 10:43
-
-
Save Cydonia7/516a9e2efb532e2033a56513de46c6ee to your computer and use it in GitHub Desktop.
Snapshot testing in Symfony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests; | |
use Symfony\Component\VarDumper\Test\VarDumperTestTrait; | |
trait SnapshotTrait | |
{ | |
use VarDumperTestTrait; | |
private function assertSnapshot($name, $content) | |
{ | |
$this->snapshotIfNeeded($name, $content); | |
$this->compareSnapshot($name, $content); | |
} | |
private function snapshotIfNeeded($name, $content) | |
{ | |
$snapshotFile = $this->getSnapshotFile($name); | |
if (!file_exists($snapshotFile) || getenv('UPDATE_SNAPSHOTS') === '1') { | |
file_put_contents($snapshotFile, $this->getDump($content)); | |
} | |
} | |
private function compareSnapshot($name, $content) | |
{ | |
$this->assertDumpEquals(file_get_contents($this->getSnapshotFile($name)), $content); | |
} | |
private function getSnapshotFile($name) | |
{ | |
return __DIR__.'/snapshots/'.$name.'.txt'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment