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

Adds HigherOrderExpectations #12

Merged
merged 8 commits into from
Jun 10, 2021

Conversation

nedwors
Copy link
Contributor

@nedwors nedwors commented Jun 7, 2021

Hi!

Thanks for the amazing package 🚀

This is a PR to allow properties to be dynamically accessed from the expectation value. For example, given a User, the following is possible:

expect($user)
    ->name->toEqual('Dr Pest')
    ->email->not->toBeNull->toEqual('dr@pest.com')
    ->posts->toHaveCount(2)->each(fn ($post) => $post->published->toBeTrue)
    ->posts->sequence(
        fn ($post) => $post->title->toEqual('Simply the Pest'),
        fn ($post) => $post->title->toEqual('Pest Friends')
    )

This will work with an array or an object.

I appreciate this PR is quite heavy on additions - eg. changing how the __get method functions in Expectation - and I may have missed something along the way. So, it might not even be desired. But I thought it was a cool feature to submit and see what everyone thinks. Happy to hear all thoughts and suggestions.

expect($me)->is_waiting_patiently->toBeTrue

Thanks!

Update:

Support for methods has been added. So now the following would be possible:

expect($user)->getName()->toEqual('Nuno')

And even:

expect($calculator)->add(1, 4)->toEqual(5)


/**
* Creates a new higher order expectation.
*/
public function __construct(Expectation $original, string $property)
public function __construct(Expectation $original, string $name, bool $asMethod = false, ...$arguments)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the $asMethod and $arguments be passed in a separate method?

return (new HigherOrderExpectation($this, $method))->asMethod(...$parameters);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought exactly this but went with parameters to allow the expectation set-up to happen in the constructor?

I'm not sure of a clean way to defer the setup of the expectation in the class without having everywhere that calls $this->expectation to instead call $this->expectation() and have that method as:

private function expectation()
{
   return $this->expectation ??= // Set up the expectation here
}

Copy link
Contributor Author

@nedwors nedwors Jun 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukeraymonddowning - I've added static constructors as one way at solving this:

return HigherOrderExpectation::forProperty($this, $name)

return HigherOrderExpectation::forMethod($this, $name, ...$parameters)

I'm not sure if static constructors are in fitting with this project's code style but I thought it was at least worth presenting it.

Copy link
Member

@lukeraymonddowning lukeraymonddowning Jun 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we instead assume asMethod if the constructor received more than 2 arguments? Perhaps remove the varadic and have it as an array.

Just thinking that when you look at the static constructors they're actually remarkably similar and properties will never have arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukeraymonddowning chef's kiss. Great call, thank you 🔥

@nunomaduro nunomaduro merged commit ace4d44 into pestphp:master Jun 10, 2021
@nedwors
Copy link
Contributor Author

nedwors commented Jun 10, 2021

Wow, amazing! @nunomaduro, should I start on something for the docs, or do you have more things brewing? 😀

@lukeraymonddowning
Copy link
Member

@nedwors please crack on and start work on the docs 👌

@nedwors nedwors deleted the higher-order-expectations branch June 10, 2021 17:13
@nedwors
Copy link
Contributor Author

nedwors commented Jun 10, 2021

I'll get on it tonight 👍

@nunomaduro
Copy link
Member

@nedwors Thank you!

@nedwors
Copy link
Contributor Author

nedwors commented Jun 10, 2021

@nunomaduro, @lukeraymonddowning PR opened - pestphp/docs#57. Any and all suggestions welcome!

@nedwors nedwors mentioned this pull request Jun 10, 2021
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.

3 participants