PHPackages                             holgerk/assert-golden - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. holgerk/assert-golden

ActiveLibrary[Testing &amp; Quality](/categories/testing)

holgerk/assert-golden
=====================

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

v2.1.2(1y ago)38.5k↓45.2%1MITPHPPHP ^8.1CI passing

Since Oct 24Pushed 1y ago1 watchersCompare

[ Source](https://github.com/holgerk/assert-golden)[ Packagist](https://packagist.org/packages/holgerk/assert-golden)[ RSS](/packages/holgerk-assert-golden/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (16)Used By (1)

assertGolden Assertion
======================

[](#assertgolden-assertion)

[![GitHub Release](https://camo.githubusercontent.com/01e0a60b0f8ddf49efc157664770a1a363b3e2f0cf6138df4611de6792b1cd6c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f686f6c6765726b2f6173736572742d676f6c64656e)](https://camo.githubusercontent.com/01e0a60b0f8ddf49efc157664770a1a363b3e2f0cf6138df4611de6792b1cd6c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f686f6c6765726b2f6173736572742d676f6c64656e)[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/8c19b679fc0144e965d20ba303476bc46e1d0289303ca627b0d38829751603c7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f686f6c6765726b2f6173736572742d676f6c64656e2f74657374732e796d6c)](https://camo.githubusercontent.com/8c19b679fc0144e965d20ba303476bc46e1d0289303ca627b0d38829751603c7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f686f6c6765726b2f6173736572742d676f6c64656e2f74657374732e796d6c)[![Packagist Downloads](https://camo.githubusercontent.com/fad6dcf9eab7b7d8d20c6e09739d7ccdfc85973eaa0628e7824d9b7d12478b63/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686f6c6765726b2f6173736572742d676f6c64656e)](https://camo.githubusercontent.com/fad6dcf9eab7b7d8d20c6e09739d7ccdfc85973eaa0628e7824d9b7d12478b63/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686f6c6765726b2f6173736572742d676f6c64656e)

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,                 //  'golden'] //  'golden',
    ],
    ['color' => 'golden']
);
```

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

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require holgerk/assert-golden --dev
```

Usage
-----

[](#usage)

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

### Trait Usage

[](#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

[](#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

[](#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
----------

[](#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 is more than one assertGolden call, it is not possible to detect a distinct position.

assertGoldenFile Assertion
==========================

[](#assertgoldenfile-assertion)

`assertGoldenFile` may be useful if you have a large expected value and don't like it to be directly within your test.

Given the following code:

```
use PHPUnit\Framework\TestCase;
use function Holgerk\AssertGolden\assertGoldenFile;

class ExampleTest extends TestCase
{
    /** @test */
    public function some_feature(): void
    {
        assertGoldenFile(actual: ['color' => 'green']);
    }
}
```

...during the first execution, the following file is generated:

```
