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[.規則])
相關文章
相關標籤/搜索