我开始为应用程序编写测试,并且我已经创建了一个包装器RedirectComponent来处理所有重定向。现在我开始测试我的应用程序(我对测试相当新)我在这里关注这个例子...... http://book.cakephp.org/2.0/en/development/testing.html#a-more-complex-example
现在一切都运行良好,除了我不能在$this->headers
参数上运行断言,因为它总是空白的。我发现如果我使用控制器重定向方法直接在控制器中重定向,一切正常,$this->headers
设置为:
array(
'Location' => 'http://localhost/inquiries/view/4'
)
然而,整个应用程序正在使用此RedirectComponent,我希望在编写使用此组件重定向的控制器测试时显示$this->headers
。
我很遗憾如何解决这个问题,因为看起来我做的一切都很正确而且应该有效。
以下是我的代码。
class InquiriesControllerTestCase extends ControllerTestCase {
/**
* Test Add Pass
*
* @return void
*/
public function testAddPass() {
$minimumInsertData = array(
'Inquiry'=>array(
'fname'=>'---Minimum Insert---',
'lname'=>'---Minimum Insert---',
'phone'=>'---Minimum Insert---',
'email'=>'email@email.com',
'subject'=>'---Minimum Insert---',
'inquiry'=>'---Minimum Insert---',
)
);
#create mock controller
$Inquiries = $this->generate('Inquiries', array(
'components'=>array(
'Email',
'Redirect',
'Session',
'Sms',
)
));
#expects one sms message
$Inquiries->Sms
->expects($this->once())
->method('send')
->will($this->returnValue(true));
#expects two emails
$Inquiries->Email
->expects($this->exactly(2))
->method('send')
->will($this->returnValue(true));
#expects flash success
$Inquiries->Redirect
->expects($this->once())
->method('flashSuccess');
#call add
$test = $this->testAction('/inquiries/add', array('data'=>$minimumInsertData, 'method'=>'post', 'return'=>'contents'));
debug($this->headers);
die;
}
}
class InquiriesController extends MastersController {
/*
* Name
*/
public $name = 'Inquiries';
/*
* Attached Components
*/
public $components = array(
'Captcha',
'Email',
'PersistentValidation',
'Sms',
);
/*
* Helpers
*/
public $helpers = array(
'AjaxChat',
);
/**
* Add
* @return void
*/
public function add(){
$this->viewPath = 'Inquiries';
$this->set("title_for_layout", 'Contact Us');
if(!empty($this->request->data)) {
$this->request->data['Inquiry']['status_id'] = $this->Inquiry->Status->id('unresolved', 'inquiry');
//$this->request->data['Inquiry']['user_id'] = $this->Auth->user('id');
//$this->request->data['Inquiry']['account_id'] = $this->Auth->user('account_id');
$this->request->data['Inquiry']['ip'] = env('REMOTE_ADDR');
if($this->Inquiry->save($this->request->data)) {
#send SMS
if($this->sendSms) {
$smsToAso = array(
'number'=>Configure::read('Inquiry.add.sms.number'),
'from'=>Configure::read('Inquiry.add.sms.from'),
'carrier'=>Configure::read('Inquiry.add.sms.carrier'),
'text'=>Configure::read('Inquiry.add.sms.text').' https://www.airsatone.com/admin/inquiries/view/'.$this->Inquiry->id,
);
$this->Sms->send($smsToAso);
}
#send email
if($this->sendEmail){
#send user an email
$this->Email->template = Configure::read('Inquiry.add.email.template');
$this->Email->to = $this->request->data['Inquiry']['email'];
$this->Email->from = Configure::read('Inquiry.add.email.from');
$this->Email->subject = Configure::read('Inquiry.add.email.subject');
$this->Email->message = Configure::read('Inquiry.add.email.message');
$this->Email->send();
#send email to ASO
$message = '<p>ASO Inquiry from '.$this->request->data['Inquiry']['fname'].' '.$this->request->data['Inquiry']['lname'];
if(!empty($this->request->data['Inquiry']['company'])) $message .= ' at '.$this->request->data['Inquiry']['company'].'</p>';
$message .= '<p>You may contact this individual by phone:'.$this->request->data['Inquiry']['phone'].' or email:'.$this->request->data['Inquiry']['email'].'</p>';
$message .= $this->request->data['Inquiry']['inquiry'];
$this->Email->template = Configure::read('Inquiry.add.email_aso.template');
$this->Email->to = Configure::read('Inquiry.add.email_aso.to');
$this->Email->from = Configure::read('Inquiry.add.email_aso.from');
$this->Email->subject = Configure::read('Inquiry.add.email_aso.subject');
$this->Email->message = $message;
$this->Email->send();
}
return $this->redirect(array('controller'=>'inquiries', 'action'=>'view', $this->Inquiry->id));
//return $this->Redirect->flashSuccess('Thanks for your inquiry, XXXXXX will be contacting you regarding your inquiry shortly.', array('controller'=>'inquiries', 'action'=>'view', $this->Inquiry->id));
} else {
return $this->redirect($this->referer());
//return $this->Redirect->flashWarning('There was a problem submitting your form. Please check to make sure the form was filled out correctly.', $this->referer());
}
}
}
}
class RedirectComponent extends Component {
/*
* Controller
*/
public $controller = null;
/*
* Components
*/
public $components = array('Session');
/*
* Settings
*/
public $settings = array();
/*
* Success
*/
public $success = null;
/*
* Warning
*/
public $warning = null;
/*
* Notice
*/
public $notice = null;
/*
* Config
*/
public $config = array();
/**
* Initialize
* @param $controller
*/
public function initialize(&$controller){
$this->controller = $controller;
$this->settings = array_merge(array('success'=>'success', 'warning'=>'warning', 'notice'=>'notice'), $this->config);
$this->success = $this->settings['success'];
$this->warning = $this->settings['warning'];
$this->notice = $this->settings['notice'];
}
/**
* Flash Success
*
* Redirects a user with a success flash message
* @param $msg
* @param $url
*/
public function flashSuccess($msg, $url=null){
$this->Session->setFlash($msg, $this->success);
if(!empty($url)){
return $this->controller->redirect($url, null, true);
}
}
/**
* Flash Warning
*
* Redirects a user with a warning flash message
* @param $msg
* @param $url
*/
public function flashWarning($msg, $url=null) {
$this->Session->setFlash($msg, $this->warning);
if(!empty($url)){
return $this->controller->redirect($url, null, true);
}
}
/**
* Flash Notice
*
* Redirects a user with a notice flash message
* @param $msg
* @param $url
*/
public function flashNotice($msg, $url=null){
$this->Session->setFlash($msg, $this->notice);
if(!empty($url)){
return $this->controller->redirect($url, null, true);
}
}
/**
* Determines if an id is empty & sends a flash arning if it is.
*
* @param $id
* @param $url
* @param $msg
*/
public function idEmpty($id=null, $url=null, $msg='Invalid Id'){
if (!$id && empty($this->controller->data)) {
$this->flashWarning($msg, $url);
}
}
}
答案 0 :(得分:1)
你的SessionComponent::flashSuccess()
方法永远不会被执行,因为你嘲笑它。因此,它无法操纵$this->header
。如果你想测试你的重定向是否正确,为什么不检查你的重定向函数是否使用正确的参数调用:
$Inquiries->Redirect
->expects($this->once())
->method('flashSuccess')
->with('/where/it/should/redirect');
// or whatever parameters it takes