CakePHP 2.1 - Unit Testing Controllers $this->view and $this->contents equals null
Switching a over to cakephp 2.1 and just starting to learn unit
testing...
When I `ArticlesControllerTestCase::testView()`
and run
$this->testAction('articles/view/1');
debug($this->view);
debug($this->contents);
die;
both `$this->view and $this->contents` is equal to null
I've emptied the beforeFilter calls and beforeRender calls to try and
eliminate that...
I noticed that in the controller action, if I set `$this-
>render('view');` at the end of the method I get `$this->view` = 'what
it is suppose to be', but `$this->contents` = 'to the same thing and
does not contain the layout
any idea why this would be happening?
class ArticlesController extends MastersController {
/*
* Name
*/
public $name = 'Articles';
/*
* Publicly Accessible Methods
*/
public $allowed = array('view', 'index', 'category', 'news');
/*
* Default Search Params
*/
public $presetVars = array(
array('field' => 'title', 'type' => 'value'),
array('field' => 'category_id', 'type' => 'value'),
array('field' => 'status_id', 'type' => 'value'),
);
/*
* Admin Menu Options
*/
public $adminMenu = array('index'=>array('Category'));
/**
* Before Filter Callback
* (non-PHPdoc)
* @see controllers/MastersController::beforeFilter()
* @return void
*/
public function beforeFilter(){
parent::beforeFilter();
$categories = $this->Article->Category->find('class',
array('article',
'conditions'=>array('not'=>array('Category.name'=>'Content'))));
$this->set(compact('categories'));
}
/**
* View
* @param $id
* @see controllers/MastersController::_view()
* @return void
*/
public function view($id){
parent::_view($id);
$articleTitle = $this->Article->findField($id,'title');
$recentNews = $this->Article->find('recentNews');
$this->set('title_for_layout', $articleTitle);
$this->set(compact('recentNews'));
}
};
class MastersController extends AppController {
/*
* Name
*/
public $name = 'Masters'; # expected to be overridden
/**
* View
* Default view method for all controllers
* Provides an individual record based on the record id
* @param int $id: model id
* @return void
*/
protected function _view($id){
$this->Redirect->idEmpty($id, array('action'=>'index'));
${Inflector::singularize($this->request->params['controller'])}
= $this->{$this->modelClass}->find('record', array($id));
${Inflector::variable(Inflector::singularize($this->request-
>params['controller']))} = ${Inflector::singularize($this->request-
>params['controller'])};
if(empty(${Inflector::singularize($this->request-
>params['controller'])})){
return $this->Redirect->flashWarning('Invalid Id.', $this-
>referer());
}
$this->set(compact(Inflector::singularize($this->request-
>params['controller']),
Inflector::variable(Inflector::singularize($this->request-
>params['controller']))));
}
}
class ArticlesControllerTestCase extends ControllerTestCase {
/**
* Fixtures
*
* @var array
*/
public $fixtures = array('app.article');
/**
* Set Up
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->Articles = new TestArticlesController();
$this->Articles->constructClasses();
}
/**
* Tear Down
*
* @return void
*/
public function tearDown() {
unset($this->Articles);
parent::tearDown();
}
/**
* Test View
*
* @return void
*/
public function testView() {
$this->testAction('articles/view/1');
debug($this->view); die;
}
}
class ArticleFixture extends CakeTestFixture {
/**
* Fields
*
* @var array
*/
public $fields = array(
'id' => array('type' => 'integer', 'null' => false, 'default' =>
NULL, 'key' => 'primary'),
'slug' => array('type' => 'string', 'null' => true, 'default' =>
NULL, 'length' => 120, 'collate' => 'latin1_swedish_ci', 'charset' =>
'latin1'),
'title' => array('type' => 'string', 'null' => false, 'default'
=> NULL, 'length' => 100, 'collate' => 'latin1_swedish_ci', 'charset'
=> 'latin1'),
'body' => array('type' => 'text', 'null' => false, 'default' =>
NULL, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
'created' => array('type' => 'datetime', 'null' => false,
'default' => NULL),
'modified' => array('type' => 'datetime', 'null' => false,
'default' => NULL),
'status_id' => array('type' => 'integer', 'null' => false,
'default' => NULL),
'category_id' => array('type' => 'integer', 'null' => false,
'default' => NULL),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique'
=> 1)),
'tableParameters' => array('charset' => 'utf8', 'collate' =>
'utf8_general_ci', 'engine' => 'InnoDB')
);
/**
* Records
*
* @var array
*/
public $records = array(
array(
'id' => '1',
'slug' => NULL,
'title' => 'Test Article #1 without slug - published',
'body' => '<p>Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur.</p><p>Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est
laborum.</p>',
'created' => '2011-02-15 21:14:03',
'modified' => '2011-02-15 21:14:03',
'status_id' => '1',
'category_id' => '5'
),
);
}
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home