Unit Testing in PHP with PHPunit
We will learn about unit test. in this tutorial I will use https://phpunit.de/. Php unit is a very popular framework testing. well in some other programming languages. there are also other unit tests. the usefulness of this framework is not a framework for building websites like laravel or codeigneter. but the framework is for testing, so we can test our code automatically. when we test a small unit or one small function of our website. for more details , lets start the tutorial.
What Will I Learn?
- Setting Unit test
- Using PSR-4 Autoload
- Make unit testing to test the functions
Requirements
- Install Php unit
- Localhost (xampp,wampp or etc)
- Intermediate PHP OOP
- Composer
Difficulty
- Advanced
Install Php Unit and Settings
The first step , We must to install the dependency of phpunit first, to install it. I use the composer. My php version is php version 5.6, So I use version 5 on php unit. if version of php> = 7. You should use php unit 6 .
composer require --dev phpunit/phpunit ^5
.xml .
<phpunit
bootstrap="vendor/autoload.php"
colors="true"
stopOnFailure="true">
<testsuites>
<testsuite name="testsuite">
<directory>Test</directory>
</testsuite>
</testsuites>
</phpunit>
bootstrap="vendor/autoload.php" : Files that we need to load before we run its test.
colors="true" : We give the color in terminal to be seen more clearly if an error occurs . The value is boolean(true or false).
stopOnFailure="true": To stop the code in case of error. The value is boolean(true or false).
< testsuites >< /testsuites > : Keep in mind the < testsuites >tag, under < phpunit >tag, and the < testsuites > tag , add < testsuite > tag .
app Folder : In this folder we will put the projects
testFolder : In this folder we will make our code testing.
{
"require": {
"phpunit/phpunit": "5"
},
"autoload":{
"psr-4":{
"App\\": "app"
}
}
}
"App\": "app" : "App\" is the namespace system we will use. and "app" is the name of the folder that we will load. Well after we specify its autoload system, we must update composer.json, in this way :
composer dump-autoload -o
Create Testing Unit
There is an interesting in unit testing system, usually when we want to test something we first create a function then we testing the function. but most programmers advise you to create unit testing first. It's ounds a little weird but there is a reason that will why we make unit testing, The first if we make a function or a project that is large of course we will be lazy to make its unit testing. The second We have a purpose about the function of coding that we will make.
setFirstName('Utopian');
$this->assertEquals($user->getFirstName(),'Utopian');
$user->getFirstName();
}
}
use PHPunit\Framework\TestCase; : Use dependency of PHPunit.
use App\models\Users; : Use the user class in the app folder.
class testingUser extends TestCase : We extends the existing classes in the PHP dependency unit TestCase.
$user->setFirstName('Utopian'); : This is a function that I will test, well the function will be created in model->user.
$this->assertEquals($user->getFirstName(),'Utopian'); : We can compare by using $user->getFirstName(), when we get first name we will compare with ''utopian ''.
$user->getFirstName(); : This function to get getFirstName.
Users.php
firstName = $firstName;
}
public function getFirstname(){
return $this->firstName;
}
}
namespace App\models; : The namespace system we created using psr-4.
public $firstName; : for property declarations, the name is $firstName
setFirstname($firstName){}: This function to set its username, the setFirstname must be same with then function name in testingUser.php.
$this->firstName = $firstName; : To assign property value, we give its property firstName name and its value is taken from $ firstName.
function getFirstname(){} : This function to get firstname to get the name we have created in unit test before, Its with 'utopian' name.
return $this->firstName; : We can return the firstName , So We can get the name .
And we can try to check if our unit testing is running well, you can open your project and run a command prompt and type phpunit. If there is no error you can see the testing run properly.
And in the end we have created a unit test to see if the function that we run is working as it should. It is a bit confusing when we have to make unit tests first , before we make our function. But it will benefit a lot if we use the system. Thank you for following my tutorial. hopefully this tutorial useful for you.
Posted on Utopian.io - Rewarding Open Source Contributors
Leave Unit Testing in PHP with PHPunit to:
Read more #utopian-io posts
Best Posts From Ryan alfarisi
We have not curated any of alfarisi94's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Ryan alfarisi
- A cup of coffee
- Camping in the forest with a thousand stars in the night
- Amazing !! Swimming pools from springs
- My vocation
- Pagination with PHP OOP system #3 Create a dynamic search system, Page between
- Pagination with PHP OOP system #3 Create search system, Dynamic SQL Query
- Pagination with PHP OOP system #2 Create the previous page, Create the next page, Active class in PHP
- Pagination with PHP OOP system #1 Basic OOP Class, Fetch data with PDO Database, Use Function in a Class
- Create a Project Board Using Materialize CSS #4 Update the order of items in each group and Move items to another group
- Create a Project Board Using Materialize CSS #3 Update the Group Order and Request AJAX
- Create a Project Board Using Materialize CSS #2 Connect database and Display data in Sortable Element
- Create a Project Board Using Materialize CSS #1 Sortable List, Sort Group, Sort list in Group,
- Consuming JWT in Client side with Vuejs
- Consuming JWT in Client side with Jquery
- Consuming JWT API with MongoDB and Node.js part-3# Verify Token, Decode Token , Route Protection
- error!
- Consuming JWT API with MongoDB and Node.js part-2# User Validation, Create token.
- Consuming JWT API with MongoDB and Node.js part-1# Setup JWT, Setup database, Create Route API
- Create Our Own CSS Frameworks part-6 (Create horizontal cards)
- Create Our Own CSS Frameworks part-5 (Create cards )