duanlie7962 2015-03-20 10:51
浏览 22
已采纳

如何在PHP中编写接口测试

If I'm writing an interface I usually specify the behavior the implementations should expose. For ensuring this behavior one should write tests against this interface. How do I best write and organize those tests in a way, that they can easily be used by writers of implementations to ensure their implementations meet the requirements? Is some way of extending (writing subclasses) the interfaces tests the way to go or do I implements some design-pattern like factory?

  • 写回答

2条回答 默认 最新

  • donglu9896 2015-03-20 11:32
    关注

    You could create an abstract class with some basic tests of interface contracts.

    Consider following example:

    interface FactorialComputer {
        public function compute($input);
    }
    
    class RecursiveFactorialComputer implements FactorialComputer  {
        public function compute($input) {
            if ($input < 0) {
                throw new InvalidArgumentException(...);
            }
    
            if ($input == 0 || $input == 1) {
                return 1;
            }
    
            return $input * $this->compute($input - 1);
        }
    }
    
    class IterativeFactorialComputer implements FactorialComputer  {
        public function compute($input) {
            $result = 1;
    
            for ($i = 1; $i <= $input; $i++) {
                $result *= $i;
            }
    
            return $result;
        }
    }
    

    And tests for both implementations:

    abstract class AbstractFactorialComputerTest extends PHPUnit_Framework_TestCase {
        /**
         * @var FactorialComputer 
         */
        protected $instance;
    
        protected abstract function getComputerInstance();
    
        public function setUp() {
            $this->instance = $this->getComputerInstance;
        }
    
        /**
         * @expectedException InvalidArgumentException
         */
        public function testExceptionOnInvalidArgument() {
            $this->instance->compute(-1);
        }
    
        public function testEdgeCases()
        {
            $this->assertEquals(1, $this->instance->compute(0));
            $this->assertEquals(1, $this->instance->compute(1));
        }
    
        ...
    }
    
    class RecursiveFactorialComputerTest extends AbstractFactorialComputerTest
    {
        protected abstract function getComputerInstance() {
            return new RecursiveFactorialComputer();
        }
    
        public function testComputeMethodCallsCount() {
            // get mock and test number of compute() calls
        }
    }
    
    class IterativeFactorialComputerTest extends AbstractFactorialComputerTest
    {
        protected abstract function getComputerInstance() {
            return new IterativeFactorialComputer();
        }
    }
    

    Using this approach every programmer should be capable of creating a complete unit test for interface implementation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现