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

Clock Mocking doesn't work from v3.12.0 #2700

Open
rasstislav opened this issue Sep 20, 2023 · 8 comments
Open

Clock Mocking doesn't work from v3.12.0 #2700

rasstislav opened this issue Sep 20, 2023 · 8 comments

Comments

@rasstislav
Copy link

rasstislav commented Sep 20, 2023

Hello,

this commit broke Clock mocking.

Is it possible to fix it?

Thanks

@phansys
Copy link
Collaborator

phansys commented Sep 21, 2023

I didn't check the clock mocking features from symfony/phpunit-bridge. Limiting the syntax in the codebase based on the limitations of this implementation is something I'd like to avoid.
Could you please confirm if the same occurs for other packages with the same purpose? (like slope-it/clock-mock).

BTW, in case we consider this as a bug, we must provide the required test cases to avoid regressions in the future.

@boedah
Copy link

boedah commented Oct 11, 2023

Hi @phansys !

After upgrading DoctrineExtensions some of our tests also started failing.

ClockMock of phpunit-bridge relies on dynamically replacing calls to global date-/time-related functions.
The commit linked by @rasstislav replaced time() with new \DateTime(), so the mocked date is not used.

I looked at the library you linked slope-it/clock-mock, but it requires a PHP extension.

A quick win for DoctrineExtensions would be to replace the new \DateTime() call with new \DateTime::createFromFormat('U', (string) time()) (as suggested in the Symfony docs).

If you want to allow this change I could provide a PR including a test to ensure interoperability with phpunit-bridge.

@stof
Copy link
Contributor

stof commented Oct 24, 2023

The future-proof way would be to support injecting a PSR-20 clock in the listener and using it to get the current time.

@Chris53897
Copy link
Contributor

@rasstislav Can this be closed as completed (see linked PR)?

@rasstislav
Copy link
Author

@Chris53897 my tests are still not working due to DateTime as @boedah also mentioned.

@rasstislav
Copy link
Author

@boedah, can you please test it as well?

@mbabker
Copy link
Contributor

mbabker commented Feb 26, 2024

@Chris53897 my tests are still not working due to DateTime as @boedah also mentioned.

What tests specifically? Without a specific use case, it's hard to know what it is that's causing your issue. The last release supports injecting a PSR-20 where practical, with the only places using still using new \DateTime() being an ODM query filter where injecting a clock isn't really feasible (or if it is I guess that'd be through the parameters API, but not being an ODM user I couldn't tell you if that's good or bad), and the setLoggedAt() methods on the base log entry document/entity classes that it's recommended to extend from for that integration, and you can work around that with implementing your own log entry object and overloading those methods to use some other time source if need be.

@boedah
Copy link

boedah commented Mar 1, 2024

@rasstislav I can confirm it works now.

Unfortunately, Symfony\Component\HttpFoundation\Session\Storage\MetadataBag (also in initialize in line 53) still uses time() instead of a PSR-20 Clock.

This is what I have done in a Symfony 5.4 app:

  1. Alias the clock service:
    clock:
        class: Symfony\Component\Clock\Clock
        autowire: false
        autoconfigure: false
    Symfony\Component\Clock\ClockInterface: '@clock'
    Psr\Clock\ClockInterface: '@clock'
  1. In tests using the MetadataBag (e.g. session inactive tests), still use ClockMock::withClockMock of Symfony PHPUnit Bridge
  2. In all other tests use ClockSensitiveTrait of Symfony Clock

Hope this helps :)

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

6 participants