paint-brush
Setting Up Laravel Unit and Security Testsby@erwinnerwin
335 reads
335 reads

Setting Up Laravel Unit and Security Tests

by ErwinFebruary 22nd, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Laravel Unit and Security Tests are a set of automated tests performed on a Laravel application to ensure its functionality and security. The tests are performed using PHPUnit and PHPCS and are executed on a Git Actions workflow. The following are the steps involved in performing Laravel Unit and Security Tests: Checkout the code from the repository. Set up PHP 8.1 as the PHP version to use. Restore the composer cache if available. Install the dependencies required for the application. Set up the Laravel application. Save the composer cache for future use. Set permissions for the storage and bootstrap/cache directories. Execute the unit and feature tests using PHPUnit. Check the code for any coding standards violations using PHPCS. Install a security checker package and run a security check on the application.
featured image - Setting Up Laravel Unit and Security Tests
Erwin HackerNoon profile picture

The Laravel Unit and Security Tests workflow is a powerful tool that can help you ensure the quality and security of your Laravel application. This workflow uses GitHub Actions to automate the testing process and check for any potential security vulnerabilities.

Step 1: Setting Up PHP 8.1

The first step of the workflow is to set up PHP 8.1, the latest version of the programming language. This is done using the "Setup PHP 8.1" step, which uses the

shivammathur/setup-php@v2 action
. This step will install and configure PHP 8.1 on the virtual machine.

Step 2: Restoring the Composer Cache

The next step is to restore the composer cache. This is done using the actions/cache@v2 action, which is designed to cache the dependencies of your application. This step is important because it speeds up the installation process and makes it more efficient.

Step 3: Installing Dependencies

The third step is to install the dependencies required by your Laravel application. This is done using the composer install command, which downloads and installs the required packages. This step is crucial for ensuring that all the necessary components are in place for the application to work correctly.

Step 4: Setting Up the Application

The fourth step is to set up the application. This is done using several php commands, which copy the .env.example file to

.env
, generate an application key, and set the directory permissions. These commands are necessary for getting your application ready for testing.

Step 5: Saving the Composer Cache

The fifth step is to save the composer cache. This is done using the

actions/cache@v2
action, which caches the dependencies of your application. This step ensures that the dependencies are cached for future runs of the workflow, which makes the process more efficient.

Step 6: Executing Tests (Unit and Feature Tests) via PHPUnit

The sixth step is to execute the tests for your Laravel application. This is done using the

vendor/bin/phpunit
command, which runs the unit and feature tests for your application. This step is crucial for ensuring that your application is working as expected and that there are no bugs.

Step 7: Code Check Using PHPCS

The seventh step is to perform a code check using PHPCS. This is done using the composer run test command, which runs the code checker and identifies any issues with your code. This step is important for ensuring that your code is readable and adheres to best practices.

Step 8: Installing the Package Security Checker

The eighth step is to install the Package Security Checker. This is done using the

composer require --dev enlightn/laravel-security-checker
command, which downloads and installs the security checker package. This package is used to check for any potential security vulnerabilities in your application.

Step 9: Running the Security Check

The final step is to run the security check. This is done using the php artisan security:check command, which runs the security checker and identifies any potential security vulnerabilities. This step is crucial for ensuring that your application is secure and that there are no potential security threats.

Full yaml file:

name: Laravel Unit and Security Tests

on:
pull_request:
branches:
- master
- staging

jobs:
laravel-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup PHP 8.1
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
- name: Restore composer cache
uses: actions/cache@v2
id: composer-cache
with:
path: '/vendor'
key: php-${{ hashFiles('composer.lock') }}
restore-keys: |
php-${{ hashFiles('composer.lock') }}
- name: Install Dependencies
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader
- name: Setup application
run: |
php -r "copy('.env.example', '.env');"
php artisan key:generate
- name: Save composer cache
uses: actions/cache@v2
with:
path: vendor
key: php-${{ hashFiles('composer.lock') }}
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
- name: Code check using PHPCS
run: composer run test
- name: Install Package security checker
run: composer require --dev enlightn/laravel-security-checker
- name: Run security check
run: php artisan security:check

Conclusion:

The Laravel Unit Test and Github Workflow is a crucial process that guarantees the quality and reliability of Laravel applications. Through automating the testing procedure, time is saved and the risk of human error is minimised, allowing for early detection and resolution of bugs and security vulnerabilities. Laravel's built-in testing features, such as its unit tests, allow for automated testing of the application code, ensuring it functions properly and is secure against potential attacks. This results in the delivery of high-quality and secure software to users, maintaining the confidence in the application's stability over time. The integration of the Laravel Unit Test and Github Workflow streamlines the development process and provides a solid foundation for a successful project.