-
-
Notifications
You must be signed in to change notification settings - Fork 360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add toBeOneOf() expectation #1290
base: 3.x
Are you sure you want to change the base?
Conversation
8319e3d
to
d87bd72
Compare
d87bd72
to
ae40c0f
Compare
public function toBeOneOf(Closure ...$tests): self | ||
{ | ||
if ($tests === []) { | ||
return $this; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion this should throw
} | ||
|
||
if (count($matches) > 1) { | ||
throw new ExpectationFailedException('Failed asserting value matches exactly one expectation (matches: '.implode(', ', $matches).').'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than outputting the keys, I'd just output a count of passes and fails for a few reasons:
- Keys are limited to ints, or variable names due to using the splat operator rather than accepting an iterable, for example:
->toBeOneOf(thisIsTheOnlyWayToNameAnExpectation: fn($e) => ..., spaces_are_not_allowed: fn($e) => ...)
- If you were accepting an iterable rather than using a splat, array keys aren't guaranteed to be unique
- There could be a ton of expectations passed to this function, causing a failure to output a huge error
- Just counting passes and failures reduces the memory overhead
You can also output the total count of assertions ran by checking the delta of Assert::getCount()
before and after.
|
||
use PHPUnit\Framework\ExpectationFailedException; | ||
|
||
expect(true)->toBeTrue()->and(false)->toBeFalse(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expect(true)->toBeTrue()->and(false)->toBeFalse(); |
What:
Description:
Adds a
toBeOneOf
expectation that allows you to ensure that a value matches only one of a set of assertions. This is complimentary to #1286.