RSS
 
21. Jul. 2012

CakePHP and PHPUnit

21 Jul

Earlier I wrote about some PHPUnit tips around Cake2. At that time the old SimpleTest testing framework had just been replaced by PHPUnit. So everything around this new framework was fairly new – especially to be.

I manually installed/downloaded the test suite files since I didnt want to install any PEAR stuff – on windows this is pretty annoying anyway. So is there a quick way to always be up to date with PHPUnit? Yes, there is. Since it is now pretty stable I want to introduce the PHPUnit plugin, which I developed together with Hyra.

Installing PHPUnit – the easy way

It never was easier. All you need for it is the PHPUnit plugin. Just open your console in your app directory and type

Phpunit.Phpunit install

Select your destination (APP or ROOT vendors) and done. This will download and extract all the necessary files, and put them in your specified Vendor folder.

Make sure you got CakePlugin::loadAll() – or specifically CakePlugin::load(‘Phpunit’) in your bootstrap! Otherwise the plugin will not be available. You can now use PHPUnit through the CLI or your favourite browser right away.

See the documentation of the plugin for details. Also note the convenience tasks (‘packages’, ‘info’) to check for updates and update the plugin shell if necessary. It also displays the so far unused pear packages.

Autoload

If you have it installed in your ROOT vendors and get some include warnings while baking put this at the top of the VENDORS/PHPUnit/Autoload.php file:

set_include_path(get_include_path().PATH_SEPARATOR.dirname(dirname(__FILE__)));

This way the vendors folder itself is also an include path and those warnings will go away.

Further setup and usage

For this I can just refer you to the official documentation.

Appendix

In my case just now, info printed the following:

# PHPUnit 3.6.11
        OK (v3.6.11)
# File Iterator 1.3.1
        OK (v1.3.1)
# Text Template 1.1.1
        OK (v1.1.1)
# PHP CodeCoverage 1.1.3
        OK (v1.1.3)
# PHP Timer 1.0.2
        OK (v1.0.2)
# PHPUnit MockObject 1.1.1
        OK (v1.1.1)
# PHP TokenStream 1.1.3
        OK (v1.1.3)
# DbUnit 1.1.2
        OK (v1.1.2)
# PHPUnit Story 1.0.0
        OK (v1.0.0)
# PHPUnit Selenium 1.2.7
        OK (v1.2.7)
# PHPUnit TicketListener GitHub 1.0.0
        OK (v1.0.0)
---------------------------------------------------------------
Unused pear packages:
# FinderFacade (v1.0.1)
# Object_Freezer (v1.0.0)
# PHPUnit_SkeletonGenerator (v1.1.0)
# PHPUnit_TestListener_DBUS (v1.0.0)
# PHPUnit_TestListener_XHProf (v1.0.0)
# PHPUnit_TicketListener_Fogbugz (v1.0.0)
# PHPUnit_TicketListener_GoogleCode (v1.0.0)
# PHPUnit_TicketListener_Trac (v1.0.0)
# PHP_CodeBrowser (v1.0.2)
# PHP_Invoker (v1.1.0)
# bytekit (v1.1.2)
# hphpa (v1.2.2)
# phpcov (v1.0.0)
# phpcpd (v1.3.5)
# phpdcd (v0.9.3)
# phploc (v1.6.4)
# ppw (v1.0.4)
# test_helpers (v1.1.0)
CakePHP and PHPUnit
0 votes, 0.00 avg. rating (0% score)
 
3 Comments

Posted by Mark in CakePHP

 

Tags: ,

Leave a Reply

Tip:
If you need to post a piece of code use {code type=php}...{/code}.
Allowed types are "php", "mysql", "html", "js", "css".

Please do not escape your post (leave all ", <, > and & as they are!). If you have encoded characters and need to reverse ("decode") it, you can do that here!
 

 
  1. warsame

    December 25, 2012 at 00:03

    I still cannot get this to work properly , i have followed the entire process with my vendor's folder populated yet i ge ta fata error saying cake cannot find this path
    PHPUnit/Framework/MockObject/Autoload.php

     
  2. Mark

    December 25, 2012 at 00:07

    Did you also put above snippet in your VENDORS/PHPUnit/Autoload.php?

     
  3. warsame

    December 25, 2012 at 02:59

    I have added this line to the top of my Autoload.php file, i tried to install in both root cakephp installation folder as well as my solution's vendor folder.

    set_include_path(get_include_path().PATH_SEPARATOR.dirname(dirname(__FILE__)));

    The fatal error might be due to this file not being found
    Warning (2): require_once(PHPUnit/Framework/MockObject/Autoload.php)

    I am using XAMMP and the include path points to my xamppfiles/ directory with php unit installed there