Skip to content

Testing

Mark edited this page Apr 1, 2014 · 27 revisions

Testing - best pratices

Testing Controllers

In general it is best to only use testAction() once per test. So my convention is to use "test" + "ActionName" + "method (if not default one)"

/**
 * testAdminAdd()
 *
 * @return void
 */
public function testAdminAdd() {
	$url = Router::url(array('admin' => true, 'controller' => 'users', 'action' => 'add'));
	$options = array(
		'return' => 'contents'
	);
	$result = $this->testAction($url, $options);
	$this->assertNotEmpty($result);
}

/**
 * testAdminAddPost()
 *
 * @return void
 */
public function testAdminAddPost() {
	$url = Router::url(array('admin' => true, 'controller' => 'users', 'action' => 'add'));
	$options = array(
		'method' => 'post',
		'return' => 'contents',
		'data' => array(
			'User' => array(
				'name' => 'test'
			)
		)
	);
	$result = $this->testAction($url, $options);
	$this->assertNull($result);

	$url = Router::url(array('admin' => true, 'controller' => 'users', 'action' => 'index'));
	$this->assertTextContains($url, $this->headers['Location']);
}

Note how I use dynamic URLs here to avoid routing changes affect all controller tests. So these tests effectively test the same URL as the frontend.

So basic tests should be made for

  • index (GET)
  • view (GET)
  • add (GET + POST)
  • edit (GET + POST)
  • delete (POST)

and their admin counterparts, if applicable.

Using controller tests also tests the views to the actions. Which is a nice side effect. Any warning/error popping up there will then also be visible right away.

Testing Shells

TODO

Testing Models

TODO

Testing Helpers

TODO

Clone this wiki locally