Test your models

I am a fan of test driven development (TDD) and so it is my goal to use this approach with CakePHP. In this post I will show you how to unit test a model. See http://wiki.cakephp.org/tutorials:testing_cake_apps for the configuration of the testing infrastructure.

Let us write a model for a contact form, where the user must enter an email address and a text. This is our model for the moment:

<?php
    class Contactrequest extends AppModel
    {
    }
?>

What could go wrong with our model? Theoretically nothing, as we have not written anything. But in reality, we do not know if the framework works as expected, as there are almost no test cases for the framework available. So let us test if we can save our model.

<?php
class ContactrequestTest extends UnitTestCase
{
	var $model;
	var $validEmail = 'dho@xy.com';
	var $validComment = 'hello';
	
	function setUp()
	{
		$this->model =& new Contactrequest();
		$this->model->query('DELETE FROM contactrequests');
	}
	
	function testSave()
	{
		$this->model->data = array('Contactrequest' => array('email' => 
                                        $this->validEmail, 'comment' => $this->validComment));
		
		$this->assertTrue($this->model->save());
		$id = $this->model->getLastInsertId();
		$modelData = $this->model->find(array('id' => $id));
		$this->assertEqual($modelData['Contactrequest']['email'], 
                                               $this->validEmail);
		$this->assertEqual($modelData['Contactrequest']['comment'], 
                                               $this->validComment);
	}
}
?>

If we run this test case, we get a green bar. Cool! So we go on and write tests to ensure that contact requests with an invalid email address or an empty comment cause a validation error.

function testSaveWithEmptyComment()
{
    $this->model->data = array('Contactrequest' => array('email' => 
                                                    $this->validEmail, 'comment' => ''));
		
    $this->assertFalse($this->model->save());
    $this->assertFalse(isset($this->model->validationErrors['email']));
    $this->assertEqual($this->model->validationErrors['comment'], '1');
}
	
function testSaveWithInvalidEmail()
{
    $this->model->data = array('Contactrequest' => array('email' => 
                                                   'dho@xy', 'comment' => $this->validComment));
		
    $this->assertFalse($this->model->save());
    $this->assertFalse(isset($this->model->validationErrors['comment']));
    $this->assertEqual($this->model->validationErrors['email'], '1');
}

Ok, we can run our test case. We get, as expected, a red bar (because we have not defined any validation rules in our model). So we add the validation rules to our model:

<?php
    class Contactrequest extends AppModel
    {
        var $validate = array('email' => array(array(VALID_EMAIL)), 
                                            'comment' => array(array(VALID_NOT_EMPTY)));
    }
?>

(Notice: The definition of the validation rules is slightly different from the standard way as I use the validation approach I described in an earlier post)

And now we run our testcase again: a green bar! With that we have finished our first tested model.

One Trackback/Pingback

  1. […] cake baker » Test your models cake baker explains how to set up unit tests for CakePHP applications (tags: cakephp unittest) […]

Post a Comment

Required fields are marked *
*
*

%d bloggers like this: