How to Install PHPUnit Into XAMPP

by Woei Yu, May 21, 2015

Any developer worth his/her salt knows how important unit testing is. And when you are using with PHP, PHPUnit is easily the most popular Unit Testing framework.

Sometimes though PHPUnit can be hard to install on XAMPP so, in this article, I am going to cover step by step instructions on how I set up PHPUnit on my local Windows 7 machine.

Setting Up Your Environment

There are 3 steps you need to have done before installing PHPUnit.

1: Install XAMPP

First, obviously, you need to get and install XAMPP; if you already have it then skip to the next step.

XAMPP is a simple, lightweight Apache distribution that makes it extremely easy for developers to create a local web server for development and testing purposes. Everything you need to set up a Web server (Apache), database (MySQL), and programming language (PHP) – is included in a simple extractable file. XAMPP is also cross-platform, which means it works equally well on Linux, Mac and Windows.

I won’t go through how to install XAMPP because it is not the focus of this article and there are many sites to get help with installing XAMPP.

After XAMPP is installed, you’ll usually have your files in a directory like c:\xampp.

2: Install Composer

The next step is to download and install Composer; if you’ve already done that then skip to the next step.

Composer is a dependency manager that allows you to install all the things needed to run PHPUnit. Once you’ve downloaded the file, click on the install package.

Step 1: Starting Setup

Step 1: Starting Setup

Step 2: Accept the defaults

Step 2: Accept the defaults

Step 3: Navigate to the php.exe file

Step 3: Navigate to the php.exe file

On my machine, XAMPP is installed in c:\xampp, so my php.exe is located in c:\xampp\php\php.exe.

During the installation, I had this error message:

“The openssl extension is missing, which will reduce the security and stability of Composer. If possible you should enable it or recompile php with –with-openssl”

To fix that, go to php.ini file located within the PHP folder. In my particular situation, it is located in c:\xampp\php\php.ini. Search for “extension=php_openssl.dll”.

Uncomment the line by removing the semi-colon before it, save the file, and then continue the installation.

3: Activate XDebug

XAMPP comes pre-packaged with XDebug on all versions after 1.7; to activate XDebug, we need to edit php.ini file.

By default, all the XDebug parameters are commented out.

Just remove the semi-colon before all the lines to activate XDebug and save the php.ini file.

In order to put our new configuration to use, we’ll need to restart Apache.

To do that, open your XAMPP Control Panel, and click on “Stop” button.

To Stop the XAMPP Server

To Stop the XAMPP Server

Then click on the “Start” button to restart the server.

To Start the XAMPP Server

To Start the XAMPP Server

Installing PHPUnit

Now that everything is setup it’s time to install PHPUnit.

All web files typically sit inside the “htdocs” directory located within the XAMPP directory. So, on my machine, my web files should be placed in c:\xampp\htdocs.

To install PHPUnit using Composer we need the file composer.json which you can download here. Open the file and, you see in line 3, I defined phpunit to be required. Once the install is run, composer will download the requested files and place them in the “vendor” directory.

To install PHPUnit, we fire up the Shell program from XAMPP Control Panel.

Open the Shell Program in XAMPP

Open the Shell Program in XAMPP

I have placed the composer.json file within the “UnitTest” folder and I need to navigate to that directory where my tests will be by typing in “chdir htdocs\UnitTest”.

Navigate to Directory in Shell Program

Navigate to Directory in Shell Program

Now I’m in the directory of UnitTest folder, I can run “composer update”… and my Composer repository should update by reading the composer.json file and installing everything it needs to run PHPUnit and putting the necessary dependency files within the “vendor” directory.

Install PHPUnit in Shell Program

Install PHPUnit in Shell Program

Installed PHPUnit Files and Folders

Installed PHPUnit Files and Folders

Now that PHP Unit is installed we can start using it.

Using PHPUnit

I have created a php file, SimpleTest.php, that has 2 assertions. Assertions are the basis of unit testing in PHP and are basically methods that verify that a unit of code behaves as expected. In this case, I am returning a false and a true assertion.

So to test the file SimpleTest.php, just type in phpunit SimpleTest.php; note that PHPUnit will assume that all public methods in the class are tests and will automatically execute them.

Simple PHPUnit Test

Simple PHPUnit Test

And it returns the right test results with 2 assertions and 1 failure.

Test Result for a Simple PHPUnit Test

Test Result for a Simple PHPUnit Test

And if you want to do a unit test outside of your test folder, simple type in phpunit followed by relative path of your test file. In this case, I created a simple test named “OutsideSimpleTest” which I placed under “htdocs” folder. So the command will be “phpunit ../OutsideSimpleTest.php”

A Simple PHPUnit Test to a Specific File Path

A Simple PHPUnit Test to a Specific File Path

So you can see you are not constrained within the folder where you installed phpUnit.

But this can be rather tedious not to mention inefficient to do test one page at a time, which is why it is so the next section is a huge time saver. Running through all your tests in a batch.

Running Batch Files

First, we need to define our test suite configuration in the phpunit.xml file and place it in my PHPUnit folder. In this case, we specify that our “SimpleProject” folder will contain all the test files. Here is how to do that:

<directory>./SimpleProject/</directory> simple tells PHPUnit where your test will be located so you don’t have to manually tell it every time you run your test.

By default, anything in that folder that has the suffix “test” in the file name, will be run as a test. If you have a different naming convention, eg you want to name your test like Simple1.UnitTest.php, use this

To verify that, I have created 2 test files (Simple1Test.php and Simple2Test.php), both returning true assertions.

And …

Simple type in “phpunit”. It will read in the phpunit.xml and load in the appropriate test cases and run them.

Runing PHPUnit via phpunit.xml configuration file

Runing PHPUnit via phpunit.xml configuration file

The best thing about configuration files you have a range of settings you can define. If you want to test certain files instead of running all the files under a particular directory …

If you want to include or exclude files, you can invoke the blacklist and whitelist settings

The available settings are pretty noteworthy. See https://phpunit.de/manual/current/en/appendixes.configuration.html for more XML information.

Conclusion

To recap, to install PHPUnit into XAMPP…

  1. Make sure XAMPP is installed
  2. Make sure Composer is installed
  3. Make sure XDebug is activated
  4. Create the composer.json and install PHPUnit
  5. Create the PHPUnit.xml configuration file and the simple test case
  6. Run the PHPUnit on the simple test case to make sure PHPUnit is working

 

Download source file here

6 Comments


    • Michael West
      Replay Cancel Replay
    • November 22, 2015

    HI Woei Yu, I just wanted to thank you for your guide on how to get PHPUnit working on Windows, it was very helpful to a trainee developer just starting out. I did encounter one problem where the XAMMP console returned "composer is not recognized as an internal or external command" after running "composer install". This was solved by following this stackoverflow solution - "http://stackoverflow.com/questions/18688059/install-phpunit-with-composer". Everything is working as expected now, thanks again for your very easy to follow guide. Kind Regards, Michael West

      • Replay Cancel Replay
      • November 22, 2015

      Hi Michael. Nice to see someone getting some use out of my article :) Thanks for the link as well. Different computers behave differently and it is virtually impossible to map out all the possible errors that can occur. So your link definitely comes in handy. Cheers, Woei Yu

    • Guillermo Alberti
      Replay Cancel Replay
    • November 24, 2015

    Excellent introduction. I would add that SimpleTest.php is placed inside the folder UnitTest\SimpleProject

    • Avnish
      Replay Cancel Replay
    • February 3, 2016

    Thanks a lot Woei Yu. Well explained! It was really very helpful for me.

    • Mayank
      Replay Cancel Replay
    • June 3, 2016

    It's Working

  • Thanks for the article. :-) Any tips on adding Selenium support to it?

Leave a Reply

Your email address will not be published Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

*

Let me know what you think

RSS
Follow by Email
Facebook
Google+
http://woeiyu.com/software/php/installing-phpunit/">
Twitter
SHARE
LinkedIn

Recommendations

Enjoy this article? Please spread the word :)