I have been looking for a tutorial to help me integrate PHPUnit with CakePHP. Looking to use Selenium tests too so prefer PHPUnit.
I have been trying to follow the tutorial on http://cakebaker.42dh.com/2006/03/22/selenium/ but cant seem to get it work. Any good tutorials out there?
Thanks!
It's relatively easy. I use cake 1.3 from composer installation. This is how my composer.json looks like:
{
"config": {
"vendor-dir": "vendors/composer"
},
"require": {
"phpunit/phpunit": "3.7.*",
"cakephp/cakephp-1.3": "1.3",
},
"repositories": [
{
"type": "package",
"package": {
"name": "cakephp/cakephp-1.3",
"version": "1.3",
"source": {
"url": "https://github.com/cakephp/cakephp.git",
"type": "git",
"reference": "1.3"
}
}
}
]
}
Then the phpunit bootstrap.php file in tests directory:
<?php
include('../vendors/composer/autoload.php');
include('../webroot/index.php');
This is phpunit.xml form the same dir:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="bootstrap.php"
backupStaticAttributes="false"
cacheTokens="false"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
mapTestClassNameToCoveredClassName="false"
printerClass="PHPUnit_TextUI_ResultPrinter"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
strict="false"
verbose="false"
>
<testsuites>
<testsuite name="AllTests">
<directory>.</directory>
</testsuite>
</testsuites>
<filter>
<blacklist>
<directory suffix=".php"></directory>
<file></file>
<exclude>
<directory suffix=".php"></directory>
<file></file>
</exclude>
</blacklist>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php"></directory>
<file></file>
<exclude>
<directory suffix=".php"></directory>
<file></file>
</exclude>
</whitelist>
</filter>
</phpunit>
Don't forget to load your application classes in test setup. You can do it cakephp way. For example if your controller is named calendar your calendarTest.php may look like:
<?php
/**
* Class ComponentsCommonTest
* @property calendarController $calendarController
*/
class CalendarTest extends PHPUnit_Framework_TestCase
{
/**
* @var calendarController $calendarController
*/
private $calendarController;
function setUp()
{
App::import('Core', array('View', 'Controller', 'Model', 'Router'));
App::import('Controller', 'Calendar');
$this->calendarController =& new CalendarController();
$this->calendarController->constructClasses();
$this->calendarController->layout = null;
}
}
The same for models, vendor classes and so on. Works great for me.
Unfortunately CakePHP isn't designed to work together with PHPUnit. CakePHP has switched to using SimpleTest and you'll have one of two choices, refactor your tests to work with SimpleTest or modify the core to use PHPUnit.
However it should be stated that Mark Story has stated that CakePHP 2.0 will use PHPUnit for it's testing framework, so if you can aford to wait till then that may wind up being the best option.
CakePHP 1.3 Book on Testing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With