Skip to content
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

[8.x] Array to string conversion error while checking response status code in unit tests #38464

Closed
galvesband opened this issue Aug 19, 2021 · 2 comments

Comments

@galvesband
Copy link

  • Laravel Version: >8.51.0
  • PHP Version: 7.4.20
  • Database Driver & Version: Not database related

Description:

Sorry if this is not the right place for my issue. I'll try to be brief.

In this pull, exception tracking during unit testing is introduced. Looking at FoundationServiceProvider code you can see the exception tracking code checks the context of every logged event looking for an 'exception' key. The code assumes the content of that 'exception' key to be an exception (or at least something that can be turned into a string) and adds it to a LoggedExceptionCollection instance bound for the test.

This change brakes many of my unit tests, the reason being I tend to include exceptions in the context of log messages already transformed into arrays in an 'exception' key, so they end up nicely serialized in log files. The listener code that checks log events for exceptions in the FoundationServiceProvider just looks into the 'exception' key in the context and runs with its content, which is a bit too broad in my opinion. Am I the only one using 'exception' in log contexts already transforming its content into arrays? It seemed a somewhat reasonable compromise...

I understand I can work around this issue rewriting my code into exceptions that handle their own log representation and sending those exceptions in the logs, which is probably the long term better approach. But I don't think this behaviour is intended and the code that reports exceptions from log messages should not assume so much from the log context's content.

I'm reporting this because I think it is not intended. I'm a big fan of the project, by the way.

Steps To Reproduce:

    1. Anywhere in your code reachable by an http request. For example in api.php:
    Route::get('exception', function () {
      Log::info("Hi there", ['exception' => ['this can not be converted to a string']]);
      return response()->json(['The deed is done!']);
    });
    1. An integration test:
    class ExceptionTest extends TestCase
    {
        /** @test */
        public function check_response_status()
        {
            $response = $this->json('GET', '/api/exception');
            $response->assertStatus(200); // Throws an "Array to string conversion" error
        }
    }
@X-Coder264
Copy link
Contributor

X-Coder264 commented Aug 19, 2021

Seldaek/monolog#1535 (comment)

It's a general convention that the exception key has an exception object so that when the exception gets sent (for example) to Sentry via the Sentry Monolog handler you get the entire stack trace etc.

@taylorotwell
Copy link
Member

Yeah - no plans to change anything here. See comment above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants