I am new to the Unit Testing portion of CakePHP. There is no better time to learn than now.
I have ready through all of the documentation and numerous tutorials on the web. I have run up against a snag during the completion of some of my more basic unit tests.
My Controller:
class TestsController extends AppController {
public $components = array('RequestHandler', 'Security');
public $helpers = array('Js');
public $uses = array('Tests');
public function beforeFilter() {
$this->layout = 'admin';
parent::beforeFilter();
}
/**
* Returns database values of all tests that have been created.
*
*/
public function index() {
if ($this->request->is('requested')) {
return $tests;
}
$this->paginate = array(
'fields' => array('id', 'name', 'email', 'access_token', 'access_token_begins',
'filesizelimit', 'related_files', 'access_token_expires'),
'limit' => 10,
'order' => array(
'created' => 'desc'
));
$this->set('tests', $this->paginate('Test'));
}
My Controller test looks like this:
class TestsControllerTest extends ControllerTestCase {
public $fixtures = array('app.test');
public function testIndex() {
$result = $this->testAction('tests/index');
debug($result);
}
}
The output of my test is:
PHPUNIT_FRAMEWORK_ERROR_NOTICE
Undefined variable: tests
Test case: testsControllerTest(testIndex)
The only viable solution to the error I am receiving would be that maybe the test controller needs authentication before it can actually run the testAction()
?
This is a very simple sample from the CakePHP Documentation guide, yet I receive and undefined variable error. Any help is greatly appreciated.
I have managed to answer my own question.
The $tests
variable is undefined because of the if statement within the controller:
public function index() {
if ($this->request->is('requested')) {
return $tests;
}
$this->paginate = array(
'fields' => array('id', 'name', 'email', 'access_token', 'access_token_begins',
'filesizelimit', 'related_files', 'access_token_expires'),
'limit' => 10,
'order' => array(
'created' => 'desc'
));
$this->set('tests', $this->paginate('Test'));
}
That statement does not get hit when running the application, but it does when running the test. In this given function there is no need for the IF statement and therefore my error of UNDEFINED VARIABLE is now resolved.