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.

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

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算