在 phpunit 中传递变量
Passing variables in phpunit
我正在尝试将我在测试用例中创建的数组传递到我要测试的函数中。是否可以在测试用例中创建一个变量并将其传递或模拟到我要测试的class/function?
是否可以使用这样的东西:
$this->object = array(//array code here);
$this->testclass->attachVar->getAllObjects($this->objects);
这是我的代码:
class myClassTest extends \PHPUnit_Framework_TestCase {
protected function setUp(){
$this->testclass = new \stdClass();
$this->testclass = $this->getMockBuilder('library\ixPlanta\plantChange', $this->object)
->disableOriginalConstructor()
->getMock();
}
public function testGetAllObjects() {
$this->object = array(
'testdb' => array(
'testdb_michel' => array(
'dbname' => 'testdb',
'projectName' => 'testdb',
'projectID' => 'bd993d2b9478582f6d3b73cda00bd2a',
'mainProject' => 'test',
'search' => false,
'webgroup' => array(),
'locked' => false
)
)
);
$this->testclass->expects($this->once())
->method('GetAllObjects')
->with('testdb', false, "CHECKED")
->injectTo('object', $this->object)
->will();
$result = $this->testclass->getAllObjects('testdb', false, "CHECKED");
$this->assertTrue($result);
}
在函数 testGetAllObjects() 中,我创建了一个数组,我想将其传递给我想测试的函数
public function getAllObjects($company,$selected=false,$selectText='CHECKED'){
$objList = array();
$i = 0;
foreach($this->Objects[$company] as $key => $value){
$objList[$i] = array('value'=> $key,'name' => $value['projectName'], 'objectID' => $value['projectID']);
$objList[$i]['checked'] = '';
if($selected !== false && !is_array($selected) && $selected === $value['dbname']){
$objList[$i]['checked'] = $selectText;
}elseif($selected !== false && is_array($selected) && in_array($value['dbname'], $selected)){
$objList[$i]['checked'] = $selectText;
}
++$i;
}
return $objList;
}
我想传递给 getAllObjects 的变量是 $this->objects
我认为您误解了模拟对象。模拟对象的目的是为您 不想 测试的任何其他 class 创建一个虚拟对象。模拟一个方法意味着,防止另一个 class 调用它的实际逻辑。相反,它不会被执行,模拟只是 returns 无论你给它什么。
要测试您的实际 class,您只需实例化它并调用其方法:
class myClassTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->testclass = new MyClass();
}
public function testGetAllObjects()
{
$this->testclass->object = array(
'testdb' => array(
'testdb_michel' => array(
'dbname' => 'testdb',
'projectName' => 'testdb',
'projectID' => 'bd993d2b9478582f6d3b73cda00bd2a',
'mainProject' => 'test',
'search' => false,
'webgroup' => array(),
'locked' => false
)
)
);
$result = $this->testclass->getAllObjects('testdb', false, "CHECKED");
$this->assertTrue($result);
}
}
模拟示例:
假设您的 class 包含通过构造函数注入的 class Service
的其他对象:
class MyClass {
protected $service;
public function __construct(Service $service) {
$this->service = $service;
}
public function myMethod($argument) {
$return = $this->service->callService($argument);
return $return;
}
}
你的 Service
对象是这样的:
class Service{
public function callService($argument) {
if($argument === NULL) {
throw new \Exception("argument can't be NULL");
}
return true;
}
}
现在你可以用这个方法测试 MyClass
:
public function testMyClassMethod() {
$serviceMock = $this->getMockBuilder("Service")->getMock();
$serviceMock->expects($this->any())
->method("callService")
->will($this->returnValue(true));
$myClass = new MyClass($serviceMock);
$this->assertTrue($myClass->myMethod(NULL));
}
myMethod
仍将 return 为真,尽管如果 $argument
为 NULL
,Service
通常会抛出异常。但是因为我们模拟了这个方法,所以它永远不会被实际调用,模拟对象将 return 我们在 ->will($this->returnValue())
.
中提供的任何内容
我正在尝试将我在测试用例中创建的数组传递到我要测试的函数中。是否可以在测试用例中创建一个变量并将其传递或模拟到我要测试的class/function?
是否可以使用这样的东西:
$this->object = array(//array code here);
$this->testclass->attachVar->getAllObjects($this->objects);
这是我的代码:
class myClassTest extends \PHPUnit_Framework_TestCase {
protected function setUp(){
$this->testclass = new \stdClass();
$this->testclass = $this->getMockBuilder('library\ixPlanta\plantChange', $this->object)
->disableOriginalConstructor()
->getMock();
}
public function testGetAllObjects() {
$this->object = array(
'testdb' => array(
'testdb_michel' => array(
'dbname' => 'testdb',
'projectName' => 'testdb',
'projectID' => 'bd993d2b9478582f6d3b73cda00bd2a',
'mainProject' => 'test',
'search' => false,
'webgroup' => array(),
'locked' => false
)
)
);
$this->testclass->expects($this->once())
->method('GetAllObjects')
->with('testdb', false, "CHECKED")
->injectTo('object', $this->object)
->will();
$result = $this->testclass->getAllObjects('testdb', false, "CHECKED");
$this->assertTrue($result);
}
在函数 testGetAllObjects() 中,我创建了一个数组,我想将其传递给我想测试的函数
public function getAllObjects($company,$selected=false,$selectText='CHECKED'){
$objList = array();
$i = 0;
foreach($this->Objects[$company] as $key => $value){
$objList[$i] = array('value'=> $key,'name' => $value['projectName'], 'objectID' => $value['projectID']);
$objList[$i]['checked'] = '';
if($selected !== false && !is_array($selected) && $selected === $value['dbname']){
$objList[$i]['checked'] = $selectText;
}elseif($selected !== false && is_array($selected) && in_array($value['dbname'], $selected)){
$objList[$i]['checked'] = $selectText;
}
++$i;
}
return $objList;
}
我想传递给 getAllObjects 的变量是 $this->objects
我认为您误解了模拟对象。模拟对象的目的是为您 不想 测试的任何其他 class 创建一个虚拟对象。模拟一个方法意味着,防止另一个 class 调用它的实际逻辑。相反,它不会被执行,模拟只是 returns 无论你给它什么。
要测试您的实际 class,您只需实例化它并调用其方法:
class myClassTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->testclass = new MyClass();
}
public function testGetAllObjects()
{
$this->testclass->object = array(
'testdb' => array(
'testdb_michel' => array(
'dbname' => 'testdb',
'projectName' => 'testdb',
'projectID' => 'bd993d2b9478582f6d3b73cda00bd2a',
'mainProject' => 'test',
'search' => false,
'webgroup' => array(),
'locked' => false
)
)
);
$result = $this->testclass->getAllObjects('testdb', false, "CHECKED");
$this->assertTrue($result);
}
}
模拟示例:
假设您的 class 包含通过构造函数注入的 class Service
的其他对象:
class MyClass {
protected $service;
public function __construct(Service $service) {
$this->service = $service;
}
public function myMethod($argument) {
$return = $this->service->callService($argument);
return $return;
}
}
你的 Service
对象是这样的:
class Service{
public function callService($argument) {
if($argument === NULL) {
throw new \Exception("argument can't be NULL");
}
return true;
}
}
现在你可以用这个方法测试 MyClass
:
public function testMyClassMethod() {
$serviceMock = $this->getMockBuilder("Service")->getMock();
$serviceMock->expects($this->any())
->method("callService")
->will($this->returnValue(true));
$myClass = new MyClass($serviceMock);
$this->assertTrue($myClass->myMethod(NULL));
}
myMethod
仍将 return 为真,尽管如果 $argument
为 NULL
,Service
通常会抛出异常。但是因为我们模拟了这个方法,所以它永远不会被实际调用,模拟对象将 return 我们在 ->will($this->returnValue())
.