Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/remarkablemark/rector-laravel-service-mocking
🐘 Rector Laravel service mocking
https://github.com/remarkablemark/rector-laravel-service-mocking
codemod composer laravel mocking php rector refactoring service
Last synced: 11 days ago
JSON representation
🐘 Rector Laravel service mocking
- Host: GitHub
- URL: https://github.com/remarkablemark/rector-laravel-service-mocking
- Owner: remarkablemark
- License: mit
- Created: 2023-12-10T23:38:44.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2024-04-25T02:51:32.000Z (7 months ago)
- Last Synced: 2024-05-01T21:25:45.203Z (6 months ago)
- Topics: codemod, composer, laravel, mocking, php, rector, refactoring, service
- Language: PHP
- Homepage: https://packagist.org/packages/remarkablemark/rector-laravel-service-mocking
- Size: 87.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# rector-laravel-service-mocking
[![packagist](https://img.shields.io/packagist/v/remarkablemark/rector-laravel-service-mocking)](https://packagist.org/packages/remarkablemark/rector-laravel-service-mocking)
[![test](https://github.com/remarkablemark/rector-laravel-service-mocking/actions/workflows/test.yml/badge.svg)](https://github.com/remarkablemark/rector-laravel-service-mocking/actions/workflows/test.yml)[Rector](https://github.com/rectorphp/rector) to replace deprecated Laravel service mocking testing methods such as `expectsEvents`, `expectsJobs`, and `expectsNotifications`.
From [Laravel 10](https://laravel.com/docs/10.x/upgrade#service-mocking):
> The deprecated `MocksApplicationServices` trait has been removed from the framework. This trait provided testing methods such as `expectsEvents`, `expectsJobs`, and `expectsNotifications`.
>
> If your application uses these methods, we recommend you transition to `Event::fake`, `Bus::fake`, and `Notification::fake`, respectively. You can learn more about mocking via fakes in the corresponding documentation for the component you are attempting to fake.## Requirements
PHP >=7.4
## Install
Install with [Composer](http://getcomposer.org/):
```sh
composer require --dev rector/rector remarkablemark/rector-laravel-service-mocking
```## Usage
Register the rule in `rector.php`:
```php
paths([
__DIR__ . '/tests',
]);
$rectorConfig->rule(LaravelServiceMockingRector::class);
};
```See the diff:
```php
vendor/bin/rector process --dry-run
```Apply the rule:
```php
vendor/bin/rector process
```Clear the cache and apply the rule:
```php
vendor/bin/rector process --clear-cache
```## Rule
### Before
```php
$this->expectsEvents([MyEvent::class]);
```### After
```php
\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:
```php
Event::fake([MyEvent::class]);
// dispatch your event here...
Event::assertDispatched(MyEvent::class);
```If you have multiple events, call `assertDispatched` for each event:
```php
Event::fake([MyEvent1::class, MyEvent2::class]);
// ...
Event::assertDispatched(MyEvent1::class);
Event::assertDispatched(MyEvent2::class);
```## License
[MIT](LICENSE)