GithubHelp home page GithubHelp logo

thenlabs / snapshots-comparator Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 35 KB

Comparison of state arrays with expectations.

License: MIT License

PHP 100.00%
php states snapshot-testing snapshots testing testing-tools

snapshots-comparator's Introduction

SnapshotsComparator

Comparison of state arrays with expectations.

If you like this project gift us a ⭐.


Installation.

composer require thenlabs/snapshots-comparator dev-main

Usage example.

We have the next two status vars:

$before = [
    'document1' => [
        'id' => '1',
        'title' => 'The document 1',
        'customData1' => 'custom data 1',
    ]
];

$after = [
    'document1' => [
        'id' => '1',
        'title' => 'The document one',
        'anotherData' => 'another data',
    ]
];

When we compare both(without declaring expectations) we obtain the next results:

use ThenLabs\SnapshotsComparator\Comparator;

$result = Comparator::compare($before, $after);

false === $result->isSuccessful(); // becouse there are unexpectations.

$result->getUnexpectations() === [
    'CREATED' => [
        'document1' => [
            // 'anotherData' was created.
            'anotherData' => 'another data',
        ]
    ],
    'UPDATED' => [
        'document1' => [
            // 'title' was updated
            'title' => 'The document one',
        ]
    ],
    'DELETED' => [
        'document1' => [
            // 'customData1' was deleted
            'customData1' => 'custom data 1',
        ]
    ],
];

We can see that we obtain all the information about the difference between before and after, as example, which data they was created, updated or deleted.

All these changes they was unexpected becouse we not declaring expectations, and that is the reason for which $result->isSuccessful() returns false.

In the next example we declare all the expectations and that way accomplish a successful result without unexpectations.

use ThenLabs\SnapshotsComparator\Comparator;
use ThenLabs\SnapshotsComparator\ExpectationBuilder;

$expectations = new ExpectationBuilder();

$expectations->expectCreated([
    'document1' => [
        'anotherData' => 'another data',
    ]
]);

$expectations->expectUpdated([
    'document1' => [
        'title' => function ($value) { // custom comparator
            return is_string($value);
        },
    ]
]);

$expectations->expectDeleted([
    'document1' => [
        'customData1' => 'custom data 1',
    ]
]);

$result = Comparator::compare($before, $after, $expectations);

true === $result->isSuccessful();
[] === $result->getUnexpectations();

snapshots-comparator's People

Contributors

andaniel05 avatar

Watchers

 avatar

Forkers

andaniel05

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.