4.7.6.1 Creating a test case
Say you have a typical articles controller, with its corresponding model, and it looks like this:
<?php
class ArticlesController extends AppController {
var $name = 'Articles';
var $helpers = array('Ajax', 'Form', 'Html');
function index($short = null) {
if (!empty($this->data)) {
$this->Article->save($this->data);
}
if (!empty($short)) {
$result = $this->Article->findAll(null, array('id',
'title'));
} else {
$result = $this->Article->findAll();
}
if (isset($this->params['requested'])) {
return $result;
}
$this->set('title', 'Articles');
$this->set('articles', $result);
}
}
?> <?phpclass ArticlesController extends AppController {var $name = 'Articles';var $helpers = array('Ajax', 'Form', 'Html');function index($short = null) {if (!empty($this->data)) {$this->Article->save($this->data);}if (!empty($short)) {$result = $this->Article->findAll(null, array('id','title'));} else {$result = $this->Article->findAll();}if (isset($this->params['requested'])) {return $result;}$this->set('title', 'Articles');$this->set('articles', $result);}}?>
Create a file named articles_controller.test.php in your app/tests/cases/controllers directory and put the following inside:
<?php
class ArticlesControllerTest extends CakeTestCase {
function startCase() {
echo '<h1>Starting Test Case</h1>';
}
function endCase() {
echo '<h1>Ending Test Case</h1>';
}
function startTest($method) {
echo '<h3>Starting method ' . $method . '</h3>';
}
function endTest($method) {
echo '<hr />';
}
function testIndex() {
$result = $this->testAction('/articles/index');
debug($result);
}
function testIndexShort() {
$result = $this->testAction('/articles/index/short');
debug($result);
}
function testIndexShortGetRenderedHtml() {
$result = $this->testAction('/articles/index/short',
array('return' => 'render'));
debug(htmlentities($result));
}
function testIndexShortGetViewVars() {
$result = $this->testAction('/articles/index/short',
array('return' => 'vars'));
debug($result);
}
function testIndexFixturized() {
$result = $this->testAction('/articles/index/short',
array('fixturize' => true));
debug($result);
}
function testIndexPostFixturized() {
$data = array('Article' => array('user_id' => 1, 'published'
=> 1, 'slug'=>'new-article', 'title' => 'New Article', 'body' => 'New Body'));
$result = $this->testAction('/articles/index',
array('fixturize' => true, 'data' => $data, 'method' => 'post'));
debug($result);
}
}
?> <?phpclass ArticlesControllerTest extends CakeTestCase {function startCase() {echo '<h1>Starting Test Case</h1>';}function endCase() {echo '<h1>Ending Test Case</h1>';}function startTest($method) {echo '<h3>Starting method ' . $method . '</h3>';}function endTest($method) {echo '<hr />';}function testIndex() {$result = $this->testAction('/articles/index');debug($result);}function testIndexShort() {$result = $this->testAction('/articles/index/short');debug($result);}function testIndexShortGetRenderedHtml() {$result = $this->testAction('/articles/index/short',array('return' => 'render'));debug(htmlentities($result));}function testIndexShortGetViewVars() {$result = $this->testAction('/articles/index/short',array('return' => 'vars'));debug($result);}function testIndexFixturized() {$result = $this->testAction('/articles/index/short',array('fixturize' => true));debug($result);}function testIndexPostFixturized() {$data = array('Article' => array('user_id' => 1, 'published'=> 1, 'slug'=>'new-article', 'title' => 'New Article', 'body' => 'New Body'));$result = $this->testAction('/articles/index',array('fixturize' => true, 'data' => $data, 'method' => 'post'));debug($result);}}?>
