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

feat: Check logs against parts of the message only #6704

Merged
merged 2 commits into from
Oct 20, 2022

Conversation

paulbalandan
Copy link
Member

@paulbalandan paulbalandan commented Oct 17, 2022

Description
Currently, CIUnitTestCase::assertLogged() matches the logged message verbatim. For simple messages this is no problem but for complex, hard-to-build, or volatile messages, it may be hard to come up with the exact message. This PR adds the capability for the assertLogged method to compare only parts of the message instead of the whole.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@paulbalandan paulbalandan added docs needed Pull requests needing documentation write-ups and/or revisions. 4.3 labels Oct 17, 2022
@kenjis kenjis added the enhancement PRs that improve existing functionalities label Oct 17, 2022
@kenjis
Copy link
Member

kenjis commented Oct 17, 2022

The implementation looks good.

@paulbalandan paulbalandan removed the docs needed Pull requests needing documentation write-ups and/or revisions. label Oct 17, 2022
@kenjis
Copy link
Member

kenjis commented Oct 18, 2022

Something wrong with GA checks.

Some checks were not successful
1 failing and 5 successful checks

Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would lean towards creating a new assertLoggedStrict() or the like, but if you two prefer this I'm good with it.

Edit: the opposite, I suppose! assertLogContains()

@paulbalandan
Copy link
Member Author

I was actually contemplating between a new method and modified method. I'll check what I can do.

@kenjis
Copy link
Member

kenjis commented Oct 18, 2022

Generally It is better to avoid boolean flag parameter.
I prefer a new method, because it is easier to read.

$this->assertLogged('error', 'variable did not', false); // What's false? Jump to the method.
$this->assertLogContains('error', 'variable did not');

@paulbalandan paulbalandan changed the title feat: Add $useExactComparison option to assertLogged feat: Check logs against parts of the message only Oct 18, 2022
@@ -349,8 +349,6 @@ protected function mockSession()
* @param string|null $expectedMessage
*
* @return bool
*
* @throws Exception
*/
public function assertLogged(string $level, $expectedMessage = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related to this PR, but why $expectedMessage can be null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️

/**
* Asserts that there is a log record that contains `$logMessage` in the message.
*/
public function assertLogContains(string $level, string $logMessage, string $message = ''): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$expectedMessage is better? assertLogged() uses it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is $expectedMessage is used since the assertion compares the message as expected in the logs.
For assertLogContains, I think it is counter-intuitive to use $expectedMessage when it can only be a part of the whole expected message. I was also thinking whether to use $needle or $needleMessage as alternative name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No preference from me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a little uncomfortable with both $needle and $needleMessage.
needle needs haystack.

So let's leave it as it is.

@kenjis kenjis merged commit ee3e329 into codeigniter4:4.3 Oct 20, 2022
@paulbalandan paulbalandan deleted the assert-logged-approx branch October 20, 2022 05:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.3 enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants