-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Assert array partials #1371
Comments
In esssence, you mean Doesn't |
That was my first assumption but... nope. Assert contains will look for an array item that corresponds exactly to the needle itself, and I believe this should be the expected behavior :D The new assertion should detect if the needle represents a [key=>value] fragment from the subject array. |
@whatthejeff @sebastianbergmann do you think it's worth a PR? I did a quick experiment on a local branch and the assertion is apparently ready to rumble :) |
Ah, almost guessed so. My only note would be to name it |
@sun it's not the same behavior as |
I rejected a similar PR (#361) during a triage some time ago. There was a time when the majority of the PRs we received were just random new assertions of dubious usefulness. We probably merged more of them than we should've. The result is that we have a number of assertions that are rarely (if ever) used. It's sort of a slippery slope, I guess. To combat this, I started rejecting the majority of these PRs unless they were heavily requested. Let me run this by @sebastianbergmann and see what he thinks. Just for reference, what is your current use case for this? |
FWIW, I also tinkered about that problem space, since I ran into the same question as a maintainer of Drupal's homegrown testing framework. Over time, I concluded the following as the most pragmatic and most sensible approach:
In short, it's primarily about naming, less about purpose or use-cases. It doesn't hurt anyone if there are well-named + well-designed assertion methods, for which you didn't have a use-case (yet). A good testing framework works intuitively:
(…seriously hope this example works; didn't run it through PHP before posting 😛) As @marcioAlmada pointed out, it's perfectly OK if the assertion method supports a more intelligent/smart behavior on the given data that the corresponding PHP core function. Only if it's not possible to design it in way that doesn't break regular expectations, additional method suffixes should be used (in this case, e.g., assertArrayIntersectAssocRecursive()). |
I disagree with this sentiment for just about any software, honestly. |
@whatthejeff recently I found a use case right inside a PHPUnit test case while working on #1358. I only needed to test the existence of a given With this new assertion, tests would look much more simpler and objective: $this->assertArrayPart(
array('message_regex' => '#regex#'),
PHPUnit_Util_Test::getExpectedException('ExceptionTest', 'testWithRegexMessage')
);
$this->assertArrayPart(
array('message_regex' => '#regex#'),
PHPUnit_Util_Test::getExpectedException('ExceptionTest', 'testWithRegexMessageFromClassConstant')
);
$this->assertArrayPart(
array('message_regex' => 'ExceptionTest::UNKNOWN_MESSAGE_REGEX_CONSTANT'),
PHPUnit_Util_Test::getExpectedException('ExceptionTest', 'testWithUnknowRegexMessageFromClassConstant')
); It could be much easier to split these kind of tests - https://github.com/sebastianbergmann/phpunit/blob/master/tests/Util/TestTest.php#L69-L151 - into smaller assertions, since each part of those configuration arrays are pertinent to a specific feature and therefore should be tested separately, not like a matrix of configurations. PS: I'm using |
@sun As said before, $actual === array_replace_recursive($actual, $expected); |
Thanks for the use case, @marcioAlmada. I'll run this by @sebastianbergmann and see what he thinks. Might also be nice to have assertions like $this->assertArrayKeyEquals(
'#regex#', 'message_regex',
PHPUnit_Util_Test::getExpectedException('ExceptionTest', 'testWithRegexMessage')
); Then we can take advantage of the comparator subframework which lets us support deltas, canonicalization, etc. |
@whatthejeff |
@marcioAlmada Yeah, I know. I was suggesting that it might be nice to have both :) |
Alright, I pinged @sebastianbergmann. Let's see what he thinks. |
@whatthejeff sorry, I misread your post :) thank you! |
I've just made a PR just in case I'm not able to respond promptly during the next 3 days ;) |
Nice! I haven't heard back from @sebastianbergmann yet. I'll be sure to follow up once I do. |
#1371 (comment) makes sense to me. Go ahead :-) |
Thanks for the PR, @marcioAlmada. I've left some initial feedback. I'm not sure where I stand on the naming discussions. On one hand, I agree with @sun that mapping it to core functions makes life easier. On the other hand, I would prefer typing @sebastianbergmann do you have any feedback on the name? |
Hi, thanks @sebastianbergmann and @sun for all the feedback Despite I like @sun rhetoric (always map assertions to core functions),
@whatthejeff I like |
Discussed this with @sebastianbergmann. Let's go with |
ok, PR seems to be all set now :) |
Shouldn't this be |
@sebastianbergmann said he liked |
Alright, let's do |
No problem. Search and replace to the rescue :) |
Thanks, @marcioAlmada. I'll look into the hhvm-nightly segfault. |
Alright, the hhvm-nightly crash is unrelated to your changes. Seems they have a regression in |
Alright, the segfault should be fixed in master. Would you mind rebasing so that we can make sure everything passes? |
You mean to |
@marcioAlmada Well, however your prefer to bring your branch up to date works for me :) |
\o/ Yay, all is green! I think we're good to go :) |
Yeah! Ok, done. Build went fine, thanks to you. No segfaults :) Do you know why hhvm-nightly buid is so slow? It took 3.79 minutes oO |
I think it's probably related to facebook/hhvm@842d6fb, but I haven't confirmed it just yet. |
Implementation for assertArraySubset #1371
@marcioAlmada Also, HHVM is generally going to be slower in this type of situation thanks to the JIT. We might want to disable it actually. |
Yes, you're right. But hhvm-nightly builds got much slower lately. Thanks for pointing facebook/hhvm@842d6fb out. Cheers! |
Yeah, there's definitely an issue. I'll keep looking into it. |
Looks like this new assertion is not documented yet: sebastianbergmann/phpunit-documentation#217 Can you provide a patch, @marcioAlmada? |
@sebastianbergmann, If I remember right, @whatthejeff already took care of the docs :) |
sebastianbergmann/phpunit-documentation#217 is still open and I don't see documentation. |
Oh, that's true. I thought docs were already in place. I'll send a patch this weekend ;) |
Hi 😄
I've got into some situations involving partial assertion of arrays that could be easily solved if we had the following implementation:
It could also assert recursions:
I searched through all the docs and found nothing equivalent.
The text was updated successfully, but these errors were encountered: