Skip to content

Fixup expected jobs and events rule #307

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

Merged
merged 12 commits into from
Mar 12, 2025

Conversation

peterfox
Copy link
Collaborator

Changes

  • refactors the ReplaceExpectsMethodsInTestsRector rule to handle more scenarios
  • adds new tests
  • Adds new ExpectedClassMethodMethodCalls value object
  • Adds new DispatchableTestsMethodsFactory node factory
  • Adds new ExpectedClassMethodAnalyzer analyser

Why

The original version while working was a bit basic and only covered expectsJobs and expectsEvents. It never found any uses of doesntExpectJobs and doesntExpectEvents because it just felt too complex to make happen within a single rule.

The rule now handles it nicely.

 use Illuminate\Foundation\Testing\TestCase;

 class SomethingTest extends TestCase
 {
     public function testSomething()
     {
-        $this->expectsJobs([\App\Jobs\SomeJob::class, \App\Jobs\SomeOtherJob::class]);
-        $this->expectsEvents(\App\Events\SomeEvent::class);
-        $this->doesntExpectEvents(\App\Events\SomeOtherEvent::class);
+        \Illuminate\Support\Facades\Bus::fake([\App\Jobs\SomeJob::class, \App\Jobs\SomeOtherJob::class]);
+        \Illuminate\Support\Facades\Event::fake([\App\Events\SomeEvent::class, \App\Events\SomeOtherEvent::class]);

         $this->get('/');
+
+        \Illuminate\Support\Facades\Bus::assertDispatched(\App\Jobs\SomeJob::class);
+        \Illuminate\Support\Facades\Bus::assertDispatched(\App\Jobs\SomeOtherJob::class);
+        \Illuminate\Support\Facades\Event::assertDispatched(\App\Events\SomeEvent::class);
+        \Illuminate\Support\Facades\Event::assertNotDispatched(\App\Events\SomeOtherEvent::class);
     }
 }

@peterfox peterfox added the enhancement New feature or request label Feb 24, 2025
@peterfox peterfox self-assigned this Feb 24, 2025
@peterfox peterfox merged commit 03101b5 into main Mar 12, 2025
5 checks passed
@peterfox peterfox deleted the feature/fixup-expected-jobs-and-events-rule branch March 12, 2025 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants