Transporter is a futuristic way to send API requests in PHP. This is an OOP approach to handle API requests.
You can install the package via composer:
composer require juststeveking/laravel-transporter
You can publish the config file with:
php artisan vendor:publish --provider="JustSteveKing\Transporter\TransporterServiceProvider" --tag="transporter-config"
The contents of the published config file:
return [ 'base_uri' => env('TRANSPORTER_BASE_URI'), ];
To generate an API request to use with Transporter, you can use the Artisan make command:
php artisan make:api-request NameOfYourRequest
This will by default publish as:
Transporter Requests are an extention of Laravels
PendingRequest so all of the methods available on a Pending Request is available to you on your requests.
Also when you send the request, you will receive a
Illuminate\Http\Client\Response back, allowing you to do things such as
failed() very easily. We are simply just shifting how we send it into a class based approach.
TestRequest::build() ->withToken('foobar') ->withData([ 'title' => 'Build a package' ]) ->send() ->json();
When building your request to send, you can override the following:
- Request Data using
- Request Query Params using
- Request Path using
Instead of the standard
send() method, it is also possible to use the fun alias
energize(). Please note, no sound effects are included.
TestRequest::build() ->withToken('foobar') ->withData([ 'title' => 'Build a package' ]) ->energize() ->json();
Faking a Request
To fake a request, all you need to do is replace the build method with the fake method, which takes an optional
status parameter, to set the status code being returned with the response:
TestRequest::fake( status: 200, )->withToken('foobar') ->withData([ 'title' => 'Build a package' ])->withFakeData([ 'data' => 'faked' ])->send();
Which will return a response with the data you pass through to
withFakeData, which internally will merge what is on the class with what you pass it. So you can build up an initial state of faked data per class.
To run the tests in parallel:
composer run test
To run the tests with a coverage report:
composer run test-coverage
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.