-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Add LoggerAssertionsTrait
which provide shortcuts to assert a log has been written
#51696
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
Conversation
a1d115c
to
efeddc0
Compare
Good idea! |
LoggerAssertionsTrait
which provide shortcuts to assert a log has been written
efeddc0
to
41bc209
Compare
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.
Shouldn't this be part of the Monolog bundle rather than the FrameworkBundle?
It does only work with Monolog
private static function ensureMonologHandlerIsAvailable(): void | ||
{ | ||
if (!self::getContainer()->has('monolog.handler.test')) { | ||
self::fail('The "monolog.handler.test" service is not available. Try registering the service "Monolog\Handler\TestHandler" as "monolog.handler.test" in your test configuration.'); |
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.
What about mentioning using the Monolog bundle configuration if available?
when@test:
monolog:
handlers:
test: { type: test }
main:
type: fingers_crossed
# […]
This would work, because it'll be registered through the Monolog bundle extension in the handlers chain, but I don't get how just registering a monolog.handler.test
would ensure this logger handler is used when using the logger in your application.
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.
You are right, I should belongs to the Monolog bridge, as it only works with Monolog. Or I have to reimplement hasRecord*
methods for generic loggers (which I do not want to do 😄).
I'm going to move it, thanks for your feedback
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.
I don't well know this part, especially how to use this bridge in tests. Would you help me?
|
||
trait LoggerAssertionsTrait | ||
{ | ||
public static function assertLogExists(string $expectedLog, string $level = Logger::DEBUG): void |
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.
The signature is wrong. Monolog's level constants on the Logger class are integers, not strings.
And they are deprecated in favor of the enum in 3.x.
and in 3.x, the various hasRecord*
methods of theTestHandler expect the Level enum.
41bc209
to
34c2d5a
Compare
Like #50662, this PR brings a new collection to enhance assertions to help users to ensure some logs has been written during a Request processing.
In a controller:
In the test:
Todo