Showing posts with label PHPUnit. Show all posts
Showing posts with label PHPUnit. Show all posts

Monday, June 2, 2014

PHPUnit - Migrate from PEAR Install to PHAR (Windows & Linux)

Migrate PHPUnit from PEAR toPHAR


You have installed PHPUnit with PEAR, but the installation method is migrating and you keep getting the error message:

You have installed PHPUnit via PEAR. This installation method is no longer supported and http://pear.phpunit.de/ will be shut down no later than December, 31 2014.

Please read http://phpunit.de/manual/current/en/installation.html and learn how to use PHPUnit from a PHAR or install it via Composer. 


It lets you know that you haven't installed it the right way, but now how do you correct the problem and remove the annoying error message? 


First remove it (Windows & Linux):

pear uninstall phpunit/PHPUnit



Get and install PHPUnit using the phar, Linux (or Amazon EC2) version:

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit

#and in my case, the phpunit executable also needed to be placed here
cp /usr/local/bin/phpunit /usr/bin/phpunit

Get and Install PHPUnit using the phar, Windows version:

Download the .phar file to somewhere you can run it with php.
  1. Open this address in a browser and save to your disk: https://phar.phpunit.de/phpunit.phar 
  2. Open the command line and go to the directory (cd {savepath}) you have saved the file in. 
  3. Execute tests with: php phpunit.phar testfile.php  
  4. Copy phpunit.phar to overwrite your existing phpunit file (replace c:\php\phpunit with your installed location).  Back up the original phpunit file just in case.
    copy c:\php\phpunit c:\php\phpunit.bakcopy phpunit.phar c:\php\phpunit

NOTE:

If you are getting the error below when running your tests:
Failed opening required 'PHPUnit/Autoload.php'
You may need to remove or comment out the Autoload include line .  This file has already been included in the .phar PHP Archive.

//require_once 'PHPUnit/Autoload.php';



Now, use this testing framework to check your code and help you feel confident when deploying your applications.


There are a great number of resources on the official PHPUnit page that you should check out if you want to learn more about PHPUnit and how to use it to your advantage - PHPUnit Presentations.

Thursday, September 6, 2012

Installing PHPUnit with XAMPP


Install PHPUnit with XAMPP

Update:

This method is now deprecated per Sebastian Bergmann.  Use the phar or composer method to install PHPUnit.


XAMPP is a great utility to get you started quickly with Apache, MySQL, and PHP.  In fact I wasn't even able to get these programs working together properly on my new Windows 7 machine until I used XAMPP to install all the components I needed.  But do you how to setup PHPUnit with XAMPP?  To get PHPUnit working, the steps aren't quite as obvious.  As you will see the PEAR installation tool is used, but it needs some additional work as explained below.

1. First get and install XAMPP: http://www.apachefriends.org/en/xampp.html

Now after the install, usually you'll have your files under something like c:\xampp\php, etc.  This works pretty well, but to get PHPUnit, you need PEAR which needs some additional install.

2. Get the latest PEAR update and run the PEAR installer:
download the file at http://pear.php.net/go-pear.phar and place it in your C:\xampp\php folder
cd \xampp\php
php go-pear.phar
3. Change the location of your pear.ini (item#11 or 12 - the menu varies according to your system) to C:\xampp\php - This is especially important on Windows 7 that protects the windows folder. If this is not changed your PEAR install will not be able to save its configuration files.

4. Run the C:\xampp\php\PEAR_ENV.reg file created to import your PEAR environment variable settings.

5. Log out of Windows and back in - This will load the new environment variables into memory (otherwise the old location for pear.ini will be used).  Confirm your settings by running:
pear config-show
 It should now read: C:\xampp\php\pear.ini

6. Install PHPUnit & Skeleton Generator through PEAR
pear config-set auto_discover 1 
pear install pear.phpunit.de/PHPUnit
pear install phpunit/PHPUnit_SkeletonGenerator
7. Check your version of PHPUnit - It should be at least version 3.6.x

phpunit --version
You should also now have phpunit-skelgen.bat in the C:\xampp\php folder.

If this blog was helpful to you, please thank me by clicking on the google +1 link.  Every time you click +1 a unicorn gets its wings... and what's cooler than a flying unicorn?

Netbeans NOTE:
First of all, if you are trying to use Netbeans to do unit testing in PHP, then you should probably start at the great tutorial at: http://netbeans.org/kb/docs/php/phpunit.html#installing-phpunit

Update:
The PEAR install method is now at end of life and can no longer be used according to Sebastian Bergmann's github page.

Try this method for using the phar install method: Phar install method.