Rector to replace deprecated Laravel service mocking testing methods
remarkablemark/rector-laravel-service-mocking is a Laravel package for rector to replace deprecated laravel service mocking testing methods.
It currently has 0 GitHub stars and 15 downloads on Packagist (latest version 3.0.0).
Install it with composer require remarkablemark/rector-laravel-service-mocking.
Discover more Laravel packages by remarkablemark
or browse all Laravel packages to compare alternatives.
Last updated
Rector to replace deprecated Laravel service mocking testing methods such as expectsEvents, expectsJobs, and expectsNotifications.
From Laravel 10:
The deprecated
MocksApplicationServicestrait has been removed from the framework. This trait provided testing methods such asexpectsEvents,expectsJobs, andexpectsNotifications.If your application uses these methods, we recommend you transition to
Event::fake,Bus::fake, andNotification::fake, respectively. You can learn more about mocking via fakes in the corresponding documentation for the component you are attempting to fake.
PHP >=8.2
Install with Composer:
composer require --dev rector/rector remarkablemark/rector-laravel-service-mocking
Register the rule in rector.php:
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Remarkablemark\RectorLaravelServiceMocking\LaravelServiceMockingRector;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/tests',
]);
$rectorConfig->rule(LaravelServiceMockingRector::class);
};
See the diff:
vendor/bin/rector process --dry-run
Apply the rule:
vendor/bin/rector process
Clear the cache and apply the rule:
vendor/bin/rector process --clear-cache
$this->expectsEvents([MyEvent::class]);
\Illuminate\Support\Facades\Event::fake([MyEvent::class])->assertDispatched([MyEvent::class]);
The test may still fail because of assertDispatched so it's recommended to refactor to:
Event::fake([MyEvent::class]);
// dispatch your event here...
Event::assertDispatched(MyEvent::class);
If you have multiple events, call assertDispatched for each event:
Event::fake([MyEvent1::class, MyEvent2::class]);
// ...
Event::assertDispatched(MyEvent1::class);
Event::assertDispatched(MyEvent2::class);