当前位置 主页 > 服务器问题 > win服务器问题汇总 >

    PHPUnit + Laravel单元测试常用技能

    栏目:win服务器问题汇总 时间:2019-12-07 10:08

    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]
          ];
        }
      }