ste*_*ros 5 php phpunit unit-testing mocking redis
我正在测试的类在更深的地方使用Redis:
<?php
class Publisher {
function publish($message) {
Redis::publish($message);
}
}
class Foo {
public function publishMessage() {
$message = $this->generateMessage();
$this->publish($message);
}
private function publish($message) {
$this->getPublisher()->publish($message);
}
// below just for testing
private $publisher;
public function getPublisher() {
if(empty($this->publisher) {
return new Publisher();
}
return $this->publisher;
}
public function setPublisher($publisher) {
$this->publisher = $publisher;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我不知道该如何测试。当然,我不想测试Redis。我真正需要测试的是发送给Redis的消息是否符合我的期望。(我认为)我可以编写一个返回消息并公开的函数。但是我不喜欢那个主意。在此示例中,我可以设置发布者,因此在测试时我可以返回另一个发布者类。与其发送消息,不如将其保存在内部,以便稍后声明。
class Publisher {
public $message;
function publish($message) {
$this->message = $message;
}
}
Run Code Online (Sandbox Code Playgroud)
但是然后我不知道如何模拟Publisher类来更改方法。否则我将不得不继承Publisher类。同样,通过这种方式,我的测试类必须包含仅用于测试的代码。我也不喜欢。
我该如何正确测试?存在用于Redis的模拟库,但不支持发布。
Some options described as methods of test class
class FooTest extends PHPUnit_Framework_TestCase // or PHPUnit\Framework\TestCase for version
{
/**
* First option: with PHPUnit's MockObject builder.
*/
public function testPublishMessageWithMockBuilder() {
// Internally mock builder creates new class that extends your Publisher
$publisherMock = $this
->getMockBuilder(Publisher::class)
->setMethods(['publish'])
->getMock();
$publisherMock
->expects($this->any()) // how many times we expect our method to be called
->method('publish') // which method
->with($this->exactly('your expected message')) // with what parameters we expect method "publish" to be called
->willReturn('what should be returned');
$testedObject = new Foo;
$testedObject->setPublisher($publisherMock);
$testedObject->publish();
}
/**
* Second option: with Prophecy
*/
public function testPublishMessageWithProphecy() {
// Internally prophecy creates new class that extends your Publisher
$publisherMock = $this->prophesize(Publisher::class);
// assert that publish should be called with parameters
$publisherMock
->publish('expected message')
->shouldBeCalled();
$testedObject = new Foo;
$testedObject->setPublisher($publisherMock->reveal());
$testedObject->publish();
}
/**
* Third wierd option: with anonymous class (php version >= 7)
* I am not recommend do something like that, its just for example
*/
public function testFooWithAnonymousClass()
{
// explicitly extend stubbed class and overwrite method "publish"
$publisherStub = new class () extends Publisher {
public function publish($message)
{
assert($message === 'expexted message');
}
};
$testedObject = new Foo;
$testedObject->setPublisher($publisherStub->reveal());
$testedObject->publish();
}
}
Run Code Online (Sandbox Code Playgroud)
As a side note: if your Foo class requires Publisher for its work you should set it via constructor, not setter method. Use setter methods only for optional dependencies
So from the comments I suggest in the actual code you are creating object of Publisher class with new
like this
public function publishMessage() {
$message = $this->generateMessage();
$publisher = new Publisher;
$publisher->publish($message);
}
Run Code Online (Sandbox Code Playgroud)
Or maybe you are using Redis::publish
static method directly
public function publishMessage() {
$message = $this->generateMessage();
Redis::publish($message);
}
Run Code Online (Sandbox Code Playgroud)
Well, this is called coupled classes and is considered a bad practice, because violates D in SOLID. Nonetheless, there is a workaround for mocking/stubbing dependency in such cases, again with anonymous classes.
Assuming that dependency class didn't loaded yet you can do something like this:
$class = new class() {
function publish(string $message) {
assert($message === 'expected');
}
};
class_alias(get_class($class), 'Redis');
Run Code Online (Sandbox Code Playgroud)
If you repeat this trick in multiple tests you will get warning:
PHP Warning: Cannot declare class Redis, because the name is already in use
To overcome it you will need to run your tests with --process-isolation
I think we should never do it (it is a dirty hack) and use DI, but sometimes we deal with legacy
归档时间: |
|
查看次数: |
2198 次 |
最近记录: |