magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Fix Test Logger monolog compatibility

Open convenient opened this issue 2 years ago • 21 comments

Not sure if this is the preferred approach, but one of our automated suites which builds magento with our module caught this today.

System info

  • Magento 2.4.4
  • PHP 8.1

To reproduce

  1. Get a setup of 2.4.4 installed
  2. See that in the constraints allow for an install of monolog/monolog at 2.7.0
  3. Try to run integration tests (https://devdocs.magento.com/guides/v2.4/test/integration/integration_test_execution.html)

The error

Currently we get this error because the method signatures are different

PHP Fatal error:  Declaration of Magento\TestFramework\ErrorLog\Logger::addRecord(int $level, string $message, array $context = []): bool must be compatible with Monolog\Logger::addRecord(int $level, string $message, array $context = [], ?Monolog\DateTimeImmutable $datetime = null): bool in dev/tests/integration/framework/Magento/TestFramework/ErrorLog/Logger.php on line 69

Root cause

This version of monolog/monolog was tagged about an hour ago https://github.com/Seldaek/monolog/releases/tag/2.7.0

See this entry

Added $datetime parameter to Logger::addRecord as low level API to allow logging into the past or future (https://github.com/Seldaek/monolog/pull/1682)

Which goes along with this commit https://github.com/Seldaek/monolog/commit/0ddba7342ff919990e1914d24db15e565f153676#diff-1e7fd545cec457de96f5ed6bd7249ba091cd9e699b4057db15ff1e2e0364025bR297

This changes the method signature of addRecord like so

-    public function addRecord(int|Level $level, string $message, array $context = []): bool
+    public function addRecord(int|Level $level, string $message, array $context = [], DateTimeImmutable $datetime = null): bool 

See the magento core logger is defined like this https://github.com/magento/magento2/blob/a8543fed035d85c667195e95f69476d51f98381b/dev/tests/integration/framework/Magento/TestFramework/ErrorLog/Logger.php#L69-L73

The method signature needs to be updated to match monolog/monolog definition

Contribution checklist (*)

  • [ ] Pull request has a meaningful description of its purpose
  • [ ] All commits are accompanied by meaningful commit messages
  • [ ] All new or changed code is covered with unit/integration tests (if applicable)
  • [ ] README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • [ ] All automated tests passed successfully (all builds are green)

convenient avatar Jun 09 '22 10:06 convenient

Hi @convenient. Thank you for your contribution Here are some useful tips how you can test your changes using Magento test environment. Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

:exclamation: Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s) For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here

:information_source: Run only required test builds during development. Run all test builds before sending your pull request for review.

For more details, review the Magento Contributor Guide documentation.

:warning: According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

m2-assistant[bot] avatar Jun 09 '22 10:06 m2-assistant[bot]

@magento run all tests

convenient avatar Jun 09 '22 11:06 convenient

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@magento run all tests

convenient avatar Jun 09 '22 11:06 convenient

@magento run all tests

convenient avatar Jun 09 '22 11:06 convenient

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@magento run all tests

convenient avatar Jun 09 '22 12:06 convenient

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

PHPStan detected violation(s):
 ------ ------------------------------------------------------------------- 
  Line   integration/framework/Magento/TestFramework/ErrorLog/Logger.php    
 ------ ------------------------------------------------------------------- 
  82     Method Monolog\Logger::addRecord() invoked with 4 parameters, 2-3  
         required.                                                          
 ------ ------------------------------------------------------------------- 

I dont know how your static tests are bootstrapped but evidently not pulling in the new version of monolog.

When i do a composer create project they're definitely pulling it in

convenient avatar Jun 09 '22 15:06 convenient

@magento run Unit Tests

convenient avatar Jun 10 '22 08:06 convenient

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

Hi @convenient you'll probably want to add use Monolog\DateTimeImmutable; after line 11 in this Logger file.

If you look at Monolog they're using their own class for DateTimeImmutable

  • https://github.com/Seldaek/monolog/tree/main/src/Monolog

We ran across this same problem in the Drupal monolog module: https://www.drupal.org/project/monolog/issues/3284825

zrpnr avatar Jun 10 '22 16:06 zrpnr

Huh thanks @zrpnr

I didn't spot that was just aiming for green tests 😅

convenient avatar Jun 10 '22 16:06 convenient

@magento run all tests

convenient avatar Jun 12 '22 11:06 convenient

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@magento run Functional Tests EE, Functional Tests CE, Functional Tests B2B

convenient avatar Jun 13 '22 08:06 convenient

Before I spend any time fighting test cases can I please have some feedback on the official monolog stance on what Magento is doing here, see https://github.com/Seldaek/monolog/issues/1684#issuecomment-1152874168

Your PR works @convenient I guess but really I do wonder why Logger was extended here instead of adding a custom Handler to collect less-than-error records. That's what handlers are for, overriding addRecord for this is madness IMO. You could even do it with native handlers from monolog combining a FilterHandler and a TestHandler (or just the latter..) to gather records and access them again in the test.

Seems to me using a handler is the right thing to do here

convenient avatar Jun 13 '22 08:06 convenient

Looks like in Magento 2.4.5 we have a fix for this issue with the same changes https://github.com/magento/magento2/commit/360fd1ba9d6e67185b7fbd3e00223cd948bb896e I believe soon the last release will be merged to 2.4-develop, so we'll see the conflict there.

ihor-sviziev avatar Aug 09 '22 21:08 ihor-sviziev

Thanks for the fyi @ihor-sviziev . I did spot this in the pre release a week or so back.

In these scenarios I wonder why my commits aren't cherry picked or merged rather than doing new ones. attribution isn't a massive deal but I've clearly done the leg work here

From identifying the issue (an hour or so after it appeared), to writing a bug report, speaking with the upstream developers, and trying to have a communication with Magento about a better way of fixing it than what was in this PR.

convenient avatar Aug 09 '22 21:08 convenient

@convenient true! /CC @sidolov @sdzhepa @mahesh-singh-rajawat

ihor-sviziev avatar Aug 09 '22 21:08 ihor-sviziev

Hello @convenient and @ihor-sviziev

Thank you for pointing out this situation!

Unfortunately, I do not have a clear and transparent answer to "Why did it happen?" But I agree it is absolutely not acceptable.

We are working with teams and @mahesh-singh-rajawat to prevent this kind of situation in future

Sorry for the incident and any inconveniences

cc: @sidolov

sdzhepa avatar Aug 12 '22 14:08 sdzhepa