Home > Backend Development > PHP Tutorial > Introduction to how to use think-phpunit for unit testing in thinkphp3.2.3

Introduction to how to use think-phpunit for unit testing in thinkphp3.2.3

不言
Release: 2023-04-03 06:12:02
Original
2370 people have browsed it

This article mainly introduces how to use think-phpunit for unit testing in thinkphp3.2.3. It has a certain reference value. Now I share it with you. Friends in need can refer to the

question. Description

thinkphp3.2.3 officially does not provide unit testing tools. As the project grows, unit testing has to be put on the agenda to ensure the robustness of the code. After practice, https://github.com/snowair/think-phpunit is a good choice, but const undefined errors occurred during use. The specific error message is: syntax error, unexpected 'const' (T_CONST),

Problem Analysis

constKeyword prompt syntax error, description The current PHPUNIT does not support this syntax. After consulting the official documentation, we found that const is a newly referenced function of php5.3. So the root cause should be that the PHPUNIT version is too low.

The official address description of const defined constants: http://php.net/manual/zh/lang...

Let’s checkvender\snowair/composer.jsonThe following code was found:

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

which requires the phpunit version to be >=4.7 && <5.0. The cause of the problem is now confirmed. Keyword error caused by phpunit version being too low.

Solution:

Refer to the VCS version control section of php composer and modify the version number of phpunit.

  1. forkThe original project to your own warehouse. For example, the project address after my fork is: https://github.com/callme119/think-phpunit.

  2. The composer.json file in https://github.com/callme119/think-phpunit, modify the version of phpunit to ^5.0

  3. Modify The VCS part referenced by the project. Add repositories attribute.

For example, my project reference, after modification, is:

{
    "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 official address: https://docs.phpcomposer.com/...

Summary

  1. If you just want to use snowair/think-phpunit normally, then please refer to https://github.com/callme119 /think-phpunit just install it.

  2. If you have version problems in other projects, please refer to this article, use github to fix it, and add it to composer.json of your own project. repositoriesAttribute to specify a specific repository.

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to traverse folders through php to obtain the image directory name and file name

The above is the detailed content of Introduction to how to use think-phpunit for unit testing in thinkphp3.2.3. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template