browser: Undefined array key 1 when the test failed
Hi,
I am migrating a large code base and try to use zenstruck/browser to replace the symfony client.
I am having some trouble as I am testing my login process. It’s a json api, I have no problem with my admin authentication but when I am trying to login a normal user, the test fails. I am surely not asking you to fix my test for me (but if you can without the code, you’re very good 😉 )
Here is my test, I reduced the code to the minimal required here (the only thing which is not visible is that I am using a dataProvider to get $username
and $password
):
$response = $this->browser()
->post('/api/login_check', [
'body' => [
'username' => $username,
'password' => $password,
],
])
->use(function (\Zenstruck\Browser\Response $response) {
self::assertSame(Response::HTTP_OK, $response->statusCode());
})
->response();
So the test fails at assertSame
as the $response->statusCode()
is 401. The problem remains the same if I change the ->use
callback to a classic assertSame
after the $response
assignation.
The problem is that my output looks like this:
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
Testing Functional\Tests\App\Infrastructure\Symfony\Controller\LoginCheckControllerTest
Undefined array key 1
THE ERROR HANDLER HAS CHANGED!
Remaining self deprecation notices (10)
Remaining indirect deprecation notices (29)
I have no message, no file created in the var/browser/
directory so I know my test fails because I added some dd
above the assertSame
but I think this is not the expected behavior 😉
Also var/log/test.log
has no information.
As you might imagine, it’s a legacy code migration… The architecture is a bit complex but still very clean and from what I can say the legacy tests don’t interfere with this new ones (as I use different test suites and only runs the suite I am interested in, plus the test above only executes one test). I might have missed something and hopefully someone could point it out? Or at least give me some advice as I don’t know where to look at for now.
If the test fails outside of the browser context (my fixtures are not loaded properly for example) I get a regular phpunit error stack trace.
Thank you for your help, Please ask if I could add more information I didn’t think of.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (12 by maintainers)
We work at the same place, actually and we exchanged about that. 😉 I should be able to test the PR and the ideas I left here in the next two weeks while updating the project I am working on.
That PR is now merged so hopefully now that we have tests, we can solve this once and for all. We should add some of @0x346e3730’s data provider names as they are mighty complex!
@flohw I will try to create a reproducible test; however, it might not be very soon 😞 Thank you for the quick reply.
This is because the name normalization is required to create the error or failure log file with the response to be able to see what’s happening.
The generated error looks like a what PHPUnit generates when a test fails so the regex matches partially. I’m not sure it’s strictly related to the data provider but they interfere with the error generated by
assertSeeIn
.I am not sure how we could solve this. I hope @kbond or @nikophil will have a quick answer for you.
Can you provide a reproducible test case? It may help.
@flohw We are using PHP 8.1. Somehow the regex is not working properly:
gives us
Undefined array key 1
instead it should give us:If the name is not normalized at all, it gives the correct message. We are using data providers for this particular scenario, would that be an issue maybe?
Thanks for the detailed explaination @flohw!
I encountered on a case when I got:
when running a test case with
dataProvider
with failure. Dug a bit deeper and found that:is not capture via regex of method:
BUT if I remove that orphan parenthesis:
everything is fine, at least in regex101 but not in preg.
\Zenstruck\Browser\Test\BrowserExtension::normalizeTestName
. If I debug a regex:I guess it’s added for nested arguments list but it’s failing. How do we proceed with that?
PS:
\preg_match('/^([\w:\\\\\]+)(.+"([\w ]+)".+?$/', $name, $matches);
(after exported from regex101) works but doesn’t capture the data.