我正在用symfony2進行一些PHPUnit測試。我遇到了一個特定測試的問題。PhpUnit測試存根方法多重返回
我正在測試一個響應表單,當然我的一個類的響應是真實的一個錯誤。我有一個模擬我的數據庫,我有一個從我的databaseRepository的方法之一存根。
問題是,在一個測試中,我做了一個存根到一個有效數組的方法,第二個測試我只想查詢是無效的null。
我的分貝模擬:
//Setting up mock of database repository class
$this->db = $this->getMockBuilder('DatabaseRepository')
->disableOriginalConstructor()
->getMock();
$this->db->expects($this->any())
->method('getRecord')
->will($this->returnValue(self::$registrationRecord));
$this->db->expects($this->any())
->method('getRecord')
->willReturn(null);
所以我想有兩個不同的預期,但這obviousley不行.....是有可能有一個存根方法有兩種不同的回報.. ?
測試1:
<?php
class UnsubscribeRegistrationTemplateTest extends \PHPUnit_Framework_TestCase
{
/**
* @var UnsubscribeRegistrationTemplate
*/
protected $object;
/**
* @var ValidationClass
*/
public $validate;
/**
* @var DatabaseRepository
*/
public $db;
//Database Record Mock
public static $registrationRecord = array
(
'rowid' => '96',
'unsubscription' => 'N',
'updated' => 'BB'
);
/**
*
*/
protected function setUp()
{
//Setting up mock of validation class
$this->validate = $this->getMockBuilder('ValidationClass')
->disableOriginalConstructor()
->getMock();
$this->validate->expects($this->any())
->method('validateInput')
->willReturn(true);
//Setting up mock of database repository class
$this->db = $this->getMockBuilder('DatabaseRepository')
->disableOriginalConstructor()
->getMock();
$this->db->expects($this->any())
->method('getRegistrationRecord')
->will($this->returnValue(self::$registrationRecord));
$this->db->expects($this->any())
->method('getRegistrationRecord')
->will($this->returnValue(null));
$this->db->expects($this->any())
->method('setPreRegistrationEnquiryUnsubscriptionEnabled')
->willReturn(true);
$this->object = $this->createUnsubscribeRegistrationTemplateInstance();
}
/**
* @return UnsubscribeRegistrationTemplate
*
*/
public function createUnsubscribeRegistrationTemplateInstance()
{
//initialize Unsubscribe Registration Template
return new UnsubscribeRegistrationTemplate
(
$this->validate,
$this->db
);
}
/**
* @param array $mapping
* @return Request
*/
public function createRequest(array $mapping)
{
$request = new Request();
foreach ($mapping as $k =>$v)
{
$request->query->set($k, $v);
}
return $request;
}
/**
*
*/
public function testUnsubscribeRegistrationTemplateValidResponse()
{
$request = $this->createRequest(array(
'registration_id' => '96',
'source_channel' => 'BB'
));
$response = new Response(
true,
'Unsubscription successful'
);
$this->assertEquals($response, $this->object->create($request));
}
/**
*
*/
public function testUnsubscribeRegistrationTemplateEmptyResponse()
{
$request = $this->createRequest(array(
'registration_id' => '96',
'source_channel' => 'BB'
));
$response = new Response(
false,
'Registration Record Not Found.'
);
$this->assertEquals($response, $this->object->create($request));
}
/**
*
*/
public function testIsAlreadyRegisteredValidResponse()
{
//Testing record is already unsubscribed
$registrationRecord = array(
'unsubscription_enabled' => 'Y'
);
$this->assertTrue($this->object->isAlreadyUnsubscribed($registrationRecord));
}
/**
*
*/
public function testIsAlreadyRegisteredInValidResponse()
{
//Testing record not unsubscribed
$registrationRecord = array(
'unsubscription_enabled' => 'N'
);
$this->assertFalse($this->object->isAlreadyUnsubscribed($registrationRecord));
}
/**
*
*/
protected function tearDown()
{
unset($this->object);
}
}
移動在相對測試方法的期望,因爲例如,如果'testIsAlreadyRegisteredInValidResponse'想要那個'getRecord '返回null,將代碼移到那裏,並從設置中移除其他期望。 – Matteo
是的我之前這樣做,但預期的功能被我的PhpStorm突出顯示,我雖然它不會工作,所以從來沒有連續嘗試:/現在我做了,它工作完美,但PHP風暴hihglights它說:方法期望未找到在類庫databaseRepository – Koper
不用擔心它,只是因爲phpstorm不會拒絕正確的對象類型,可能是由於錯誤或缺少PHPDoc註釋。所以你解決了? – Matteo