Php 如何模拟应该接受数组的方法?
我不熟悉使用PHPUnit进行测试,我正在尝试测试一个名为Php 如何模拟应该接受数组的方法?,php,unit-testing,testing,phpunit,shopware,Php,Unit Testing,Testing,Phpunit,Shopware,我不熟悉使用PHPUnit进行测试,我正在尝试测试一个名为returnOnLogin()的方法,该方法接受参数Enlight\u Event\u EventArgs$args并返回true 下面是我要测试的方法: public function returnOnLogin(\Enlight_Event_EventArgs $args) { $controller = $args->get('subject'); $view = $controller->View();
returnOnLogin()
的方法,该方法接受参数Enlight\u Event\u EventArgs$args
并返回true
下面是我要测试的方法:
public function returnOnLogin(\Enlight_Event_EventArgs $args)
{
$controller = $args->get('subject');
$view = $controller->View();
$controller->redirect([
'controller' => 'verification'
]);
// $view->addTemplateDir(
// __DIR__ . '/Views'
// );
return true;
}
这是我的测试:
class MyFirstTestPluginTest extends TestCase
{
public function testReturnOnLogin()
{
$my_plugin = new MyFirstTestPlugin(true);
$expected = true;
//I tried following but it did not work
$this->assertEquals($expected, $my_plugin->returnOnLogin(//here is the problem it requires this array that I dont know));
}
}假设您的控制器类是
controller
,并且假设我们不关心在$controller
中调用view()
,这应该包括您要查找的内容:
class MyFirstTestPluginTest extends TestCase
{
public function testReturnOnLogin()
{
/**
* create a test double for the controller (adjust to your controller class)
*/
$controller = $this->createMock(Controller::class);
/**
* expect that a method redirect() is called with specific arguments
*/
$controller
->expects($this->once())
->method('redirect')
->with($this->identicalTo([
'controller' => 'verification'
]));
/**
* create a test double for the arguments passed to returnLogin()
*/
$args = $this->createMock(\Enlight_Event_EventArgs::class);
/**
* expect that a method subject() is invoked and return the controller from it
*/
$args
->expects($this->once())
->method('subject')
->willReturn($controller);
$plugin = new MyFirstTestPlugin(true);
$this->assertTrue($plugin->returnOnLogin($args));
}
}
这个测试是做什么的?
安排
此测试首先安排双重测试,以便与被测系统(您的插件)一起使用
第一个双重测试是您的控制器,我们将其设置为这样一种方式:使用与指定数组相同的参数调用一次方法redirect()
第二个test double是参数,我们将它设置为这样一种方式,即我们希望一个方法'subject()`被调用一次,并返回控制器
然后,我们只需创建一个MyFirstTestPlugin
实例,将true
传递给构造函数,即可设置被测系统
不幸的是,您没有与我们共享构造函数,我们不知道参数true
代表什么。如果它影响returnLogin()
的行为,那么我们显然需要添加更多的测试,以便在参数采用不同的值时断言该行为
表演
然后,该测试在被测系统上调用方法returnLogin()
,并通过其中一个测试
断言
最后,该测试断言方法returnLogin()
返回true
注意看一看
controller
,并且假设我们不关心在$controller
中调用view()
,这应该包括您要查找的内容:
class MyFirstTestPluginTest extends TestCase
{
public function testReturnOnLogin()
{
/**
* create a test double for the controller (adjust to your controller class)
*/
$controller = $this->createMock(Controller::class);
/**
* expect that a method redirect() is called with specific arguments
*/
$controller
->expects($this->once())
->method('redirect')
->with($this->identicalTo([
'controller' => 'verification'
]));
/**
* create a test double for the arguments passed to returnLogin()
*/
$args = $this->createMock(\Enlight_Event_EventArgs::class);
/**
* expect that a method subject() is invoked and return the controller from it
*/
$args
->expects($this->once())
->method('subject')
->willReturn($controller);
$plugin = new MyFirstTestPlugin(true);
$this->assertTrue($plugin->returnOnLogin($args));
}
}
这个测试是做什么的?
安排
此测试首先安排双重测试,以便与被测系统(您的插件)一起使用
第一个双重测试是您的控制器,我们将其设置为这样一种方式:使用与指定数组相同的参数调用一次方法redirect()
第二个test double是参数,我们将它设置为这样一种方式,即我们希望一个方法'subject()`被调用一次,并返回控制器
然后,我们只需创建一个MyFirstTestPlugin
实例,将true
传递给构造函数,即可设置被测系统
不幸的是,您没有与我们共享构造函数,我们不知道参数true
代表什么。如果它影响returnLogin()
的行为,那么我们显然需要添加更多的测试,以便在参数采用不同的值时断言该行为
表演
然后,该测试在被测系统上调用方法returnLogin()
,并通过其中一个测试
断言
最后,该测试断言方法returnLogin()
返回true
注意看一看
returnOnLogin
是在类中定义的。您只需要运行returnOnLogin
方法,并对结果调用assertTrue
。哪一位有问题?是的,returnOnLogin在MyFirstTestPlugin类中。我来看看谢谢你。我在传递参数时遇到问题。我编辑了我的代码,请看最后一行代码。也许从这里开始:?并提供更多关于您案例的信息。因为我假设returnOnLogin
是在类中定义的。您只需要运行returnOnLogin
方法,并对结果调用assertTrue
。哪一位有问题?是的,returnOnLogin在MyFirstTestPlugin类中。我来看看谢谢你。我在传递参数时遇到问题。我编辑了我的代码,看看最后一行代码。很高兴我能帮忙,也许你想看看Chris Hartjes的一些书,他们在不久前帮助我开始测试。很高兴我能帮忙,也许你想看看Chris Hartjes的一些书,他们在不久前帮助我开始测试。