Your Laravel application’s console kernel probably contains some scheduled tasks.
But how do you make sure they are configured correctly and no developer will accidentally remove or modify them?
Laravel’s HTTP test assertions are great for testing the response of your application.
They provide a lot of methods to assert the response status, headers, content, and more.
But sometimes you have a specific response that you want to test and there’s no built-in method for it, especially when there’s a repeating pattern.
There are many ways to modify data in a table during deployment.
Some applications use database seeds, some use console commands and some use custom scripts with raw SQL queries.
But in Laravel, migrations are the easiest and most convenient way to do this.
You’ve probably done it several times. You wanted to modify data in a table during deployment and you used a migration to do it.
It gets the job done, doesn’t require any additional setup/script/library, and is version-controlled by default.
Let’s imagine we have the following code that runs and conditionally throws an exception:
useSymfony\Component\HttpKernel\Exception\HttpException;classDeletePostAction{publicfunctionexecute(Post$post){if($post->is_published){thrownewHttpException(403,'Cannot delete a published post');}$post->delete();}}
If we want to write test coverage for this class it will require at least 2 test cases:
Assert that the action deletes the provided post successfully
Assert that the action throws an exception when the provided post is published
In the previous post, we discussed how to use pre-built Docker images with Laravel Vapor to reduce deployment time.
There I also mentioned using GitHub Actions to automate the deployment.
In this post, I want to showcase some examples, including one that uses a private AWS ECR repository as a base image to deploy Vapor applications using GitHub Actions.
Vapor’s custom Docker runtimes are great for customizing environment dependencies, adding PHP extensions, changing configuration, etc.
But doing all of this requires building the deployment image from scratch, on every single deployment.
If you are using tools like GitHub Actions to automate the deployment, increased deployment time will eat up your GitHub Actions minutes quickly.
In your Laravel controller or somewhere in your code you probably have a try/catch like this:
useApp\Exceptions\FailedRequestException;classApiController{publicstaticsendRequest(){$api=newSomeApi();try{$api->sendRequest();returnresponse()->json(['data'=>$api->getData(),]);}catch(FailedRequestException$e){returnresponse()->json(['error'=>'Failed to send API request',]);}}}
If you ever used Eloquent events, you are probably aware of special boot() static method in Eloquent models.
This method allows you to hook into special events by running given functions.
Here’s an example, let’s say we have a Post model and when we are creating a new post, model needs to generate slug attribute based on name.