$serviceManager = $this->getApplicationServiceLocator();
$serviceManager->setAllowOverride(true);
//Order Service
$realOrderService = $serviceManager->get('order');
$orderServiceMock = $this
->getMockBuilder(get_class($realOrderService))
->getMock();
$serviceManager->setService('order', $orderServiceMock);
//Order Service Entity
$realOrderServiceEntity = new \Application\Model\Transaction;
$orderServiceEntityMock = $this
->getMockBuilder(get_class($realOrderServiceEntity))
->getMock();
$orderServiceMock
->expects($this->any())
->method('getEntity')
->will($this->returnValue($orderServiceEntityMock));
$orderServiceMock
->expects($this->any())
->method('getIsPaid')
->will($this->returnValue(TRUE));
$this->dispatch('/test/create','POST', $data);
在上面的代码中,我只是获取与' order'相关的实际对象的类名。服务提供者。然后我创建了该对象的模拟。
然后我将服务设置为模拟的实例,以便当控制器引用'顺序'服务它获取模拟的实例。
接下来,我基于\ Application \ Model \ Transaction模型创建一个orderServiceEntityMock。
然后每当调用orderServiceMock getEntity时,我都会返回模拟对象。
然后每当调用orderServiceMock getIsPaid时,我想返回true。
控制器:
public function create($data)
{
//Create the order based on the information sent by the user
$this->order = $this->getServiceLocator()->get('order');
$this->order->setCard(new CreditCard($data['order']['billing']));
$this->order->setAddress(new Address($data['order']['billing']));
$this->order->setTotal($data['order']['amount']);
$this->order->processPayment();
if($this->order->getIsPaid())
{
每当我们到达最后一个条件语句时,它都会失败。 我做错了什么?