Installation and example of phpUnit in Sublime Text

Content

  • What is PHPUnit used for?
  • Installing PHPUnit on Windows
    • 1. Create containing folder
    • 2.Edit PATH environment variable
    • 3. Add new path to the PATH variable
    • 4. Add php installation path to PATH
    • 5. Download and install PHPUnit
  • Using phpunit in sublime text
  • How to use PHPUnit
    • 1. Creating test class Animal.php
    • 2. Creation of the Test class
    • 3. Run the created test
  • Extra: Conventions and Extended Information

What is PHPUnit used for?

PHPUnit is a framework for programming code tests. This means that we will be able to test our scripts and detect errors that we have missed.

Imagine that we have a php Class that has a toString() function. We could do some test code with PHPUnit to test if the toString() returns the correct information. Don’t you find it useful? Although you may think that for small developments it may be unnecessary, PHPUnit becomes vital when working on large applications where a testing phase is usually carried out before giving the OK to the application.

Installing PHPUnit on Windows

Before we can launch ourselves to explore the possibilities of PHPUnit and see an example, we must focus on the installation for its correct operation.

1. Create containing folder

In order to make PHPUnit work, we need to add to the PATH environment variable the path to a file with everything required by PHPUnit. Therefore, the first thing we will do is create a new directory where we will save the files. For example C:\bin

2.Edit PATH environment variable

Secondly we will have to add the above folder to the system environment PATH variable. We search the system for the configuration of environment variables, we can do it in two ways in windows:

2.1.1 OPTION 1: Windows 8 or 10 environment variables

In windows 8 or 10 we can click on the start button, and write directly “environment variables”. An option will appear that will say “Edit system environment variables”.

2.1.2 OPTION 2: Environment variables from Computer. Windows 7, 8 or 10

In Windows 7, 8 or 10 we right-click on Computer and access Properties, in the window that opens we click on Advanced system settings and click on the option “Environment Variables…”

2.2 Editing the PATH environment variable

Once in the Environment Variables menu we will look at the System Variables options box, look for the “Path” option by scrolling and click on edit.

3. Add new path to the PATH variable

Now we can see all the file paths accessible by the Path variable, click on New and write the directory that we created previously: “C:\bin”.

4. Add php installation path to PATH

Before finishing editing the Path variable we will add the path of the php installation. For Wamp normally C:\wamp\bin\php\php5.6.15 and in xampp C:\xampp\php

5. Download and install PHPUnit

We already have added the necessary routes for the execution of the command php in CMD, and list the “bin” folder to add the PHPUnit file to. Before continuing, I recommend you restart the PC so that the changes are correctly operative. Next we must download and finish installing the PHPUnit library:

  1. Download the PHPUnit.phar file from https://phar.phpunit.de/phpunit.phar and insert it into the C:\bin folder created previously.
  2. We change the name of the file to phpunit.phar
  3. We open the Windows command line (CMD) and type:
    1. cd C:\bin
    2. echo @php “%~dp0phpunit.phar” %* > phpunit.cmd
    3. exit
  4. We prove that everything has gone accordingly by opening the window again CMD and typing phpunit –version. If a message appears with the version of PHPUnit installed, everything is correct, otherwise try to restart and rewrite the phpunit –version command.

Using phpunit in sublime text

The use of the PHPUnit framework in Sublime Text requires the installation of the PHPUnit package through packageControl.

If you do not have packageControl installed in your Sublime Text installation, follow these steps:

  1. Download package Control from the following link: Package Control.sublime-package
  2. Click on Preferences > Browse Packages
  3. In the User folder add the downloaded file
  4. Restart sublime text and you’re done.

Once packageControl is installed, we are going to see how to add the PHPUnit package:

  1. We go to Sublime Text preferences and give package control.
  2. In the package control options we select Install Package.
  3. We write PHPUnit and select the first package that appears. It will be installed in a few moments, we close Sublime Text for the changes to take place and when we open it again it will be ready for use. We can verify this by right-clicking on the programming area and observing that different options related to the framework appear.

How to use PHPUnit

Using PHPUnit is easy, the first thing we have to do is in our work folder, create a file .xml called phpunit.xml with the following content:

This file is used to tell PHPUnit where we will have our code to execute the tests. Where it says directory we will have to put the test directory: ./test/ it means that I will save the code files for the tests in that folder, in the root directory of my project.

Now we are going to see how to do our first Test with PHPUnit:

1. Creating test class Animal.php

We create a file called animal.php in the working root folder. And inside we create the class Animal:

<?php
class Animal{
   private $subgrupo;
   private $especie;
   private $alimentacion;
   private $edad;
   public function __construct($subgrupo, $especie, $alimentacion, $edad){
      $this->subgrupo=$subgrupo;
      $this->especie=$especie;
      $this->alimentacion=$alimentacion;
      $this->edad=$edad;
   }
   public function sonido(){
      return 'Muu';
   }
}
?>

2. Creation of the Test class

Now we go to the folder Test, and create a file there called AnimalTest.php. Inside the file we write the code of a class with our first test:

<?php
require_once('Animal.php');
class AnimalTest extends PHPUnit_Framework_TestCase
{
     public function testCanInstatiate(){
        $animal = new Animal('', '', '', 11);
        $this->assertEquals($animal->sonido(), 'Muuu');
     }
}
?>

The code of this class creates a class object Animal and using the method of PHPUnit inherited assertEquals to compare the result of the method sound() with a test text.

3. Run the created test

To test our test, we go to the file .xml and we right click, PHPUnit and Run Using This Xml File.

will be opened to us a window at the bottom of sublime text with the result of the test.

In the case of our test it tells us that it has made a test (Tests:1) and with 1 failure (Failures: 1). It also indicates us with the symbol “-“ what has been shown on the screen, and with the symbol “+” the expected text.

Extra: Conventions and Extended Information

PHPUnit has a couple of conventions that help us organize our code optimally:

  • The name of each Test Class must be the same as the name of the file to be tested, but with the word Test at the end.. In the AnimalTest example
  • Also the class name must be equal to the file name, like any other class, so this one is very simple. The Animal class is in a file named Animal.php and the AnimalTest class is in a file named AnimalTest.php

The example I have explained here is a small sample of what PHPUnit can do.. This framework has many more possibilities, if you want to see them for yourself and learn more, I recommend that you visit their website: PHPUnit Documentation

Leave a Reply