A large number of web applications that we test at TestMatick are built on a free open-source web framework – Laravel.
We can surely say that Laravel completely supports PHPUnit test environment in its core. As we know, PHPUnit is the most popular framework for testing the functionality of PHP code. It helps us to easily create 2 types of tests – unit and functional tests.
We will start analyses of the capabilities of Laravel framework from its grounds, namely from the definition of the unit and functional testing. As we move forward, we will see a complete picture of the development of unit and functional tests in the application.
Functional and Unit Tests
If you are familiar with interaction with PHPUnit, you should understand that any testing in this environment can be easily divided into 2 categories: unit testing and functional testing.
In the process of unit testing, you have a task to assess a previously established function or a special method. Just as important, you test a part of the logic of a written code at present time.
In the product developed by your quality control company, you can find the rule that your announced method consists of more than one logic block, so you should think about dividing it into certain methods to make each one include logically correctly built fragment of program code.
To completely understand the logic of the process of unit testing, you can look at this example of a code:
According to written functions, the method performs only one programmed action. It uses ucfirst function to quickly transform one title into another one with the uppercase.
Whereas we use unit test only for verification of the correct building of a logical value of program code, the functional test gives us a possibility to test the correctness of establishing the correct variant of actions. Analyzing this definition more thoroughly, functional testing allows a tester to simulate a set of actions which may be performed by a client inside the utility in order to run his necessary variant.
A good example is that you as QA can perform functional testing for a certain group of input parameters which usually consists of the following steps:
- Developing the GET query to access the login page;
- Checking whether a correct page has been displayed;
- Creating the POST query to immediately send the data to the web page with your login;
- Checking whether your created session has been successful.
Only in this way you can quickly and efficiently create a functional test case. In the next chapters, we will analyze the simple examples of how to efficiently and quickly create simple functional and unit tests in Laravel application.
Delivering the Preconditions
Before we start to create the group of actual tests, we should do some things which will interact with our created tests.
First of all, we should create the Post working model and also the migration connected with it. For this you need to write a special artisan command which is exactly responsible for the development of the post model:
Right this command allows us to generate the class of the post model and also the database migration connected with them.
A developed class of the post model must look like that:
And to make the file of performed migration be in working condition, you should mention a correct way of its location:
Of course, we must save the post title. For this you need to qualitatively process the program code of the file of your post migration to make it have such a structure:
According to the created scheme, we can see that a special column which is responsible for saving our title with messages has been added.
Then you just need to run the special migrate command in order to create a necessary table with the common database.
Then you should completely change the used post model to the following:
We have just added a special accessor method which helps us to edit titles of the messages, namely the manipulations which are necessary for your example of the test. All this is also completely true for the post model we analyzed before.
Now our task is to create a file container which will help us to design a necessary functional test script at the final stage of testing.
Inside the index method, we should try to extract the message identifier from the set of parameters of our query with a future downloading of post model.
Specify the path to the necessary file:
Unit Testing of the Application in Laravel
Previously, we analyzed step-by-step instruction to configure our primary test. Now we will try to re-create the example which helps to understand how unit testing concept interacts inside Laravel.
Usually, Laravel utility is the artisan command which helps the testers to play with a classical typical class of its unit test.
Let’s run this command in order to create a special AccessorTest class. In this case, it’s very important to have a word “unit” which helps to create a unit test case located in the specified directory of tests/unit.
This manipulation will help us to create the following important class inside it:
Add a “useful” program code to it:
As we can see, a program code completely overlaps with the things we can see in any core of PHP. We just needed to import all dependencies of code from Laravel utility which can help us to connect all API, necessary for the test.
So, we’ve done with unit testing in Laravel!
Firstly, we need to create preconditions in order to work in the functional test environment (in our case, AccessorTest). As we don’t use a functional expression – unit, all our work will be regarded as a multi-functional test case with shifting to a special catalog – tests/feature.
It will help to create the necessary class:
Then change everything to the necessary program code:
We must repeat once more: this code must look familiar for us, especially if you are familiar with functional testing.
So what we got: first of all, we managed to extract the post from the mentioned database and know that value inside $db_post_title will change. Then we will have a possibility to simulate the “GET” query and to capture the obtained response into the $response variable.
Then we analyzed the response code inside $response variable with the expected response. According to our basic data, it must be 200 since we must get a real report on GET query. A special title must be also mentioned in the response which starts with uppercase and it’s exactly what we need in the process of interaction with the assertSeeText method.
Some Words about Integration Testing in Laravel
Interaction with integration tests – it’s a combination of performing the established metrics in the process of interaction with a particular visual product. Such test cases are completely different from typical unit tests which test the parts of utility which are independent of each other.
Usually, integration tests allow your application to process particular information blocks (for example, the logic of visiting a particular web page after certain manipulations) and check the functionality of the utility to be responsible for established commands.
In its nature, every integration test interacts with Laravel application in the same way as with a simple black box.
They don’t “care” what is inside the utility, the obtained result is above all.
To conclude, we can say that we have thoroughly analyzed the process of testing in Laravel environment which supports a program structure of PHPUnit in its environment. We studied the bases, analyzed the principles of building unit and functional tests and also understood some peculiarities of testing in the particular functional context of Laravel.
In the process of studying of capabilities of testing in Laravel, we created some good examples which help us to understand how actually to quickly create unit and functional test cases on the basis of established artisan command.