好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > PHPUnit + Laravel單元測試常用技能

PHPUnit + Laravel單元測試常用技能

熱門標簽:江西電銷機器人收費 高德地圖標注店鋪收費嗎 泊頭在哪里辦理400電話 天潤融通外呼系統好嗎 江門回撥外呼系統 高德地圖標注位置怎么標注 杭州語音電銷機器人 電銷機器人沒有效果怎么樣 欣思維地圖標注

1. 數據供給器

用來提供參數和結果,使用 @dataProvider 標注來指定使用哪個數據供給器方法。例如檢測app升級數據是否符合預期,addProviderAppUpdateData()提供測試的參數和結果。testAppUpdateData()檢測appUpdateData()返回的結果是否和給定的預期結果相等,即如果$appId='apple_3.3.2_117', $result=['status' => 0, 'isIOS' => false], 則$data中如果含有['status' => 0, 'isIOS' => false], 則斷言成功。建議在數據提供器,逐個用字符串鍵名對其命名,這樣在斷言失敗的時候將輸出失敗的名稱,更容易定位問題

示例代碼:

?php
  namespace Tests\Unit;

  use App\Services\ClientService;
  use Tests\TestCase;

  class ClientServiceTest extends TestCase
  {
    /**
     * @dataProvider addProviderAppUpdateData
     *
     * @param $appId
     * @param $result
     */
    public function testAppUpdateData($appId, $result)
    {
      $data = (new ClientService($appId))->appUpdateData();

      $this->assertTrue(count(array_intersect_assoc($data, $result)) == count($result));
    }

    public function addProviderAppUpdateData()
    {
      return [
        'null'         => [null, ['status' => 0, 'isIOS' => false, 'latest_version' => 'V']],
        'error app id'     => ['sdas123123', ['status' => 0, 'isIOS' => false, 'latest_version' => 'V']],
        'android force update' => ['bx7_3.3.5_120', ['status' => 0, 'isIOS' => false]],
        'ios force update'   => ['apple_3.3.2_117', ['status' => 1, 'isIOS' => true]],
        'android soft update' => ['sanxing_3.3.2_117', ['status' => 2, 'isIOS' => false]],
        'ios soft update'   => ['apple_3.3.3_118', ['status' => 2, 'isIOS' => true]],
        'android normal'    => ['fhqd_3.3.6_121', ['status' => 1, 'isIOS' => false]],
        'ios normal'      => ['apple_3.3.5_120', ['status' => 1, 'isIOS' => true]],
        'h5'          => ['h5_3.3.3', ['status' => 1, 'isIOS' => false]]
      ];
    }
  }

斷言成功結果:

2. 斷言方法

常用有assertTrue(), assertFalse(), assertNull(), assertEquals(), assertThat()。

assertThat()自定義斷言。常用的約束有isNull()、isTrue()、isFalse()、isInstanceOf();常用的組合約束logicalOr()、logicalAnd()。例如檢測返回的結果是否是null或ApiApp類。

示例代碼:

?php
  namespace Tests\Unit;

  use App\Models\ApiApp;
  use App\Services\SystemConfigService;
  use Tests\TestCase;

  class SystemConfigServiceTest extends TestCase
  {
    /**
     * @dataProvider additionProviderGetLatestUpdateAppApi
     *
     * @param $appType
     */
    public function testGetLatestUpdateAppApi($appType)
    {
      $result = SystemConfigService::getLatestUpdateAppApi($appType);
      $this->assertThat($result, $this->logicalOr($this->isNull(), $this->isInstanceOf(ApiApp::class)));
    }

    public function additionProviderGetLatestUpdateAppApi()
    {
      return [
        'apple'  => [1],
        'android' => [2],
        'null'  => [9999]
      ];
    }
  }

斷言成功結果:

3. 對異常進行測試

使用expectExceptionCode()對錯誤碼進行檢測,不建議對錯誤信息文案進行檢測。例如檢測設備被鎖后是否拋出3026錯誤碼。

示例代碼:

?php
  namespace Tests\Unit;

  use App\Services\UserSecurityService;
  use Illuminate\Support\Facades\Cache;
  use Tests\TestCase;

  class UserSecurityServiceTest extends TestCase
  {
    public static $userId = 4;

    /**
     * 設備鎖檢測
     * @throws \App\Exceptions\UserException
     */
    public function testDeviceCheckLock()
    {
      $this->expectExceptionCode(3026);
      Cache::put('device-login-error-account-', '1,2,3,4,5', 300);
      UserSecurityService::$request = null;
      UserSecurityService::$udid  = null;
      UserSecurityService::deviceCheck(self::$userId);
    }
  }

斷言成功結果:

4. 測試私有屬性和私有方法使用反射機制

如果只測試私有方法可使用ReflectionMethod()反射方法,使用setAccessible(true)設置方法可訪問,并使用invokeArgs()或invoke()調用方法(invokeArgs將參數作為數組傳遞)。例如檢測IP是否在白名單中。

示例代碼:

被檢測代碼:

namespace App\Facades\Services;

  /**
   * Class WebDefender
   */
  class WebDefenderService extends BaseService
  {
     //ip白名單
    private $ipWhiteList = [
      '10.*', 
      '172.18.*', 
      '127.0.0.1' 
    ];

    /**
     * ip是否在白名單中
     *
     * @param string $ip
     *
     * @return bool
     */
    private function checkIPWhiteList($ip)
    {
      if (!$this->ipWhiteList || !is_array($this->ipWhiteList)) {
        return false;
      }
      foreach ($this->ipWhiteList as $item) {
        if (preg_match("/{$item}/", $ip)) {
          return true;
        }
      }

      return false;
    }
   }

檢測方法:

?php

  namespace Tests\Unit;

  use App\Facades\Services\WebDefenderService;
  use Tests\TestCase;

  class WebDefenderTest extends TestCase
  {
    /**
     * 測試IP白名單
     * @dataProvider additionProviderIp
     *
     * @param $ip
     * @param $result
     *
     * @throws \ReflectionException
     */
    public function testIPWhite($ip, $result)
    {
      $checkIPWhiteList = new \ReflectionMethod(WebDefenderService::class, 'checkIPWhiteList');
      $checkIPWhiteList->setAccessible(true);
      $this->assertEquals($result, $checkIPWhiteList->invokeArgs(new WebDefenderService(), [$ip]));
    }

    public function additionProviderIp()
    {
      return [
        '10 ip' => ['10.1.1.7', true],
        '172 ip' => ['172.18.2.5', true],
        '127 ip' => ['127.0.0.1', true],
        '192 ip' => ['192.168.0.1', false]
      ];
    }
   }

測試私有屬性可使用ReflectionClass(), 獲取屬性用getProperty(), 設置屬性的值用setValue(), 獲取方法用getMethod(), 設置屬性和方法可被訪問使用setAccessible(true)。例如檢測白名單路徑。

示例代碼:

被檢測代碼:

?php
  namespace App\Facades\Services;

  use App\Exceptions\ExceptionCode;
  use App\Exceptions\UserException;
  use Illuminate\Support\Facades\Cache;

  /**
   * CC攻擊防御器
   * Class WebDefender
   */
  class WebDefenderService extends BaseService
  {
    //路徑白名單(正則)
    private $pathWhiteList = [
      //'^auth\/(.*)',
    ];

    private static $request = null;

     /**
     * 請求路徑是否在白名單中
     *
     * @return bool
     */
    private function checkPathWhiteList()
    {
      $path = ltrim(self::$request->getPathInfo(), '/');
      if (!$path || !$this->pathWhiteList || !is_array($this->pathWhiteList)) {
        return false;
      }
      foreach ($this->pathWhiteList as $item) {
        if (preg_match("/$item/", $path)) {
          return true;
        }
      }

      return false;
    }
  }

檢測方法:

?php
  namespace Tests\Unit;

  use App\Facades\Services\WebDefenderService;
  use Illuminate\Http\Request;
  use Tests\TestCase;

  class WebDefenderTest extends TestCase
  {
     /**
     * 檢測白名單路徑
     * @dataProvider additionProviderPathWhiteList
     *
     * @param $pathProperty
     * @param $request
     * @param $result
     *
     * @throws \ReflectionException
     */
    public function testCheckPathWhiteList($pathProperty, $request, $result)
    {
      $reflectedClass = new \ReflectionClass('App\Facades\Services\WebDefenderService');

      $webDefenderService   = new WebDefenderService();
      $reflectedPathWhiteList = $reflectedClass->getProperty('pathWhiteList');
      $reflectedPathWhiteList->setAccessible(true);
      $reflectedPathWhiteList->setValue($webDefenderService, $pathProperty);

      $reflectedRequest = $reflectedClass->getProperty('request');
      $reflectedRequest->setAccessible(true);
      $reflectedRequest->setValue($request);

      $reflectedMethod = $reflectedClass->getMethod('checkPathWhiteList');
      $reflectedMethod->setAccessible(true);
      $this->assertEquals($result, $reflectedMethod->invoke($webDefenderService));
    }

    public function additionProviderPathWhiteList()
    {
      $allPath      = ['.*'];
      $checkPath     = ['^auth\/(.*)'];
      $authSendSmsRequest = new Request([], [], [], [], [], ['HTTP_HOST' => 'api.dev.com', 'REQUEST_URI' => '/auth/sendSms']);
      $indexRequest    = new Request([], [], [], [], [], ['HTTP_HOST' => 'api.dev.com', 'REQUEST_URI' => '/']);
      $noMatchRequest   = new Request([], [], [], [], [], ['HTTP_HOST' => 'api.dev.com', 'REQUEST_URI' => '/product/sendSms']);

      return [
        'index'        => [[], $authSendSmsRequest, false],
        'no request'     => [$allPath, $indexRequest, false],
        'all request'     => [$allPath, $authSendSmsRequest, true],
        'check auth sms'   => [$checkPath, $authSendSmsRequest, true],
        'check path no match' => [$checkPath, $noMatchRequest, false]
      ];
    }
  }

5. 代碼覆蓋率

使用--coverage-html導出的報告含有類與特質覆蓋率、行覆蓋率、函數與方法覆蓋率。可查看當前單元測試覆蓋的范圍。例如輸出WebDefenderTest的代碼覆蓋率到桌面(phpunit tests/unit/WebDefenderTest --coverage-html ~/Desktop/test)

6. 指定代碼覆蓋率報告要包含哪些文件

在配置文件(phpunit.xml)里設置whitelist中的processUncoveredFilesFromWhitelist=true, 設置目錄用directory>標簽,設置文件用file>標簽。例如指定app/Services目錄下的所有文件和app/Facades/Services/WebDefenderService.php在報告中。

示例代碼:

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

      testsuite name="Feature">
        directory suffix="Test.php">./tests/Feature/directory>
      /testsuite>
    /testsuites>
    filter>
      whitelist processUncoveredFilesFromWhitelist="true">
        directory suffix=".php">./app/Services/directory>
        file>./app/Facades/Services/WebDefenderService.php/file>
      /whitelist>
    /filter>
    php>
      server name="APP_ENV" value="local"/>
      server name="BCRYPT_ROUNDS" value="4"/>
      server name="CACHE_DRIVER" value="credis"/>
      server name="MAIL_DRIVER" value="array"/>
      server name="QUEUE_CONNECTION" value="sync"/>
      server name="SESSION_DRIVER" value="array"/>
      server name="APP_CONFIG_CACHE" value="bootstrap/cache/config.phpunit.php"/>
      server name="APP_SERVICES_CACHE" value="bootstrap/cache/services.phpunit.php"/>
      server name="APP_PACKAGES_CACHE" value="bootstrap/cache/packages.phpunit.php"/>
      server name="APP_ROUTES_CACHE" value="bootstrap/cache/routes.phpunit.php"/>
      server name="APP_EVENTS_CACHE" value="bootstrap/cache/events.phpunit.php"/>
    /php>
  /phpunit>

7. 參考文檔

PHPUnit官方文檔 https://phpunit.readthedocs.io/zh_CN/latest/index.html
反射類 https://www.php.net/manual/en/class.reflectionclass.php
反射方法 https://www.php.net/manual/en/class.reflectionmethod.php

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 詳解Yaf框架PHPUnit集成測試方法
  • PHP單元測試配置與使用方法詳解
  • PHP使用phpunit進行單元測試示例
  • php使用yield對性能提升的測試實例分析
  • 高質量PHP代碼的50個實用技巧必備(下)
  • 高質量PHP代碼的50個實用技巧必備(上)
  • 很讓人受教的 提高php代碼質量36計
  • 寫出高質量的PHP程序
  • 淺談如何提高PHP代碼質量之端到端集成測試

標簽:大同 平涼 深圳 內江 江門 石嘴山 雙鴨山 駐馬店

巨人網絡通訊聲明:本文標題《PHPUnit + Laravel單元測試常用技能》,本文關鍵詞  PHPUnit,Laravel,單元,測試,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHPUnit + Laravel單元測試常用技能》相關的同類信息!
  • 本頁收集關于PHPUnit + Laravel單元測試常用技能的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 全免费A级毛片免费看表情包桃豆 欧美激情婬妇ⅩXXOOO炮击 | 3d蒂法浓厚体液榨取动漫在线观看| 男生的鸡鸡视频| 大尺度激情吻胸吃奶视频| 成人h无码日本3D动漫| 尤果网美女大尺度写真| freehdxxxxcartoon游戏| 一级欧美一级A一级a爱片奂费| 东北女人的性生殖| 办公室揉弄奶头高潮呻吟A片双男 免费无码又爽又刺激A片软件妖精 | 亻俄罗斯性xxxx| 国产亚洲精品美女2020久久| 女体bd**| 扒开腿挺进粉嫩小泬喷水视频| 老师好湿?好紧?太爽了| 在车里被弄了H野战高H漫画| 亚洲国产欧美在线看片XXOO| 关于黄色的软件| 又色又爽又黄的视频软件app| 操美女视频| 欧美性极品黑人hd| 扒开上部?狂揉下部?| 甜性涩爱播放| 色婷婷色99国产综合精品| 美女扒开屁股让男人桶爽免费 | 91视频ap| 无遮挡全彩口工h全彩| 九九热精品视频在线观看| 91色噜噜噜狠狠色综合久色综合| 国产成人高清在线| 色噜噜噜噜亚洲第一| 亚洲乱强伦| janpense visa 18-19hd| 芒果视频www.5.在线观看| 免费看日b视频| 欧美亚洲国产精品第一页| 女囚肉体慰安房无删减版| 午夜a一级毛片| 成人免费| 亚洲啪啪网| 欧美日韩黄漫AV无遮挡在线观看|