1-git/yii2-debug-api-analyser

yii2的扩展。返回从yii2调试中进行的数据库请求分析json。

dev-master 2018-12-06 22:59 UTC

This package is auto-updated.

Last update: 2024-09-18 06:02:41 UTC


README

您可以使用调试模块在yii2中分析您的查询。但是,如果您有很多测试URL,这个库可以帮助您自动完成这项工作。

为了测试您的页面,请执行以下操作

1 激活yii2调试模块 查看这里。为了检查它是否正常工作,请转到以下URL

[YOUR_SITE]/debug

2 在配置中添加以下参数

'controllerMap' => [
    'apis' => 'oneGit\yii2DebugApiAnalyser\base\controllers\ApisController',
],

example of backend/config/main-local.php

return [
    'modules' => [
        'debug' => [
            'class' => 'yii\debug\Module',
            'allowedIPs' => ['*'],
            'controllerMap' => [
                'apis' => 'oneGit\yii2DebugApiAnalyser\base\controllers\ApisController',
            ],
        ],
    ],
],

2 安装phpunit并创建包含工作简单测试的测试文件夹。如果您还没有它-可以在互联网上找到

3 添加以下3个文件

3.1 创建带有您命名空间和URLs的函数的 UrlHandler.php(请参考下方的 [[YOUR DOMAIN]]

<?php

namespace tests\phpunit\apitest;

use oneGit\yii2DebugApiAnalyser\base\UrlBaseHandler;

/**
 * Class UrlHandler
 * @package tests\phpunit\apitest
 */
class UrlHandler extends UrlBaseHandler
{
    /**
     * @inheritdoc
     */
    public function getApiUrl(string $path): string
    {
        return "http://api.[[YOUR DOMAIN]]/{$path}";
    }

    /**
     * @inheritdoc
     */
    public function getTestStatUrl(string $path): string
    {
        return "http://admin.[[YOUR DOMAIN]]/debug/apis/test?path={$path}";
    }

    /**
     * @inheritdoc
     */
    public function getDebugUrl(string $tag): string
    {
        return "http://admin.[[YOUR DOMAIN]]/debug/default/view?tag={$tag}&panel=db";
    }
}

3.2 创建带有您命名空间的测试文件 ApiTest.php

<?php

namespace tests\phpunit\apitest;

use oneGit\yii2DebugApiAnalyser\base\ApiTest as BaseApiTest;

/**
 * Class ApiTest
 * @package tests\phpunit\apitest
 */
class ApiTest extends BaseApiTest
{
    /**
     * @inheritdoc
     */
    public static function setUpBeforeClass()
    {
        self::$urlHandler = new UrlHandler();
        parent::setUpBeforeClass();
    }
    
    /**
     * @inheritdoc
     */
    public function urlDataProvider(): array
    {
        return (new ListHandler)->getList();
    }
}

3.3 创建带有您命名空间和路径的文件 ListHandler.php(或其它名称)(请参考下方的 [[YOUR PAGES]]

<?php

namespace tests\phpunit\apictest;

use oneGit\yii2DebugApiAnalyser\base\UrlBaseObject;

/**
 * Class ListHandler
 * @package tests\phpunit\apictest
 */
class ListHandler extends UrlBaseObject
{
    /**
     * @inheritdoc
     */
    public function getList(): array
    {
        return [
            //[[YOUR PAGES]],
            'default/index',
            'office',
            'default/list?type=1',
        ];
    }
}

分析您扩展的类 *UrlBaseObject 中的常量。您可以通过更改子类 ListHandler 中的常量来配置测试。UrlBaseObject 有以下常量

SUCCESS_STATUS = 1;
MAX_TOTAL_QUERIES_COUNT = 30;
MAX_TOTAL_QUERIES_TIME = 150;
MAX_TOTAL_DUPLICATES = 6;
MAX_QUERY_TIME = 30;
MAX_QUERY_DUPLICATE = 6;

4 使用命令phpunit运行phpuit测试

5 分析结果(以下为主要指标)

https://phpunit.readthedocs.io/ru/latest/textui.html

6 如果您有超过50个测试URL,请将以下行添加到调试设置中

  'historySize' => [[number, no les than number of urls]] //(default value- 50)

设置示例

return [
    'modules' => [
        'debug' => [
            'class' => 'yii\debug\Module**UrlBaseObject*',
            'controllerMap' => [
                'apis' => 'oneGit\yii2DebugApiAnalyser\base\controllers\ApisController',
            ],
            'historySize' => 100,
        ],
    ],
],

在您想通过phpunit错误消息中的链接打开带有查询信息的调试页面时,这很重要。默认情况下,调试模块只保存最新的50个页面。如果您的测试有100个页面,并且在第25个页面发生错误,您将无法在调试模块中看到信息页面。在这种情况下,您只能看到从第51个到第100个的页面。因此,您可以修改 historySize 参数以保存更多页面