doulu7921 2017-03-20 10:26
浏览 36
已采纳

使用新的关键字洞察单元测试PHP类

So the other day I have a bad designed PHP class which, for example, populate some objects:

class Populator
{
    function populate(array $data): array
    {
        $result = [];

        foreach ($data as $row) {
            $result[] = new PopulateMeGently($row);
        }

        return $result;
    }
}

Assume also PopulateMeGently is really heavy and the only option is mock it in unit tests.

So how test populate method in Populator class? Or how to refactor it, to make testable? I have available IoC and other fancy stuff I can use, but for now everything I tried looks bulky and ugly.

Thank you!

  • 写回答

1条回答 默认 最新

  • drpjdfj618393 2017-03-21 08:43
    关注

    the least and cheapest thing you could do would be smth like

    class Populator
    {
        private $rowProcessor;
    
        public function __construct($rowProcessor){
            $this->rowProcessor = $rowProcessor;
        }
    
        public function populate(array $data){
            $result = [];
            foreach($data as $row){
                $result[] = $this->rowProcessor->process($row);
            }
            return $result;
        }
    }
    

    general idea is to have some other object injected into Populator and make it responsible for PopulateMeGently creation. Don't new inside because it's uncontrollable and thus untestable.

    Concerning names and implementations it could be some Factory that could implement even methods like makeDefault(), makeNullPopulateMeGently(), makeFromRaws($data) or whatever fits to your design and needs.

    No you can mock stuff as:

    class PopulatorTest extends PHPUnit_Framework_TestCase
    {
        private $populator;
        private $rowProcessorMock;
    
        public function setUp(){
            $this->rowProcessorMock = $this->getMockBuilder('RowProcessor')
                                           ->setMethods(array('process'))
                                           ->getMock();
            $this->populator = new Populator($this->rowProcessorMock);
        }
    
        /**
        * @test
        */
        public function canPopulate(){
            $data = array(0, 1);
            $this->rowProcessorMock->expects($this->exactly(2))
                                   ->method('process')
                                   ->withConsecutive(
                                       [$this->equalTo(0)],
                                       [$this->equalTo(1)]
                                    )
                                   ->will($this->onConsecutiveCalls('zero', 'one'));
            $result = $this->populator->populate($data);
            $this->assertSame(array('zero', 'one'), $result);
        }
    }
    

    the canPopulate tests all the stuff that needs to happen with $data -- all its items get processed in order, result is returned as array of tranformed data. Please note that you don't really need to mock out PopulateMeGently (or whatever you'll rename it to). You just make sure that the thing that is responsible for processing your data is called with appropriate parameters and its results are returned appropriately.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。