pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.49k stars 343 forks source link

[2.x] Implementation to the `at` method #1004

Closed Tiskiel closed 8 months ago

Tiskiel commented 10 months ago

What:

Description:

Implementation of the at method. This will allow you to target an index or key on an iterable and apply other expectation methods to the value. It provides better readability and works on nested arrays.

Example below:

it('ensures it work with nested array', function () {
    $nestedArray = [
        [1, 2, 3],
        ['foo' => 'bar', 'john' => 'doe'],
    ];

    expect($nestedArray)
        ->at(0)->at(1)->toBe(2)
        ->and($nestedArray)
        ->at(1)->foo->toBe('bar');
});
Tiskiel commented 9 months ago

Hi there,

I have implemented the dot notation as spoked above.

Here is an example :

it('ensures it work with dot notation', function () {
    $nestedDictionary = [
        'foo' => [
            'bar' => [
                'john' => 'doe',
            ],
        ],
    ];

    expect($nestedDictionary)
        ->at('foo.bar.john')->toBe('doe');
});
nunomaduro commented 8 months ago

No plans for this.