Rumah  >  Artikel  >  pembangunan bahagian belakang  >  thinkphp3.2.3怎样使用think-phpunit来进行单元测试的介绍

thinkphp3.2.3怎样使用think-phpunit来进行单元测试的介绍

不言
不言asal
2018-07-11 14:06:362263semak imbas

这篇文章主要介绍了关于thinkphp3.2.3怎样使用think-phpunit来进行单元测试的介绍,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

问题描述

thinkphp3.2.3官方并没有提供单元测试的工具。随着项目的增大,要保证代码的健壮性,单元测试不得不提上日程。经实践,https://github.com/snowair/think-phpunit 是个不错的选择,但在使用过程中出现const 未定义的错误。具体报错信息为:syntax error, unexpected 'const' (T_CONST),

问题分析

const关键字提示语法错误,说明当前的PHPUNIT并不支持该语法,查阅官方文档发现,constphp5.3新引用的功能。所以根本原因应该是PHPUNIT版本过低引起的。

const定义常量的官方地址说明:http://php.net/manual/zh/lang...

我们查看vender\snowair/composer.json发现以下代码:

  "require": {
    "php":">=5.4",
    "phpunit/phpunit": "^4.7"
  }

即要求phpunit版本为>=4.7 && <5.0。至此问题原因确认。至phpunit版本过低引发的关键字错误。

解决方法:

参考php composerVCS版本控制部分,修改phpunit的版本号。

  1. fork原项目至自己的仓库。比如我fork后的项目地址为:https://github.com/callme119/think-phpunit.

  2. https://github.com/callme119/think-phpunit中的composer.json文件,将phpunit的版本修改为^5.0

  3. 修改项目引用的VCS部分。增加repositories属性。

比如我的项目引用,修改后为:

{
    "name": "topthink/thinkphp",
    "description": "the ThinkPHP Framework",
    "type": "framework",
    "keywords": ["framework","thinkphp","ORM"],
    "homepage": "http://thinkphp.cn/",
    "license": "Apache2",
    "authors": [
        {
            "name": "liu21st",
            "email": "liu21st@gmail.com"
        }
    ],
    "require": {
        "php": ">=5.3.0"
    },
    "autoload": {
        "classmap": ["Application","ThinkPHP/Library"]
    },
    "autoload-dev": {
        "psr-0": { "": "test" }
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/callme119/think-phpunit"
        }
    ],
    "require-dev": {
        "snowair/think-phpunit": "dev-master"
    },
    "minimum-stability": "dev"
}
composer VCS官方地址: https://docs.phpcomposer.com/...

总结

  1. 如果你只要想正常的使用snowair/think-phpunit, 那么请参阅https://github.com/callme119/think-phpunit进行安装即可。

  2. 如果你是其它的项目的版本问题,请参阅本文,利用github来进行修复后,并在自己项目的composer.json中,增加repositories属性来指明特定仓库。

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

如何通过php来遍历文件夹获取图片目录名称及文件名

Atas ialah kandungan terperinci thinkphp3.2.3怎样使用think-phpunit来进行单元测试的介绍. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn