Skip to content

Instantly share code, notes, and snippets.

@Cydonia7
Last active February 14, 2017 10:43
Show Gist options
  • Save Cydonia7/516a9e2efb532e2033a56513de46c6ee to your computer and use it in GitHub Desktop.
Save Cydonia7/516a9e2efb532e2033a56513de46c6ee to your computer and use it in GitHub Desktop.
Snapshot testing in Symfony
<?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