CakePHP 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.

To use the Test Suite Shell simply place testsuite.php in /vendors/shells.

Usage:
cake testsuite help
this message
cake testsuite run section test_type [case_type] test
- section – app, core or plugin_name
- test_type – case, group or all
- case_type – only with case, one of behaviors, components, controllers, helpers or models
- test – file without (test|group).php

Examples:
cake testsuite run core all – will run all core tests
cake testsuite run app group models – will exexute /app/tests/groups/models.group.php
cake testsuite run contents case model content – will execute /app/plugins/contents/tests/cases/models/content.test.php





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:

<?php
loadPluginModels('contents');

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() {
    unset($this->TestObject);
  }

  function testSlugBehaviour(){
    $data = ContentTestFixture::$slugTest;
    if($this->TestObject->save($data)){
      $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.

domain.com/test.php
testsuite-1

domain.com/test.php?plugin=content
testsuite-2

domain.com/test.php?case=cases%2Fmodels%2Fcontent.test.php&plugin=contents
testsuite-3





Bake with MAMP on OS X

14 06 2007

The following details are for CakePHP v1.2. They are intended to make baking easier on a Mac. For details on how to use bake I recommend cakebaker’s series on using the bake script.

Baking on Mac with MAMP can be a long winded, typo ridden experience. Check out the full command.

/Applications/MAMP/bin/php5/bin/php ~/eclipse/workspace/cake_1_2/cake/console/cake.php bake

Typing that even for a small site with 3 or 4 models, views and controllers is not fun. Trust me.

Read the rest of this entry »





CakePHP n00b Tip #1

14 06 2007

This marks the start of an ongoing series of tips for newcomers to Cake. It will be based on posts to the Cake PHP Google Group as well as things that I myself come across.

Action parameters from the Url

Passing parameters to actions via the url is simple. Urls are in the format http://site.com/controller/action/param1/param2 and so on. Your actions will be called with parameters in the order they are in the url .ie action($param1, $parm2)

This is a common scenario when using hasMany, for example Company hasMany Employees. On the company details view you want to add a “New Employee” link that automatically assigns the company_id.

Read the rest of this entry »





CakePHP Development on Mac OS X

11 06 2007

I’ve been setting up my Mac OS X development environment lately and I have got the following so far.

Server

For a server I am using the excellent MAMP package. Simple, straight forward and with the added bonus that you can switch between PHP4 and PHP5 at any time by selecting a radio box and restarting the server.

IDE

After reading a couple of posts on the CakePHP google groups I have decided to go with Eclipse as my IDE, which comes in a handy all in one package from Zend.

At work I use ZDE on a Win box. I am happy with ZDE – got to love its auto-complete feature – but I wanted to try something new and a little more flexible at home.

After less than an hour with Eclipse I am quite impressed. I have already created an “external tool” for Bake thanks to the short tutorial here, although I had to modify it for 1.2. Now I can bake with a short menu selection, directly in Eclipse rather than typing the whole thing into the terminal.

Another neat feature that I noticed is in the task view it automatically picks out comments with TODO in them and adds them to the list. Very handy for not forgetting what still needs a bit of attention.

Subversion

There is a nice binary of Subversion avaible from Martin Ott which makes SVN as simple as any package to install. Couple this with Subclipse and code versioning is sorted.

If you need to run a repository locally, the package from Martin Ott is capable of this by running from Terminal:
svnserve -d

Still to Come

I am still looking into a deployment process. I want something that will checkout my current SVN head, run the unit test, run the Selenium test and then FTP them to the remote server if all tests pass. I am looking into ANT and Capistrano, but am still undecided.





Powered By CakePHP Proposal

9 06 2007

I have written the proposal for a website to showcase other sites powered by CakePHP. Have a look at the Powered By CakePHP proposal and leave some feedback, guidence or wisdom to help me on my way with my first OSS project.

I have applied for a CakeForge repository and I’ll let you know when the initial release is available.

Now I’m of to do my exam and then home to set up a decent dev environment on my Intel Mac.





Why?

6 06 2007

Lately I have been getting more involved in the CakePHP community, in particular I have been hangin out in the CakePHP Google Group. As I get more involved I have realised that I need a bit more of a presence where I can impart my knowledge and improve my writing skills.

The final decider to start a blog was Aaron Thies’ post to the group about starting a CakePHP Powered site that showcases sites built on Cake. I think this is an excellent idea and I will be pushing it and hopefully we can get the Cake community, PHP community and web development community to all support it and use it as a point of reference.