Request Factories v3 released 🥳

Published 27/01/2023 | 1225 views

Today, Worksome released Request Factories v3, and upgrading is super simple! Let's take a look.

Today, Worksome released Request Factories v3. Not much has changed, but I wanted to go over why we decided to make a breaking change here, and how you can quickly move on over to v3.

One of the biggest complaints we received was that if you were making use of the HasFactory trait, which to be fair we told you to do, you could no longer require the package as a dev dependency; it had to be installed in production as well. Laravel follows this same paradigm, but for Laravel it isn't really an issue - of course Laravel is going to be installed in production!

<?php
 
namespace App\Http\Requests;
 
use Illuminate\Foundation\Http\FormRequest;
use Worksome\RequestFactories\Concerns\HasFactory;
 
class StoreRequest extends FormRequest
{
// PHP will throw an error here in production if the package is installed as a dev dependency
use HasFactory;
}

Seen as Request Factories are only used in tests, it seems a shame to have to install the package in production. With this in mind, and after a brilliant suggestion on GitHub discussions, we decided to remove the trait and use macros to achieve the same result.

This means that your tests can still call MyFormRequest::fake(), but you don't need to add the HasFactory trait to your Form Requests anymore. Obviously, removing the old trait means that we introduce a breaking change. However, it's super quick to upgrade:

  1. Remove all instances of use HasFactory in your app/Http/Requests directory.
  2. Remove all instances of use Worksome\RequestFactories\Concerns\HasFactory; in your app/Http/Requests directory.
<?php
 
namespace App\Http\Requests;
 
use Illuminate\Foundation\Http\FormRequest;
-use Worksome\RequestFactories\Concerns\HasFactory;
 
class StoreRequest extends FormRequest
{
- use HasFactory;
}

That's it! You can likely use your IDE to do all the heavy lifting here with a find/replace. No need to change any tests, the ::fake() and ::factory() methods will still work exactly as they did before.

The upshot of this is that you can now safely move worksome/request-factories from your composer.json require to require-dev, making your production build all that leaner.

Bumping to PHP 8.2

We also took this opportunity to make PHP 8.2 the required minimum for Request Factories v3. If you're not yet ready to make the switch to 8.2 don't worry: you can still use v2 without any issues!

PHP 8.2 brings a host of cool changes and we like to make sure we're as up to date as possible so that we can make full use of all the new goodies in our codebase. In fact, we're already making extensive use of readonly classes.

Wrapping up

So that's Request Factories v3! Nothing super shiny, but worth the write up to make sure you're able to upgrade as quickly as possible without any headaches. Be sure to ⭐️ the repo on GitHub if you haven't already and reach out on Twitter for any thoughts or ideas you might have.

Happy coding! Luke

Like what you see?

If you enjoy reading my content, please consider sponsoring me. I don't spend it on cups of coffee; it all goes towards freeing up more of my time to work on open source, tutorials and more posts like this one.