Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Adds an every call that can be chained in expectations for traversable #7

Merged
merged 1 commit into from
May 13, 2021

Conversation

lukeraymonddowning
Copy link
Member

Often, I find myself wanting to assert that everything in an array or Laravel Collection passes a given test.

For example, I might want to make sure that everything in a Collection is an instance of a certain class, or that no item in an array is greater than a certain value.

This PR adds the every method, which allows the user to perform assertions on individual items in iterable values.

Usage

Imagine we want to make sure that every item in an array is less than 100. This PR allows for this with the every method:

test('it works', function () {
    expect(range(1, 99))
        ->every->toBeLessThan(100);
});

It works with not and and methods:

test('it works', function () {
    expect(range(1, 99))
        ->every->not->toBeString()
        ->and(['hello', 'world'])
        ->every->not->toBeInt();
});

If this is something that you see as valuable, let me know if there are any changes you'd like to see and I shall sort it 👍

Thanks for the insanely beautiful API that is the expectations plugin!

@nunomaduro
Copy link
Member

Just docs missing. Can you pull request them? I will re-work some types/phpdocs later.

@nunomaduro
Copy link
Member

I feel that this should be:

test('it works', function () {
    expect(range(1, 99))
        ->each->toBeLessThan(100);

    expect(range(1, 99))
        ->each(fn ($number) => $number->toBeLessThan(100));
});

@nunomaduro nunomaduro merged commit b1fabbf into pestphp:master May 13, 2021
@nunomaduro
Copy link
Member

Going to update the pull request locally.

@nunomaduro
Copy link
Member

@lukeraymonddowning Ok - I've reworked the pull request here: 75474c1.

Can you take a look why this test is failing? 75474c1#diff-fd27a2cd204cb5863fe4e29ba58b987d235512ed4448e5eadba046a81aa10eceR39.

@lukeraymonddowning
Copy link
Member Author

@nunomaduro Fixed in #8

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants