lucatume/codeception-snapshot-assertions

Snapshot assertions sugar for Codeception.

0.2.4 2020-02-05 20:17 UTC

This package is auto-updated.

Last update: 2020-05-05 20:52:28 UTC


README

Leverage Codeception snapshot support to make snapshot testing in Codeception projects easier.

Code example

<?php
class WidgetTest extends Codeception\TestCase\Test
{
    use tad\Codeception\SnapshotAssertions\SnapshotAssertions;
    public function testDefaultContent(){
        $widget = new Widget() ;

        $this->assertMatchesHtmlSnapshot($widget->html());
    }
    
    public function testOutput(){
       $widget = new Widget(['title' => 'Test Widget', 'content' => 'Some test content']) ;

       $this->assertMatchesHtmlSnapshot($widget->html());
    }
}

Requirements

The package is based on the snapshot support added in Codeception since version 2.5.0, as such the library requirments are:

  • PHP 5.6+
  • Codeception 2.5+

Installation

Install the package using Composer:

composer install lucatume/codeception-snapshot-assertions

Codeception is a requirement for the package to work and will be installed as a dependency if not specified in the project Composer configuration file (composer.json).

What is snapshot testing?

Snapshot testing is a convenient way to test code by testing its output.
Snapshot testing is faster than full-blown visual end-to-end testing (and not a replacement for it) but less cumbersome to write than lower lever unit testing (and, again, not a replacement for it).
This kind of testing lends itself to be used in unit and integration testing to automate the testing of output.
Read more about snapshot testing here:

How is this different from what Codeception does?

  • snapshots do not require writing a class dedicated to it, you can just use the tad\Codeception\SnapshotAssertions\SnapshotAssertions trait in your test case and one of the assertMatches... methods it provides.
  • it supports string and HTML snapshot testing too.
  • the snapshots generated by the code live in a folder of the same folder, the __snapshots__ one, that generated them.

How is this different from what the Spatie package does?

  • it leverages Codeception own snapshot implementation, hence it will not work on vanilla PhpUnit
  • it lowers the library requirement from PHP 7.0 to PHP 5.6.

Usage

The package supports the following type of assertions:

  1. string snapshot assertions, to compare a string to a string snapshot with the assertMatchesStringSnapshot method.
  2. HTML snapshot assertions, to compare an HTML fragment to an HTML fragment snapshot with the assertMatchesHtmlSnapshot method.
  3. JSON snapshot assertions, to compare a JSON string to a stored JSON snapshot with the assertMatchesJsonSnapshot method.
  4. Code snapshot assertions, to compare code to a stored code snapshot with the assertMatchesCodeSnapshot method.

The first time an assert... method is called the library will generate a snapshot file in the same directory as the tests, in the __snapshots__ folder.
As an example if the following test case lives in the tests/Output/WidgetTest.php file then when the testDefaultContent method runs the library will generate the tests/Output/WidgetTest__testDefaultContent__0.snapshot.html file; you can regenerate failing snapshots by running Codeception tests in debug mode (using the --debug flag of the run command).

String assertions

This kind of assertion is useful when the output of a method is a plain string.
The snapshot produced by this kind of assertion will have the .snapshot.txt file extension.
The method used to make string snapshot assertions is tad\Codeception\SnapshotAssertions\SnapshotAssertions::assertStringSnapshot().
Usage example;

<?php
class ErrorMessageTest extends Codeception\TestCase\Test
{
    use tad\Codeception\SnapshotAssertions\SnapshotAssertions;
    
    public function testClassAndMethodOutput(){
        $errorMessage = new ErrorMessage(__CLASS__, 'foo') ;

        $this->assertMatchesStringSnapshot($errorMessage->message());
    }
    
    public function testClassOnlyOutput(){
        $errorMessage = new ErrorMessage(__CLASS__) ;

        $this->assertMatchesStringSnapshot($errorMessage->message());
    }
}

HTML assertions

This kind of assertion is useful when the output of a method is an HTML document or HTML fragment.
The snapshot produced by this kind of assertion will have the .snapshot.html file extension.
The method used to make HTML snapshot assertions is tad\Codeception\SnapshotAssertions\SnapshotAssertions::assertHtmlSnapshot().
Usage example;

<?php
class WidgetTest extends Codeception\TestCase\Test
{
    use tad\Codeception\SnapshotAssertions\SnapshotAssertions;
    
    public function testDefaultContent(){
        $widget = new Widget() ;

        $this->assertMatchesHtmlSnapshot($widget->html());
    }
    
    public function testOutput(){
       $widget = new Widget(['title' => 'Test Widget', 'content' => 'Some test content']) ;

       $this->assertMatchesHtmlSnapshot($widget->html());
    }
}

JSON assertions

This kind of assertion is useful when the output of a method is a JSON string.
The snapshot produced by this kind of assertion will have the .snapshot.html file extension.
The method used to make JSON snapshot assertions is tad\Codeception\SnapshotAssertions\SnapshotAssertions::assertJsonSnapshot().
Usage example:

<?php
class ApiTest extends Codeception\TestCase\Test
{
    use tad\Codeception\SnapshotAssertions\SnapshotAssertions;
    
    public function testGoodResponse(){
        $this->givenTheUserIsAuthenticated();
        $request = new Request([
            'me' => [
                'name'   
            ]
        ]);
        
        $api = new API() ;

        $this->assertMatchesJsonSnapshot($api->handle($request));
    }

    public function testMissingAuthResponse(){
        $request = new Request([
            'me' => [
                'name'   
            ]
        ]);
        
        $api = new API() ;

        $this->assertMatchesJsonSnapshot($api->handle($request));
    }
}

Code assertions

This kind of assertion is useful when the output of a method is code.
The snapshot produced by this kind of assertion will have the .snapshot.php file extension by default, but you can specify an extension to use for the snapshot.
The method used to make code snapshot assertions is tad\Codeception\SnapshotAssertions\SnapshotAssertions::assertCodeSnapshot().
Usage example;

<?php
class ApiTest extends Codeception\TestCase\Test
{
    use tad\Codeception\SnapshotAssertions\SnapshotAssertions;
    
    public function testGoodCode(){
		$generator = new CodeGenerator();
		$code = $generator->produce('phpCode');
        $this->assertMatchesCodeSnapshot($code);
    }

    public function testMissingAuthResponse(){
		$generator = new CodeGenerator();
		$code = $generator->produce('jsCode');
        $this->assertMatchesCodeSnapshot($code);
    }
}

Directory assertions

This kind of assertion is useful to ensure directory structure and contents do not change overtime, when the output of a code block is a directory and files in it.
This assertion will check that the current directory, and the one captured in the snapshot, have the same files, and that each file has the same contents.
The snapshot produced by this kind of assertion will have the .snapshot file extension; they are plain text files.
The method used to make code snapshot assertions is tad\Codeception\SnapshotAssertions\SnapshotAssertions::assertDirectorySnapshot().
Usage example;

<?php
class DirectorySetupTest extends Codeception\TestCase\Test
{
    use tad\Codeception\SnapshotAssertions\SnapshotAssertions;
    
    public function testGoodDirectorySetUp(){
		$generator = new DirectorySetup();
        $destination = codecept_output_dir('test');

		$generator->setUpDirectory('test', $destination );

        $this->assertMatchesDirectorySnapshot($destination);
    }

    public function testFailingDirectorySetUp(){
		$generator = new DirectorySetup();
        $destination = codecept_output_dir('failing');

		$generator->setUpDirectory('failing', $destination );

        $this->assertMatchesDirectorySnapshot($destination);
    }
}

Visitor functions

To allow more fine-grained control over how the assertion on the data should be made, each Snapshot implementation supports "data visitors.".

A data visitor is a callable that will receive, from the snapshot implementation, the expected data and the current data.
Depending on the snapshot type the arguments received by the callback might differ or be more than two.

Examples

In the following example the data visitor is used to exclude some files from a directory snapshot and to drop some hashed lines from some files:

<?php

public function test_files(){
        $dataVisitor = static function ($expected, $current, $pathName) {
            if (strpos($pathName, 'fileOne')) {
                // Empty file one, like dropping it.
                return [[], []];
            }

            if (strpos($pathName, 'fileTwo')) { 
                // Remove the hash line in file two.
                $removeHashLine = static function ($line) {
                    return !preg_match('/\\/\\/\\s*\\[HASH].*$/uim', $line);
                };
                return [
                    array_filter($expected, $removeHashLine),
                    array_filter($current, $removeHashLine)
                ];
            }

            return [$expected, $current];
        };
    
        $dirToTest = codecept_output('dir-to-test');
        $snapshot =  new DirectorySnapshot($dirToTest);
        $snapshot->setDataVisitor($dataVisitor);
        $snapshot->assert();
}

In this example the data visitor is used to remove some hash data from a JSON object:

<?php

public function test_json_object(){
        $removeHashEntry = static function ($jsonString) {
            // Remove the `hash` key from the JSON object.
            return json_encode(array_diff_key(json_decode($jsonString, true), array_flip(['hash'])));
        };
        $dataVisitor = static function ($expected, $current) use ($removeHashEntry) {
            return array_map($removeHashEntry, [$expected, $current]);
        };

        // This first snapshot will create the first HTML snapshot.
        $firstSnapshot = new JsonSnapshot(MyJsonProducingObject::data());
        $firstSnapshot->setDataVisitor($dataVisitor);
        $firstSnapshot->assert();
}