FIX: Avoid passing empty needle to strpos()#13431
Conversation
vvasiloi
left a comment
There was a problem hiding this comment.
Looks good to me. Thank you, @rimas-kudelis!
|
@lchrusciel ping |
| $country->getCode()->willReturn('PL'); | ||
| $address->getCountryCode()->willReturn('PL'); | ||
| $countryRepository->findOneBy(['code' => 'PL'])->willReturn($country); | ||
|
|
||
| $country->hasProvinces()->willReturn(true); | ||
| $address->getProvinceCode()->willReturn(null); |
There was a problem hiding this comment.
I believe this and the similar mocks in the spec above are not needed? These functions are never called as the isProvinceValid function is not reached. I even removed them and specs were still green 💃
There was a problem hiding this comment.
I think I suggested to add those, but I can't remember why.
Were the tests false positive, @rimas-kudelis?
There was a problem hiding this comment.
@Zales0123 Yes, I think that was it... that's how we tested that we need the fix.
With the fix, the builds are green either way, but without the fix, it only fails with the proper setup (the mocks).
The mocks are required to prevent regression.
There was a problem hiding this comment.
@rimas-kudelis maybe leave a comment there, because in another month who knows if anyone can remember this. 😅
There was a problem hiding this comment.
I actually put that info in the commit message. But sure, I can put it in a comment as well.
There was a problem hiding this comment.
Those extra bits of setup that were added for both tests are for the whole foreach, not just for the condition that Rimas added, which can be reproduced with PHP 7.4.
If you revert everything in changed in this PR, or just start with a 1.10 branch and remove that foreach, then the tests will still pass. Somehow I forgot to mention this in my previous comments.
There was a problem hiding this comment.
Also, why are you not using https://3v4l.org/, @lchrusciel?
There was a problem hiding this comment.
You can see the result for all versions at once there: https://3v4l.org/5ma7S
There was a problem hiding this comment.
@Zales0123 like I said, I agree that asserting that ->getCountryCode() must not be called would make the tests fail. But it feels to me like an assertion on How the validator works instead of What the expected result is. The way I wrote it allows the test subject to remain more of a blackbox, doesn't it?
There was a problem hiding this comment.
Sorry, I missed the Mateusz's latest message.
I second Rimas here and I also said something among those line earlier.
We're testing the validator here and the test is called it_does_not_add_violation, so I don't see why we would assert the behavior on the address object instead of the validator context.
There's a good synergy between: it_does_not_add_violation and $context->addViolation(Argument::any())->shouldNotBeCalled();
It seems current tests would pass even without the logic in place. This makes them more explicit by adding actual conditions which would cause them to fail if the necessary logic wasn't in place. See review comments on #13431.
|
Thank you, Rimas! 🎉 |
The province validator returns early if it finds preexisting validation errors on the address. However, when checking that, it uses
strpos()and passes current$propertyPathas a needle. This might cause problems when an address is validated as a top-level entity, because$propertyPathis an empty string in such case.