Laravel: Testing Database Driven applications with PHPUnit Part 1
Introduction
When developing database driven applications, how do we ensure that our CRUD operations work as expected? by writing database tests to check that records can be inserted, fetched, updated or deleted successfully. In future, if we happen to introduce a bug; maybe by renaming a database table or column and then forgetting update our code, the tests will tell us that the code is broken. Thus we’re are able to detect software glitches before users do.
Case Study
In this tutorial, we are going to create a fictional banking system that will enable a user to manage customers record and issue bank loans to the customers.
Installation and Database setup
First let’s create a Laravel project. Run the following command on your terminal or Command Prompt.
composer create-project laravel/laravel bank-app-demo
This tutorial assumes that you have PHP and composer installed. Also check the minimum version of PHP required for the version of Laravel you are using.
If you’ve successfully created the project navigate to the projects folder by executing this command.
cd bank-app-demo
A fresh Laravel installation will create .env file. If this file is missing on your project just run this command to create one:
cp .env.example .env
The .env file maybe hidden depending on your operating system settings. This is because it starts with a period(.)
Use your favourite DBMS or MySQL Shell to create two databases: one for development and the other for running tests. For conformity let’s name them db_bank_app and db_bank_app_testing.
The reason why we need a separate database to run tests on is because the database is reset after each of your tests so that data from a previous test does not interfere with subsequent tests. Also you can use a different database engine apart from MySQL; you just need to change the database driver where necessary.
Open .env file and add development database name, server username and password.
DB_DATABASE=db_bank_app
DB_USERNAME=your database server username
DB_PASSWORD=your database server
Create another file to set environment variables for the testing database:
cp .env.example .env.testing
Open .env.testing and update variables as shown below:
APP_ENV=testing
DB_CONNECTION=testing
DB_DATABASE=db_bank_app_testing
Host, Port, database Username and Password are the same for both connections. In .env.testing if you set value for DB_CONNECTION to anything other than testing, make sure you have updated phpunit.xml.
Update the database.php config file with the test connection. The file is location in config folder. Just add the code below the mysql connection.
Project Layout
Now we need define the project layout using bootstrap to style our blade files. On the views folder create folder named partials. The folder path should look likes this: resources/views/partials. In the partials folder create a file named header.blade.php. Open the header file and add the following code:
Still under partials folder create another file named footer.blade.php. Add the following code.
Under views folder, create another folder named layouts. The folder path should be like this: resources/views/layouts. Under the layouts folder create a file named master.blade.php. Add the following code:
Still under views folder create another folder named customers. The folder path should be like this: resources/views/customers. Under customers folder create a filed named index.blade.php. Add the following code:
Run the project by executing this command (Preferably on a new terminal or Command Prompt. NB: Remember to navigate to the project’s folder)
php artisn serve
Update the default route to load the index file instead of the default welcome page.
Route::get('/', function () {
return view('customers.index');
});
If you visit http://localhost:8000 on your browser you should see your view the title Customers list and the copyright information we added on the footer. See figure below:
In part 2 we’ll continue with writing tests and implementing the code under test.