spatie/phpunit-snapshot-assertions

[Feature] get{TYPE}Snapshot($testName)

Closed this issue · 3 comments

As a developer I would like to be able to load up a snap shot file to do reverse testing (conversions).

example:

public function it_can_decode_xml()
{
    $this->assertMatchesSnapshot($this->xml->decode($this->getXmlSnapshot('it_can_encode_xml'));
}

I don't think it is a good idea to use the output of one snapshot test as the input for another. A better idea would be to create a tests/Files directory and create an xml file in there, then use that file for your decoding test.

But if you want to use snapshot outputs as inputs, you can already get the snapshot directory by using $this->getSnapshotDirectory(), you could do the following:

public function it_can_decode_xml()
{
    $snapshotFilePath = $this->getSnapshotDirectory().'/it_can_decode_xml.php';

    $decodedXml = $this->xml->decode(
        file_get_contents($snapshotFilePath);
    );

    $this->assertMatchesSnapshot($decodedXml);
}

I did something like this yesterday (updated with the $this->getSnapshotDirectory()):

    protected function getXMLSnapshot(string $testName)
    {
        return file_get_contents($this->getSnapshotDirectory() . DIRECTORY_SEPARATOR . get_class($this) . '__' . $testName .'__1.xml');
    }

I think most people don't need these reverse testing functions. As we like to keep our packages small, we're not going to add them. You can add the methods you need in your own project.