Testing a cake – what’s missing?

In the last few days I discussed with several people about (unit) testing in CakePHP. The result is that testing with CakePHP is not as easy as it should be (not really a surprise if you ever tried to write tests *g*), and that some features are missing. So here my current todo list for the test suite:

  • Restructure tests folder
  • Command line support
  • Fixtures for model tests
  • Make it easier to test controllers (it is a bit spongy, I know)

Other ideas? Wishes? Please let me know :)


ssh_exchange_identification: Connection closed by remote host

Today, as I tried to access a server with SSH, I got the following error:

ssh_exchange_identification: Connection closed by remote host

After some searching I found a solution: I have had to add the following line to /etc/hosts.allow:


And voilĂ , it works.

How to use multiple AppModels?

CakePHP supports just one custom AppModel, which must be placed in the /app folder. If you place another AppModel in the app folder and you try to extend your model from that AppModel, you get a “Class not found” error. To avoid that error you simply have to add a require statement at the top of your model:

require(APP . 'my_special_app_model.php');

class MyModel extends MySpecialAppModel

The same approach works also for AppControllers.

Why are there two “vendors” folders in CakePHP?

You probably noticed that there are two “vendors” folders in CakePHP: one is directly in the root and the other in the “app” folder. So, why are there two such folders? It is simple. The “vendors” folder in the “app” folder is for application-specifc third-party libraries whereas the other “vendors” folder is for libraries you want to use in multiple applications.

Using Zend Framework Components in CakePHP

Yesterday, A/R/T has published my article I wrote some time ago about using Zend Framework components in CakePHP. In that article a very simple application is created which uses the Zend_Service_Flickr component to access the Flickr webservices.

An idea for sending emails with CakePHP

On the weekend, Felix Geisendörfer (aka the_undefined) presented an email solution for CakePHP based on the ActionMailer class from Ruby on Rails. It inspired me to think about this topic.

The result of this thinking is a proof-of-concept implementation of a mailer component which uses models (and some of Felix’ code). You can download it from CakeForge. Feedback is welcome :)

Here an example of how the approach works. As mentioned above, it uses models for storing the mail data. As a convention, such models must have the instance variables $from, $to, $subject, and $message. I overwrite the save function in this example to set the mail data, but you can also set the data directly, or in one or more custom functions.

// app/models/confirmation_mail.php
class ConfirmationMail extends AppModel
    var $useTable = false;
    var $from = 'me@mydomain.com';
    var $to = '';
    var $subject = 'Confirmation';
    var $message = '';
    function save($data)
        $this->to = $data[$this->name]['to'];
        $this->message = $data[$this->name]['username'] . ', thanks for subscribing.';
        return true;

The controller is simple: we “save” the data in our mail model, and send the email.

// app/controllers/example_controller.php
class ExampleController extends AppController
    var $uses = array('ConfirmationMail');
    var $components = array('Mailer');
    function test()
        $this->data['ConfirmationMail']['to'] = 'testuser@domain.com'; 
        $this->data['ConfirmationMail']['username'] = 'testuser'; 

So, what do you think about this approach?

Security fix for XSS vulnerability

Today, a new version ( of CakePHP has been released. It fixes a XSS (cross-site scripting) vulnerability in CakePHP, you find the details in ticket #1272. So it is strongly recommended to update. You find the release on CakeForge. This release contains also the SessionHelper I wrote about in the post “New core helper: SessionHelper”.

(PHP)Eclipse and phpDocumentor

In a comment Carriantor has pointed me to a tutorial about integrating phpDocumentor, a tool to generate API docs, into Eclipse. Thanks Carriantor!

How to apply a beforeSave to certain models only

This post is the answer to a question asked by Ian Hill in a comment to my previous post:

Is there a similar way to stop custom functions (especially beforeSave()) from being called in your model?

I hope I understood the question correctly and the following example answers the question ;-)

// app/app_model.php
class AppModel extends Model
    function beforeSave()
        if ($this->name != 'MyModel')
            // execute beforeSave logic
        return true;

How to apply a beforeFilter to certain actions only

If you define a beforeFilter in your controller, it will be executed before each action. Sometimes, that behaviour is not desired, and you want to exclude an action from the application of the beforeFilter. The following simple example gives you an idea of how to accomplish that:

// app/controllers/users_controller.php
class UsersController extends AppController
    function beforeFilter()
        if ($this->action != 'login')
            // execute beforeFilter logic

    function login()
        // do login

    function edit($id)
        // do edit

    function delete($id)
        // do delete