Skip to content

Comments

[12.x] Run Mockery cleanup via PHPUnit subscriber instead of explicit m::close() calls#58278

Merged
taylorotwell merged 7 commits intolaravel:12.xfrom
lucasmichot:12x/mockery-close-teardown
Jan 5, 2026
Merged

[12.x] Run Mockery cleanup via PHPUnit subscriber instead of explicit m::close() calls#58278
taylorotwell merged 7 commits intolaravel:12.xfrom
lucasmichot:12x/mockery-close-teardown

Conversation

@lucasmichot
Copy link
Contributor

@lucasmichot lucasmichot commented Jan 5, 2026

Summary

This PR centralizes Mockery cleanup by registering a PHPUnit event subscriber that calls Mockery::close() after each test method.

As a result:

  • Explicit m::close() calls are removed from individual tests
  • Mockery is consistently referenced via the m alias
  • Cleanup is guaranteed even when tests exit early or throw

Motivation

Historically, many tests relied on manually calling m::close() in tearDown() or inline within test cases. This approach is:

  • Easy to forget
  • Inconsistent across the test suite
  • Fragile when tests fail early

By moving cleanup to a PHPUnit subscriber, Mockery teardown becomes automatic, global, and uniform.

What changed

  • Added a PHPUnit runner extension that registers an AfterTestMethodFinished subscriber
  • The subscriber calls Mockery::close() after every test
  • Removed redundant m::close() calls from tests
  • Standardized Mockery usage to the m alias throughout the suite

Benefits

  • Eliminates an entire class of test hygiene issues
  • Reduces boilerplate in tests
  • Ensures deterministic Mockery cleanup
  • Aligns with PHPUnit ≥10 event-based architecture

Notes

  • The subscriber runs once per test method
  • This change does not alter test behavior, only cleanup mechanics
  • Compatible with PHPUnit 10+

@github-actions
Copy link

github-actions bot commented Jan 5, 2026

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@lucasmichot lucasmichot force-pushed the 12x/mockery-close-teardown branch from 0696022 to 3b17674 Compare January 5, 2026 16:49
@lucasmichot lucasmichot changed the title [12.x] Always alias Mockery with m [12.x] Use a PHPunit extension Jan 5, 2026
@lucasmichot lucasmichot changed the title [12.x] Use a PHPunit extension [12.x] Run Mockery cleanup via PHPUnit subscriber instead of explicit m::close() calls Jan 5, 2026
@lucasmichot lucasmichot marked this pull request as ready for review January 5, 2026 16:59
@taylorotwell taylorotwell merged commit 7b3351e into laravel:12.x Jan 5, 2026
70 checks passed
@lucasmichot lucasmichot deleted the 12x/mockery-close-teardown branch January 5, 2026 17:13
DarkGhostHunter pushed a commit to DarkGhostHunter/laravel-framework that referenced this pull request Jan 5, 2026
… `m::close()` calls (laravel#58278)

* Always alias Mockery with m

* Fix tearDown visibility

* Ensure tearDown methods always also call their parent::tearDown methods

* No need to call m::close anymore

* Drop useless tearDown methods

* Use an extension instead of always closing Mockery

* Apply StyleCI fix

---------

Co-authored-by: Lucas Michot <lucas@zaiple.com>
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