Laravel配置PHP测试

PHPCS

  • 静态测试
  • 包含如下两个组件:
    • phpcs —— 静态测试,编码规范检查
    • phpcbf —— 自动修复编码规范

安装php

composer require --dev squizlabs/php_codesniffer:~2.7

配置
项目根目录下设定配置文件 phpcs.xmlbootstrap

<?xml version="1.0"?>
<ruleset name="Mysite PHPCS Code Standard">
    <description>The coding standard at mysite.com</description>
    <arg name="tab-width" value="4" />
    <arg name="encoding" value="utf-8" />
    <arg value="s" />
    <rule ref="PSR2">
        <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace" />
    </rule>
    <file>./app</file>
    <file>./tests</file>
</ruleset>

运行bash

./vendor/bin/phpcs -i  # 查看已安装的规范

# 程序路径 规范路径
./vendor/bin/phpcs --standard=phpcs.xml
./vendor/bin/phpcbf --standard=phpcs.xml

PHPMD

  • 静态测试
  • 代码冗余度及质量检查,包括如下规范检查:
    • Clean Code Rules
    • Code Size Rules
    • Controversial Rules
    • Design Rules
    • Naming Rules
    • Unused Code Rules

安装app

composer require --dev phpmd/phpmd:~2.5

运行composer

#程序路径 测试路径 报告格式 规则路径
./vendor/bin/phpmd app,tests text resources/rulesets/cleancode.xml,resources/rulesets/codesize.xml,resources/rulesets/controversial.xml,resources/rulesets/design.xml,resources/rulesets/naming.xml,resources/rulesets/unusedcode.xml

PHPUNIT

  • 动态测试
  • 能够进行如下几种具体测试:
    • 单元测试
    • 集成测试

安装单元测试

composer require --dev phpunit/phpunit:~5.0

配置
项目根目录下设定配置文件 phpunit.xml测试

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/app.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false">
    <testsuites>
        <testsuite name="Feature Tests">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>

        <testsuite name="Unit Tests">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="APP_KEY" value="base64:应用秘钥"/>
    </php>
</phpunit>

运行ui

./vendor/bin/phpunit --configuration phpunit.xml

临时关闭规则编码

# 代码块上添加注解命令,phpmd规则举例
@SuppressWarnings(PHPMD[.规则])
相关文章
相关标签/搜索