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

Ignore Pest Files when finding and loading Classes #219

Merged
merged 11 commits into from
Aug 12, 2023
Merged

Conversation

stefanzweifel
Copy link
Owner

@stefanzweifel stefanzweifel commented Aug 12, 2023

This PR is an attempt to solve compatibility issues when the laravel-stats package is run in a project that uses PestPHP.

Due to the nature of how laravel-stats currently works, we can't easily provide support for Pest Tests. (stats searches for PHP Classes in your project and groups them into Components. Pest Tests are PHP files containing functions.)

This PR attempts to solve the compatibility issue, by checking the contents of a PHP file, before requiring it.
If the file contains "test" or "Test" and uses one of the prominent Pest PHP methods like test(), it(), describe() or beforeEach(), we consider it a Pest PHP file and exclude it from being required.
(Might be a too naive approach, but I can't think of anything better right now.)

More Technical Notes

When users run php artisan stats in a project that uses Pest, they are immediately greeted with the following error message.

Pest\Exceptions\InvalidPestCommand

Please run [./vendor/bin/pest] instead.

This happens as stats immediately starts looking for files in a project and requiring them in the ClassesFinder-class.
When a Pest file is required, the test() or it() method seems to be immediately triggered or executed, which in turn will execute the following line in Pest that runs TestSuite::getInstance().

https://github.com/pestphp/pest/blob/f1414a0beb43da066cf33276e22ea1209f047667/src/Functions.php#L121

As Pest is not triggered in a test environment the InvalidPestCommand exception is thrown.

Related Issues

TODOs

  • Add Note about Pest PHP compatibility to README

@codecov
Copy link

codecov bot commented Aug 12, 2023

Codecov Report

Merging #219 (c29f571) into main (025094d) will decrease coverage by 0.01%.
Report is 1 commits behind head on main.
The diff coverage is 95.45%.

@@             Coverage Diff              @@
##               main     #219      +/-   ##
============================================
- Coverage     95.47%   95.47%   -0.01%     
- Complexity      259      266       +7     
============================================
  Files            44       44              
  Lines           752      774      +22     
============================================
+ Hits            718      739      +21     
- Misses           34       35       +1     
Files Changed Coverage Δ
src/ClassesFinder.php 95.74% <95.45%> (-0.26%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@stefanzweifel stefanzweifel marked this pull request as ready for review August 12, 2023 10:18
@stefanzweifel stefanzweifel merged commit 5daa7ea into main Aug 12, 2023
15 of 17 checks passed
@stefanzweifel stefanzweifel deleted the ignore-pest branch August 12, 2023 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant