CakePHP控制器测试:模拟Auth组件 形势
控制器代码CakePHP控制器测试:模拟Auth组件 形势,cakephp,testing,Cakephp,Testing,控制器代码 <?php App::uses('AppController', 'Controller'); class PostsController extends AppController { public function isAuthorized() { return true; } public function edit($id = null) { $this->autoRender = false;
<?php
App::uses('AppController', 'Controller');
class PostsController extends AppController {
public function isAuthorized() {
return true;
}
public function edit($id = null) {
$this->autoRender = false;
if (!$this->Post->exists($id)) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->Post->find('first', array(
'conditions' => array(
'Post.id' => $id,
'Post.user_id' => $this->Auth->user('id')
)
))) {
echo 'Username: ' . $this->Auth->user('username') . '<br>';
echo 'Created: ' . $this->Auth->user('created') . '<br>';
echo 'Modified: ' . $this->Auth->user('modified') . '<br>';
echo 'All:';
pr($this->Auth->user());
echo 'Modified: ' . $this->Auth->user('modified') . '<br>';
} else {
echo 'Unauthorized.';
}
}
}
<?php
App::uses('PostsController', 'Controller');
class PostsControllerTest extends ControllerTestCase {
public $fixtures = array(
'app.post',
'app.user'
);
public function testEdit() {
$this->Controller = $this->generate('Posts', array(
'components' => array(
'Auth' => array('user')
)
));
$this->Controller->Auth->staticExpects($this->at(0))->method('user')->with('id')->will($this->returnValue(1));
$this->Controller->Auth->staticExpects($this->at(1))->method('user')->with('username')->will($this->returnValue('admin'));
$this->Controller->Auth->staticExpects($this->at(2))->method('user')->with('created')->will($this->returnValue('2013-05-08 00:00:00'));
$this->Controller->Auth->staticExpects($this->at(3))->method('user')->with('modified')->will($this->returnValue('2013-05-08 00:00:00'));
$this->Controller->Auth->staticExpects($this->at(4))->method('user')->will($this->returnValue(array(
'id' => 1,
'username' => 'admin',
'created' => '2013-05-08 00:00:00',
'modified' => '2013-05-08 00:00:00'
)));
$this->testAction('/posts/edit/1', array('method' => 'get'));
}
}
测试代码
<?php
App::uses('AppController', 'Controller');
class PostsController extends AppController {
public function isAuthorized() {
return true;
}
public function edit($id = null) {
$this->autoRender = false;
if (!$this->Post->exists($id)) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->Post->find('first', array(
'conditions' => array(
'Post.id' => $id,
'Post.user_id' => $this->Auth->user('id')
)
))) {
echo 'Username: ' . $this->Auth->user('username') . '<br>';
echo 'Created: ' . $this->Auth->user('created') . '<br>';
echo 'Modified: ' . $this->Auth->user('modified') . '<br>';
echo 'All:';
pr($this->Auth->user());
echo 'Modified: ' . $this->Auth->user('modified') . '<br>';
} else {
echo 'Unauthorized.';
}
}
}
<?php
App::uses('PostsController', 'Controller');
class PostsControllerTest extends ControllerTestCase {
public $fixtures = array(
'app.post',
'app.user'
);
public function testEdit() {
$this->Controller = $this->generate('Posts', array(
'components' => array(
'Auth' => array('user')
)
));
$this->Controller->Auth->staticExpects($this->at(0))->method('user')->with('id')->will($this->returnValue(1));
$this->Controller->Auth->staticExpects($this->at(1))->method('user')->with('username')->will($this->returnValue('admin'));
$this->Controller->Auth->staticExpects($this->at(2))->method('user')->with('created')->will($this->returnValue('2013-05-08 00:00:00'));
$this->Controller->Auth->staticExpects($this->at(3))->method('user')->with('modified')->will($this->returnValue('2013-05-08 00:00:00'));
$this->Controller->Auth->staticExpects($this->at(4))->method('user')->will($this->returnValue(array(
'id' => 1,
'username' => 'admin',
'created' => '2013-05-08 00:00:00',
'modified' => '2013-05-08 00:00:00'
)));
$this->testAction('/posts/edit/1', array('method' => 'get'));
}
}
问题
这里实际上有三个问题:
echo'Email:'$此->身份验证->用户('email')。'
'
(仅举个例子)在“Username”和“Created”的回显之间,测试将失败,并出现以下错误:在序列索引2调用时,方法名称的预期失败等于。这是有意义的,因为(1)
处的$This->不再正确
我怎样才能模拟Auth组件,使其(1)不重复,(2)使测试输出与浏览器相同的内容,(3)允许我在任何地方添加
$this->Auth->user('foo')
代码而不破坏测试?在回答这个问题之前,我必须承认我没有使用CakePHP框架的经验。但是,我在PHPUnit与Symfony框架的合作方面有相当多的经验,也遇到过类似的问题。针对您的观点:
…->staticExpects($this->at(5))…
语句来覆盖对Auth->user()的第6次调用。这些语句不定义使用指定值调用Auth->user()时要返回的值。它们定义,例如,对Auth对象的第二次调用必须是对参数为“username”的方法user(),在这种情况下,将返回“admin”。但是,如果您在下一点中遵循该方法,这应该不再是一个问题public function testEdit() {
$this->Controller = $this->generate('Posts', array(
'components' => array(
'Auth' => array('user')
)
));
$this->Controller->Auth
->staticExpects($this->any())
->method('user')
->will($this->returnValue(array(
'id' => 1,
'username' => 'admin',
'created' => '2013-05-08 00:00:00',
'modified' => '2013-05-08 00:00:00',
'email' => 'me@me.com',
)));
$this->testAction('/posts/edit/1', array('method' => 'get'));
}
$this->autoRender = false;
if (!$this->Post->exists($id)) {
throw new NotFoundException(__('Invalid post'));
}
$user = $this->Auth->user();
if ($this->Post->find('first', array(
'conditions' => array(
'Post.id' => $id,
'Post.user_id' => Hash::get($user, 'id')
)
))) {
echo 'Username: ' . Hash::get($user, 'username') . '<br>';
echo 'Created: ' . Hash::get($user, 'created') . '<br>';
echo 'Modified: ' . Hash::get($user, 'modified') . '<br>';
echo 'All:';
pr($user);
echo 'Modified: ' . Hash::get($user, 'modified') . '<br>';
} else {
echo 'Unauthorized.';
}
$this->autoRender=false;
如果(!$this->Post->exists($id)){
抛出新的NotFoundException(uuu('Invalid post'));
}
$user=$this->Auth->user();
如果($this->Post->find('first',数组(
“条件”=>数组(
'Post.id'=>$id,
'Post.user_id'=>Hash::get($user,'id'))
)
))) {
echo“Username:”.Hash::get($user,“Username”)。
;
echo“Created:”.Hash::get($user,“Created”)。
;
echo'Modified:'.Hash::get($user'Modified')。
;
呼应“全部:”;
公共关系(用户);
echo'Modified:'.Hash::get($user'Modified')。
;
}否则{
回声“未经授权”;
}
据我所知,Hash::get($record,$key)是从记录中检索属性的正确CakePHP方法,尽管使用这里提供的简单属性,我认为用户[$key]也可以工作