Skip to content

Conversation

jnoordsij
Copy link
Contributor

@jnoordsij jnoordsij commented Sep 1, 2025

Inspired by #56518, this PR aims to introduce SetUp and TearDown attributes for (trait) methods, that allow executing them during the respective hook calls in a test lifecycle. This allows for more flexibility on these kind of methods, e.g. no longer requiring exact name matching.

To illustrate the usage, it has also been added to the WithConsoleEvents trait.

Implementation choices:

  • the naming of attributes can be changed to liking; this seemed the simplest to me
  • I've altered the respective test to use a call-count mechanism to ensure methods are only called once; one can also verify that adding the attribute to the method with correct name indeed only results in a single call
  • after digging a bit further, the Before and After PHPUnit attributes (see also https://docs.phpunit.de/en/12.3/attributes.html#before) already introduce very similar possibilities, so it's also an option to prefer those

Potential follow-ups:

  • applying this to more internal traits to simplify the list of other trait setup calls inside setUpTraits; this would likely require some additional mechanism to enforce correct order of executing hooks
  • applying to the WithRedis trait, although its setUp and tearDown hooks are seemingly called in two different ways (right away vs. hooked to test application lifetime)

@taylorotwell
Copy link
Member

I dunno - if nothing isn't too broken atm I'll probably just leave it rather than introduce a new concept.

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

Successfully merging this pull request may close these issues.

2 participants