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.

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

报告相同问题?