[英]ZF2 phpunit Zend Logger
I am trying to write unit test for my application. 我正在尝试为我的应用程序编写单元测试。 which as logging the information functionality. 其中记录信息功能。
To start with i have service called LogInfo, this how my class look like 首先,我有一个名为LogInfo的服务,这是我的课程的样子
use Zend\Log\Logger;
class LogInfo {
$logger = new Logger;
return $logger;
}
I have another class which will process data. 我有另一个将处理数据的类。 which is below. 在下面。
class Processor
{
public $log;
public function processData($file)
{
$this->log = $this->getLoggerObj('data');
$this->log->info("Received File");
}
public function getLoggerObj($logType)
{
return $this->getServiceLocator()->get('Processor\Service\LogInfo')->logger($logType);
}
}
here i am calling service Loginfo and using it and writing information in a file. 在这里我调用服务Loginfo并使用它并在文件中写入信息。
now i need to write phpunit for class Processor 现在我需要为类Processor编写phpunit
below is my unit test cases 下面是我的单元测试用例
class ProcessorTest{
public function setUp() {
$mockLog = $this->getMockBuilder('FileProcessor\Service\LogInfo', array('logger'))->disableOriginalConstructor()->getMock();
$mockLogger = $this->getMockBuilder('Zend\Log\Logger', array('info'))->disableOriginalConstructor()->getMock();
$serviceManager = new ServiceManager();
$serviceManager->setService('FileProcessor\Service\LogInfo', $mockLog);
$serviceManager->setService('Zend\Log\Logger', $mockLogger);
$this->fileProcessor = new Processor();
$this->fileProcessor->setServiceLocator($serviceManager);
}
public function testProcess() {
$data = 'I have data here';
$this->fileProcessor->processData($data);
}
}
I try to run it, i am getting an error "......PHP Fatal error: Call to a member function info() on a non-object in" 我尝试运行它,我收到一个错误“...... PHP致命错误:在一个非对象上调用成员函数info()
i am not sure , how can i mock Zend logger and pass it to class. 我不确定,我怎么能模仿Zend记录器并将其传递给课堂。
Lets check out some of your code first, starting with the actual test class ProcessorTest
. 让我们先看看你的一些代码,从实际的测试类ProcessorTest
。 This class constructs a new ServiceManager()
. 该类构造一个new ServiceManager()
。 This means you are going to have to do this in every test class, which is not efficient ( DRY ). 这意味着您将不得不在每个测试类中执行此操作,这是无效的( DRY )。 I would suggest constructing the ServiceMananger
like the Zend Framework 2 documentation describes in the headline Bootstrapping your tests . 我建议构建ServiceMananger
就像标题引导你的测试中的Zend Framework 2文档所描述的那样。 The following code is the method we are interested in. 以下代码是我们感兴趣的方法。
public static function getServiceManager()
{
return static::$serviceManager;
}
Using this approach makes it possible to obtain the instance of ServiceManager
through Bootstrap::getServiceManager()
. 使用此方法可以通过Bootstrap::getServiceManager()
获取ServiceManager
实例。 Lets refactor the test class using this method. 让我们使用这种方法重构测试类。
class ProcessorTest
{
protected $serviceManager;
protected $fileProcessor;
public function setUp()
{
$this->serviceManager = Bootstrap::getServiceManager();
$this->serviceManager->setAllowOverride(true);
$fileProcessor = new Processor();
$fileProcessor->setServiceLocator($this->serviceManager);
$this->fileProcessor = $fileProcessor;
}
public function testProcess()
{
$mockLog = $this->getMockBuilder('FileProcessor\Service\LogInfo', array('logger'))
->disableOriginalConstructor()
->getMock();
$mockLogger = $this->getMockBuilder('Zend\Log\Logger', array('info'))
->disableOriginalConstructor()
->getMock();
$serviceManager->setService('FileProcessor\Service\LogInfo', $mockLog);
$serviceManager->setService('Zend\Log\Logger', $mockLogger);
$data = 'I have data here';
$this->fileProcessor->processData($data);
}
}
This method also makes it possible to change expectations on the mock objects per test function. 此方法还可以更改每个测试函数对模拟对象的期望。 The Processor
instance is constructed in ProcessorTest::setUp()
which should be possible in this case. Processor
实例在ProcessorTest::setUp()
构造,在这种情况下应该是可能的。
Any way this does not solve your problem yet. 无论如何这还没有解决你的问题。 I can see Processor::getLoggerObj()
asks the ServiceManager
for the service 'Processor\\Service\\LogInfo'
but your test class does not set this instance anywhere. 我可以看到Processor::getLoggerObj()
向ServiceManager
询问服务'Processor\\Service\\LogInfo'
但是你的测试类没有在任何地方设置这个实例。 Make sure you set this service in your test class like the following example. 确保在测试类中设置此服务,如下例所示。
$this->serviceManager->setService('Processor\Service\LogInfo', $processor);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.