PHPUnit + Laravel单元测试常用技能

时间:2022-09-23 21:07:57

1. 数据供给器

用来提供参数和结果,使用 @dataProvider 标注来指定使用哪个数据供给器方法。例如检测app升级数据是否符合预期,addProviderAppUpdateData()提供测试的参数和结果。testAppUpdateData()检测appUpdateData()返回的结果是否和给定的预期结果相等,即如果$appId='apple_3.3.2_117', $result=['status' => 0, 'isIOS' => false], 则$data中如果含有['status' => 0, 'isIOS' => false], 则断言成功。建议在数据提供器,逐个用字符串键名对其命名,这样在断言失败的时候将输出失败的名称,更容易定位问题

示例代码:

  1. <?php
  2. namespace Tests\Unit;
  3.  
  4. use App\Services\ClientService;
  5. use Tests\TestCase;
  6.  
  7. class ClientServiceTest extends TestCase
  8. {
  9. /**
  10. * @dataProvider addProviderAppUpdateData
  11. *
  12. * @param $appId
  13. * @param $result
  14. */
  15. public function testAppUpdateData($appId, $result)
  16. {
  17. $data = (new ClientService($appId))->appUpdateData();
  18.  
  19. $this->assertTrue(count(array_intersect_assoc($data, $result)) == count($result));
  20. }
  21.  
  22. public function addProviderAppUpdateData()
  23. {
  24. return [
  25. 'null' => [null, ['status' => 0, 'isIOS' => false, 'latest_version' => 'V']],
  26. 'error app id' => ['sdas123123', ['status' => 0, 'isIOS' => false, 'latest_version' => 'V']],
  27. 'android force update' => ['bx7_3.3.5_120', ['status' => 0, 'isIOS' => false]],
  28. 'ios force update' => ['apple_3.3.2_117', ['status' => 1, 'isIOS' => true]],
  29. 'android soft update' => ['sanxing_3.3.2_117', ['status' => 2, 'isIOS' => false]],
  30. 'ios soft update' => ['apple_3.3.3_118', ['status' => 2, 'isIOS' => true]],
  31. 'android normal' => ['fhqd_3.3.6_121', ['status' => 1, 'isIOS' => false]],
  32. 'ios normal' => ['apple_3.3.5_120', ['status' => 1, 'isIOS' => true]],
  33. 'h5' => ['h5_3.3.3', ['status' => 1, 'isIOS' => false]]
  34. ];
  35. }
  36. }

断言成功结果:

PHPUnit + Laravel单元测试常用技能

2. 断言方法

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

assertThat()自定义断言。常用的约束有isNull()、isTrue()、isFalse()、isInstanceOf();常用的组合约束logicalOr()、logicalAnd()。例如检测返回的结果是否是null或ApiApp类。

示例代码:

  1. <?php
  2. namespace Tests\Unit;
  3.  
  4. use App\Models\ApiApp;
  5. use App\Services\SystemConfigService;
  6. use Tests\TestCase;
  7.  
  8. class SystemConfigServiceTest extends TestCase
  9. {
  10. /**
  11. * @dataProvider additionProviderGetLatestUpdateAppApi
  12. *
  13. * @param $appType
  14. */
  15. public function testGetLatestUpdateAppApi($appType)
  16. {
  17. $result = SystemConfigService::getLatestUpdateAppApi($appType);
  18. $this->assertThat($result, $this->logicalOr($this->isNull(), $this->isInstanceOf(ApiApp::class)));
  19. }
  20.  
  21. public function additionProviderGetLatestUpdateAppApi()
  22. {
  23. return [
  24. 'apple' => [1],
  25. 'android' => [2],
  26. 'null' => [9999]
  27. ];
  28. }
  29. }

断言成功结果:

PHPUnit + Laravel单元测试常用技能

3. 对异常进行测试

使用expectExceptionCode()对错误码进行检测,不建议对错误信息文案进行检测。例如检测设备被锁后是否抛出3026错误码。

示例代码:

  1. <?php
  2. namespace Tests\Unit;
  3.  
  4. use App\Services\UserSecurityService;
  5. use Illuminate\Support\Facades\Cache;
  6. use Tests\TestCase;
  7.  
  8. class UserSecurityServiceTest extends TestCase
  9. {
  10. public static $userId = 4;
  11.  
  12. /**
  13. * 设备锁检测
  14. * @throws \App\Exceptions\UserException
  15. */
  16. public function testDeviceCheckLock()
  17. {
  18. $this->expectExceptionCode(3026);
  19. Cache::put('device-login-error-account-', '1,2,3,4,5', 300);
  20. UserSecurityService::$request = null;
  21. UserSecurityService::$udid = null;
  22. UserSecurityService::deviceCheck(self::$userId);
  23. }
  24. }

断言成功结果:

PHPUnit + Laravel单元测试常用技能

4. 测试私有属性和私有方法使用反射机制

如果只测试私有方法可使用ReflectionMethod()反射方法,使用setAccessible(true)设置方法可访问,并使用invokeArgs()或invoke()调用方法(invokeArgs将参数作为数组传递)。例如检测IP是否在白名单中。

示例代码:

被检测代码:

  1. namespace App\Facades\Services;
  2.  
  3. /**
  4. * Class WebDefender
  5. */
  6. class WebDefenderService extends BaseService
  7. {
  8. //ip白名单
  9. private $ipWhiteList = [
  10. '10.*',
  11. '172.18.*',
  12. '127.0.0.1'
  13. ];
  14.  
  15. /**
  16. * ip是否在白名单中
  17. *
  18. * @param string $ip
  19. *
  20. * @return bool
  21. */
  22. private function checkIPWhiteList($ip)
  23. {
  24. if (!$this->ipWhiteList || !is_array($this->ipWhiteList)) {
  25. return false;
  26. }
  27. foreach ($this->ipWhiteList as $item) {
  28. if (preg_match("/{$item}/", $ip)) {
  29. return true;
  30. }
  31. }
  32.  
  33. return false;
  34. }
  35. }

检测方法:

  1. <?php
  2.  
  3. namespace Tests\Unit;
  4.  
  5. use App\Facades\Services\WebDefenderService;
  6. use Tests\TestCase;
  7.  
  8. class WebDefenderTest extends TestCase
  9. {
  10. /**
  11. * 测试IP白名单
  12. * @dataProvider additionProviderIp
  13. *
  14. * @param $ip
  15. * @param $result
  16. *
  17. * @throws \ReflectionException
  18. */
  19. public function testIPWhite($ip, $result)
  20. {
  21. $checkIPWhiteList = new \ReflectionMethod(WebDefenderService::class, 'checkIPWhiteList');
  22. $checkIPWhiteList->setAccessible(true);
  23. $this->assertEquals($result, $checkIPWhiteList->invokeArgs(new WebDefenderService(), [$ip]));
  24. }
  25.  
  26. public function additionProviderIp()
  27. {
  28. return [
  29. '10 ip' => ['10.1.1.7', true],
  30. '172 ip' => ['172.18.2.5', true],
  31. '127 ip' => ['127.0.0.1', true],
  32. '192 ip' => ['192.168.0.1', false]
  33. ];
  34. }
  35. }

测试私有属性可使用ReflectionClass(), 获取属性用getProperty(), 设置属性的值用setValue(), 获取方法用getMethod(), 设置属性和方法可被访问使用setAccessible(true)。例如检测白名单路径。

示例代码:

被检测代码:

  1. <?php
  2. namespace App\Facades\Services;
  3.  
  4. use App\Exceptions\ExceptionCode;
  5. use App\Exceptions\UserException;
  6. use Illuminate\Support\Facades\Cache;
  7.  
  8. /**
  9. * CC攻击防御器
  10. * Class WebDefender
  11. */
  12. class WebDefenderService extends BaseService
  13. {
  14. //路径白名单(正则)
  15. private $pathWhiteList = [
  16. //'^auth\/(.*)',
  17. ];
  18.  
  19. private static $request = null;
  20.  
  21. /**
  22. * 请求路径是否在白名单中
  23. *
  24. * @return bool
  25. */
  26. private function checkPathWhiteList()
  27. {
  28. $path = ltrim(self::$request->getPathInfo(), '/');
  29. if (!$path || !$this->pathWhiteList || !is_array($this->pathWhiteList)) {
  30. return false;
  31. }
  32. foreach ($this->pathWhiteList as $item) {
  33. if (preg_match("/$item/", $path)) {
  34. return true;
  35. }
  36. }
  37.  
  38. return false;
  39. }
  40. }

检测方法:

  1. <?php
  2. namespace Tests\Unit;
  3.  
  4. use App\Facades\Services\WebDefenderService;
  5. use Illuminate\Http\Request;
  6. use Tests\TestCase;
  7.  
  8. class WebDefenderTest extends TestCase
  9. {
  10. /**
  11. * 检测白名单路径
  12. * @dataProvider additionProviderPathWhiteList
  13. *
  14. * @param $pathProperty
  15. * @param $request
  16. * @param $result
  17. *
  18. * @throws \ReflectionException
  19. */
  20. public function testCheckPathWhiteList($pathProperty, $request, $result)
  21. {
  22. $reflectedClass = new \ReflectionClass('App\Facades\Services\WebDefenderService');
  23.  
  24. $webDefenderService = new WebDefenderService();
  25. $reflectedPathWhiteList = $reflectedClass->getProperty('pathWhiteList');
  26. $reflectedPathWhiteList->setAccessible(true);
  27. $reflectedPathWhiteList->setValue($webDefenderService, $pathProperty);
  28.  
  29. $reflectedRequest = $reflectedClass->getProperty('request');
  30. $reflectedRequest->setAccessible(true);
  31. $reflectedRequest->setValue($request);
  32.  
  33. $reflectedMethod = $reflectedClass->getMethod('checkPathWhiteList');
  34. $reflectedMethod->setAccessible(true);
  35. $this->assertEquals($result, $reflectedMethod->invoke($webDefenderService));
  36. }
  37.  
  38. public function additionProviderPathWhiteList()
  39. {
  40. $allPath = ['.*'];
  41. $checkPath = ['^auth\/(.*)'];
  42. $authSendSmsRequest = new Request([], [], [], [], [], ['HTTP_HOST' => 'api.dev.com', 'REQUEST_URI' => '/auth/sendSms']);
  43. $indexRequest = new Request([], [], [], [], [], ['HTTP_HOST' => 'api.dev.com', 'REQUEST_URI' => '/']);
  44. $noMatchRequest = new Request([], [], [], [], [], ['HTTP_HOST' => 'api.dev.com', 'REQUEST_URI' => '/product/sendSms']);
  45.  
  46. return [
  47. 'index' => [[], $authSendSmsRequest, false],
  48. 'no request' => [$allPath, $indexRequest, false],
  49. 'all request' => [$allPath, $authSendSmsRequest, true],
  50. 'check auth sms' => [$checkPath, $authSendSmsRequest, true],
  51. 'check path no match' => [$checkPath, $noMatchRequest, false]
  52. ];
  53. }
  54. }

5. 代码覆盖率

使用--coverage-html导出的报告含有类与特质覆盖率、行覆盖率、函数与方法覆盖率。可查看当前单元测试覆盖的范围。例如输出WebDefenderTest的代码覆盖率到桌面(phpunit tests/unit/WebDefenderTest --coverage-html ~/Desktop/test)

PHPUnit + Laravel单元测试常用技能

6. 指定代码覆盖率报告要包含哪些文件

在配置文件(phpunit.xml)里设置whitelist中的processUncoveredFilesFromWhitelist=true, 设置目录用<directory>标签,设置文件用<file>标签。例如指定app/Services目录下的所有文件和app/Facades/Services/WebDefenderService.php在报告中。

示例代码:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <phpunit backupGlobals="false"
  3. backupStaticAttributes="false"
  4. bootstrap="tests/bootstrap.php"
  5. colors="true"
  6. convertErrorsToExceptions="true"
  7. convertNoticesToExceptions="true"
  8. convertWarningsToExceptions="true"
  9. processIsolation="false"
  10. stopOnFailure="false">
  11. <testsuites>
  12. <testsuite name="Unit">
  13. <directory suffix="Test.php">./tests/Unit</directory>
  14. </testsuite>
  15.  
  16. <testsuite name="Feature">
  17. <directory suffix="Test.php">./tests/Feature</directory>
  18. </testsuite>
  19. </testsuites>
  20. <filter>
  21. <whitelist processUncoveredFilesFromWhitelist="true">
  22. <directory suffix=".php">./app/Services</directory>
  23. <file>./app/Facades/Services/WebDefenderService.php</file>
  24. </whitelist>
  25. </filter>
  26. <php>
  27. <server name="APP_ENV" value="local"/>
  28. <server name="BCRYPT_ROUNDS" value="4"/>
  29. <server name="CACHE_DRIVER" value="credis"/>
  30. <server name="MAIL_DRIVER" value="array"/>
  31. <server name="QUEUE_CONNECTION" value="sync"/>
  32. <server name="SESSION_DRIVER" value="array"/>
  33. <server name="APP_CONFIG_CACHE" value="bootstrap/cache/config.phpunit.php"/>
  34. <server name="APP_SERVICES_CACHE" value="bootstrap/cache/services.phpunit.php"/>
  35. <server name="APP_PACKAGES_CACHE" value="bootstrap/cache/packages.phpunit.php"/>
  36. <server name="APP_ROUTES_CACHE" value="bootstrap/cache/routes.phpunit.php"/>
  37. <server name="APP_EVENTS_CACHE" value="bootstrap/cache/events.phpunit.php"/>
  38. </php>
  39. </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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://segmentfault.com/a/1190000020925586