Git Product home page Git Product logo

assert-golden's Introduction

assertGolden Assertion

GitHub Release GitHub Actions Workflow Status Packagist Downloads

Same as assertEquals, but when null is given as argument, the test file is automatically edited and null is substituted with the actual value

Given the following code:

assertGolden(
    null,                 // <- expectation value
    ['color' => 'golden'] // <- actual value
);

...during the first execution null replaced with the actual value:

assertGolden(
    [
        'color' => 'golden',
    ],
    ['color' => 'golden']
);

In principle, it's about saving oneself the recurring work of writing, updating and copying an expectation.

Installation

You can install the package via composer:

composer require holgerk/assert-golden --dev

Usage

Just pass null to the assertGolden expectation and null will be automatically replaced during the first test run.

Trait Usage

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\Test;

use Holgerk\AssertGolden\AssertGolden;

class ExampleTest extends TestCase
{
    use AssertGolden;

    #[Test]
    public function test(): void
    {
        // via method call...
        $this->assertGolden(
            null,
            ['a' => 1, 'b' => 2]
        );
        
        // ...or static call
        self::assertGolden(
            null,
            ['a' => 1, 'b' => 2]
        );
    }
}

Function Usage

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\Test;

use function Holgerk\AssertGolden\assertGolden;

class ExampleTest extends TestCase
{
    #[Test]
    public function test(): void
    {
        assertGolden(
            null,
            ['a' => 1, 'b' => 2]
        );
    }
}

Later you can edit the expectation by hand or insert null again to have it automatically replaced.

Regenerate all expectations

If you want to regenerate all expectations at once you can add the argument: --update-golden to your phpunit invocation.

# regenerate all expectations at once from their actual values
./vendor/bin/phpunit --update-golden

Limitation

It is not possible to have more than one assertGolden call on one line. Because the automatic replacement is based on the debug_backtrace function, which gives us the line number and file of the assertGolden caller, and the composer package nikic/php-parser, which is used to get the exact start and end position of the expectation argument. So if there are more than one assertGolden call it is not possible to detect a distinct position.

See Also

Credits

License

The MIT License (MIT). Please see License File for more information.

assert-golden's People

Contributors

holgerk avatar

Watchers

 avatar

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.