Tanveer’s Weblog

[CakePHP] Unit Testing

July 10, 2008 · 2 Comments

What a fun !! :D
Today i test one of my Country Model using cakephp component. That why i write down this notes to make sure that i can use several times.

1. Download Simple Test unit testing suite.
2. Uncompress it in my app/vendors directory.
3. Change DEBUG level of 1 in my app/config/core.php file
4. Create a new database connection in app/config/database.php for only testing purpose like;

var $test = array(
‘driver’ => ‘mysql’,
‘persistent’ => true,
‘host’ => ‘localhost’,
‘login’ => ‘root’,
‘password’ => ”,
‘database’ => ‘db_name’,
‘prefix’ => ”,
‘encoding’ => ‘utf8′
);

5. Fixture : Create a file named country_test_fixture.php in your app/tests/fixtures directory, with the following content

<?php
class CountryTestFixture extends CakeTestFixture {

var $name = ‘CountryTest’;

var $import = array(‘model’ => ‘Country’, ‘records’ => true , ‘connection’ => ‘test’);

}
?>

6. Test Case : Create a file named country.test.php in your app/tests/cases/models directory, with the following contents:

<?php

loadModel(‘Country’);

class CountryTest extends Country {
var $name = ‘Country’;
//var $useDbConfig = ‘test_suite’;
}

class CountryTestCase extends CakeTestCase {

var $fixtures = array( ‘Country_test’ );

function testFindAll() {

$this->CountryTest =& new CountryTest();

$result = $this->CountryTest->findAll(“Country.country_id=1″);

$expected = array(

array (
‘Country’ => array( ‘country_id’ => 1, ‘region_id’ => 2 , ‘country’ => ‘Bangladesh’ ),

)

);

$this->assertEqual($result, $expected);

}

}
?>

7. Running My Test Case: Point my browser to http://tanveer-noman/myProject/test.php. Click on App Test Cases and find the link to your models/country.test.php. Click on that link.
Waoow!! I got a nice green screen saying that your test succeded.
;)

To know more

cake bake

Categories: Fun · Tips' n Tricks' · cakePHP
Tagged:

2 responses so far ↓

Leave a Comment