phpunit laravel 单元测试

目录

下载PHPUnit

官方链接

根据自己PHP版本下载对应的PHPUnit


laravel

Laravel 默认就支持用 PHPUnit 来做测试,并为你的应用程序配置好了 phpunit.xml 文件。框架还提供了一些便利的辅助函数,让你可以更直观的测试你的应用程序。

可以使用 Artisan 命令 make:test 创建一个测试用例:

// 在 Feature 目录下创建一个测试类...
php artisan make:test UserTest

// 在 Unit 目录下创建一个测试类...
php artisan make:test UserTest --unit


单元测试案例
class AssetTest extends TestCase
{
    use ESQueryTrait;

    const DATA = [
        'ip_exact'    => '66.1.77.76',
        'ip_fuzzy'    => '66.1.77',
        'ip_error'    => '66.1.77.259',
        'os'          => 'linux',
        'web_title'   => 'Miss',
        'web_header'  => '200 OK',
        'web_body'    => 'consectetur quia',
        'label'       => '远程管理,WEB服务器',
        'factory'     => 'cisco,redhat',
        'probe_id'    => '1,2',
        'category_id' => 'DB',
    ];

    /**
     * A basic test example.
     *
     * @return void
     */
    public function testAssetIndex()
    {
        $response = $this->get('/api/asset/index');

        $response->assertJson(['code' => 200]);
    }

    public function testAssetIndexIPExact()
    {
        $data     = Arr::get(self::DATA, 'ip_exact');
        $query    = ['ip' => $data];
        $q        = http_build_query($query);
        $response = $this->get('/api/asset/index' . "?{$q}");
        $list     = $response->json('data.list');
        $this->assertGreaterThanOrEqual(1, count($list));
        foreach ((array)$list as $item) {
            $this->assertStringContainsString($query['ip'], $item['ip']);
        }

    }

    public function testAssetIndexIPFuzzy()
    {

        $data     = Arr::get(self::DATA, 'ip_fuzzy');
        $query    = ['ip' => $data];
        $q        = http_build_query($query);
        $response = $this->get('/api/asset/index' . "?{$q}");
        $list     = $response->json('data.list');
        $this->assertGreaterThanOrEqual(1, count($list));
        foreach ((array)$list as $item) {
            $this->assertStringContainsString($query['ip'], $item['ip']);
        }

    }

    public function testAssetIndexIPNotExist()
    {

        $data     = Arr::get(self::DATA, 'ip_error');
        $query    = ['ip' => $data];
        $q        = http_build_query($query);
        $response = $this->get('/api/asset/index' . "?{$q}");
        $list     = $response->json('data.list');
        $this->assertEquals(0, count($list));
    }

    public function testAssetIndexBanner()
    {

        $query    = ['has_banner' => 1];
        $q        = http_build_query($query);
        $response = $this->get('/api/asset/index' . "?{$q}");
        $total    = $response->json('data.total');
        $src      = ES::Client()->count($this->newQuery()->setIndex(Asset::INDEX)->setType(Asset::TYPE)->mustTermQuery('has_banner',
            1)->getQuery());
        $this->assertEquals($src['count'], $total);
    }

    public function testAssetIndexPort()
    {

        $query    = ['port' => '80,22,356,8848'];
        $q        = http_build_query($query);
        $response = $this->get('/api/asset/index' . "?{$q}");
        $total    = $response->json('data.total');

        $es_query         = [
            'index' => Asset::INDEX,
            'type'  => Asset::TYPE,
        ];
        $raw              = <<<EOF
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "ports",
            "query": {
              "terms": {
                "ports.port": [
                  80,
                  22,
                  356,
                  8848
                ]
              }
            }
          }
        }
      ],
      "must_not": []
    }
  }
}
EOF;
        $es_query['body'] = json_decode($raw, true);
        $src              = ES::Client()->count($es_query);
        $this->assertEquals($src['count'], $total);
    }

    public function testAssetIndexDate()
    {

        $query    = ['date_start' => '2015-01-02 00:00:00', 'date_end' => '2015-12-09 00:00:00'];
        $q        = http_build_query($query);
        $response = $this->get('/api/asset/index' . "?{$q}");
        $total    = $response->json('data.total');

        $es_query         = [
            'index' => Asset::INDEX,
            'type'  => Asset::TYPE,
        ];
        $raw              = <<<EOF
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "updated_at": {
              "gte": "2015-01-02 00:00:00",
              "lt": "2015-12-09 00:00:00"
            }
          }
        }
      ],
      "must_not": []
    }
  }
}
EOF;
        $es_query['body'] = json_decode($raw, true);
        $src              = ES::Client()->count($es_query);
        $this->assertEquals($src['count'], $total);
    }

    ....
}

执行用例
root@7b7bc5d87548:/wwwroot/zctc_asset_gjdw# ../phpunit-8.5.8.phar
PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

.......................                                           23 / 23 (100%)

Time: 6.64 seconds, Memory: 32.00 MB

OK (23 tests, 27 assertions)