Testing CakePHP Plugins

19 06 2007

With the official Test Suite bundled with CakePHP, it is pretty easy to write unit tests for your models and controllers. The Testing Models with CakePHP Test Suite tutorial on the Bakery is a good introduction to the CakePHP Test Suite.

Now this is great until it comes time to test your plugins. It seems that the official test suite does not like plugins. So I patched my cake install so that I can run plugins test cases and groups that are logically stored in /app/plugins/plugin_name/tests.

Here are the files for those interested. Please leave bugs or issues in the comments for now.

My apologies to those who downloaded 0.1, as it did not contain app/webroot/test.php – instead I had incorrectly included app/webroot/index.php

Plugin tests are exactly the same as those described at Testing Models with CakePHP Test Suite except that the loadModel(‘ModelName’) is left out becomes loadPluginModels(‘plugin’). Here is an example test case for a plugin:


class ContentTest extends Content {
  var $name = 'ContentTest';
  var $useDbConfig = 'test_suite';

class ContentTestCase extends CakeTestCase {
  var $TestObject = null;
  var $fixtures = array( 'content_test' );

  function startTest() {
    $this->TestObject = new ContentTest();

  function endTest() {

  function testSlugBehaviour(){
    $data = ContentTestFixture::$slugTest;
      $data = $this->TestObject->findById($this->TestObject->getInsertID());
      $expected = 'slug-test';
      $this->assertEqual($data['ContentTest']['slug'], $expected);
    } else {
      $this->fail('Could not save the Test Data');

Currently my patch only supports the HtmlManager – web view – and not the TextManager. Also ‘All’ under Test Groups of a plugin will not run the plugin tests, it will run the App Test Cases. I will fix these issues in the next release. The patch supports All under Group Tests and combined with my Test Suite Shell it also supports cli testing

And finally here are some screeshots of Plugin Tests in action.







4 responses

24 06 2007
CakePHP Test Suite Shell « Another Cake Baker

[…] Test Suite Shell 24 06 2007 In addition to my changes to the core Test Suite to add support for plugins, I have now created a console shell to run test suites via the command line. testsuite.php 7.1 […]

9 07 2007

Updated to v0.1.1.

I had incorrectly included webroot/index.php instead of webroot/test.php. So those who downloaded v0.1 and wondered why it didn’t work, please have another go.

I will be more careful with my releases in the future 🙂

13 11 2007

ah, very cool, exactly what I was looking for.

14 11 2007

one quick note, I had to change line 92 in test_manager.php to be:

} elseif (!empty($manager->pluginTest)) {
$testCaseFileWithPath = APP . 'plugins' . DS . $manager->pluginTest . DS . 'tests' . DS . 'cases' . DS . $testCaseFile;
} else {

hopefully this gets merged into core soon.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: