steevanb/php-url-test

测试应用程序中的所有URL

安装: 726

依赖者: 0

建议者: 0

安全: 0

星标: 3

关注者: 3

分支: 5

开放问题: 3

类型:lib

0.3.3 2020-12-09 17:40 UTC

README

version Lines Total Downloads

php-url-test

测试应用程序中的所有URL

Url test

变更日志

安装

/!\ 请注意,这是一个alpha版本 /!\

不允许更新次要/错误修复版本,因为我们可能会破坏错误修复与最终发布之间的兼容性。

composer require --dev steevanb/php-url-test 0.3.*

使用官方Docker镜像

您可以使用官方Docker镜像,而不是使用Composer在项目中安装它。

docker run \
    # Create a volume with your test configurations into /app
    -v /var/www/tests:/app \
    # You can use `URLTEST_PARAMETERS` env variable to add parameters to `urltest` command.
    -e URLTEST_PARAMETERS="--ansi --configuration=/app/urltest.yml -vvv" \
    # Allow this container to access host domains
    --net=host \
    steevanb/php-url-test:0.3.3

启动测试

# scan tests/ to find *.urltest.yml files, --recursive=false or -r=false to not do it recursively
# if urltest.yml file is found into tests/ (not in sub directories), it will be used for default configuration file
vendor/bin/urltest tests/

# test url_test_foo
vendor/bin/urltest tests/ url_test_foo

# test url_test_foo and all tests who match preg pattern /^url_test_bar[0..9]{1,}$/
vendor/bin/urltest tests/ url_test_foo,/^url_test_bar[0..9]{1,}$/

# launch tests from foo.urltest.yml only
vendor/bin/urltest tests/Tests/foo.urltest.yml

# don't use tests/urltest.yml, use another configuration file
# if you are a few developers with different domain for each developer,
# you can create a configuration file by developer and use parameters to configure it
vendor/bin/urltest tests/ --configuration=tests/foo.yml

读取测试结果并显示信息

# show only failed test comparison (by default), use -v, -vv or -vvv to get more informations
vendor/bin/urltest tests/ --reader=steevanb\\PhpUrlTest\\ResultReader\\ConsoleResultReader#error

# show only passed test comparison, use -v, -vv or -vvv to get more informations
vendor/bin/urltest tests/ --reader=steevanb\\PhpUrlTest\\ResultReader\\ConsoleResultReader#success

您可以通过实现 steevanb\PhpUrlTest\ResultReader\ResultReaderInterface 创建自己的ResultReader。

然后,您可以像使用ConsoleResultReader一样使用它,使用 --reader 参数。

您可以使用 , 分隔读取器。

vendor/bin/urltest tests/ --reader=steevanb\\PhpUrlTest\\ResultReader\\ConsoleResultReader#error,Foo\\Bar#success,Foo\\Baz

在错误时停止并恢复测试

您有3个参数可以在测试失败时停止测试,并从失败的地方恢复测试,或者跳过并继续此之后

# stop when a test fail
vendor/bin/urltest tests/ --stop-on-error

# when a test fail, continue testing since the one who fail (do not re-test previous ones)
vendor/bin/urltest tests/ --stop-on-error --continue

# used with --continue, skip last fail test, and continue testing after this one (do not re-test previous ones)
vendor/bin/urltest tests/ --skip

## 更改UrlTest可以写入文件的目录

vendor/bin/urltest tests/ --var-path=/foo

转储配置

# dump only global configuration
vendor/bin/urltest --dump-configuration tests/

# dump global configuration, and url_test_foo configuration
vendor/bin/urltest --dump-configuration tests/ url_test_foo

# dump global configuration, url_test_foo configuration and all configurations who id match preg pattern /^url_test_bar[0..9]{1,}$/
vendor/bin/urltest --dump-configuration tests/ url_test_foo,/^url_test_bar[0..9]{1,}$/

YAML测试文件示例

仅需要 request.url

testId:
    # If abstract = true, this test will not be launched. you can use it as default configuration with parent: testId in another test
    abstract: false
    # Id of parent default configuration
    parent: ~
    # 0 is first. don't use negative numbers, it's used by UrlTest
    position: 0
    events:
        # Commands called before the test. it could be a string (for only one command) or an array of commands.
        beforeTest:
            - command
        # Commands called after the test. it could be a string (for only one command) or an array of commands.
        afterTest:
            - commands
    request:
        # You can use parameters (see above) to configure what you need
        url: '%domain%/foo'
        timeout: 30
        port: 80
        method: GET
        userAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
        referer: http://referer.com
        allowRedirect: true
        # List of headers to add to request
        headers:
            X-Foo: bar
    expectedResponse:
        url: http://test.dev
        code: 200
        size: 300
        contentType: text/html
        numConnects: 1
        # Set count if you know exaclty number of redirects you want to test, or min/max
        redirect:
            min: 1
            max: 1
            count: 1
        header:
            size: 200
            # List of headers who has to exists, and have exaclty this value
            headers:
                X-Foo: bar
            # List of headers should not exists
            unallowedHeaders:
                - X-Bar
        body:
            # Content to compare with response, <file($fileName)> will get content of $fileName
            content: <file('content.html')>
            size: 100
            # Transformer id : transform data from content key before comparing it to response
            transformer: json
            # File name where tranformed expected content will be saved, if you need to test your transformer for example
            fileName: /tmp/urlTestResult/expectedResponse.html
    response:
        body:
            # Transformer id : transform data from response body before comparing it to expected response
            transformer: json
            # File name where response body will be saved
            fileName: /tmp/urlTestResult/response.html

您可以在 .urltest.yml 文件中定义所有测试的默认配置。

_defaults:
    # here you can define sames configurations as for a test
    # this configurations will be applied to all tests in this file, if value is not defined, null or ~

ResponseBodyTransformer

ResponseBodyTransformer将在两个不同的步骤中修改响应体

  • expectedResponse.body.transformer: 转换预期响应
  • response.body.transformer: 转换响应

可用的转换器列表

  • json: 尝试解码和重新编码值,如果数据不是有效的JSON,将转换响应为null
  • uuid: 尝试解码响应(应该是有效的JSON)并将所有UUID值替换为____UUID____

创建自己的ResponseBodyTransformer

要创建自己的ResponseBodyTransformer,您必须执行以下步骤

  • 创建一个实现 steevanb\PhpUrlTest\ResponseBodyTransformer\ResponseBodyTransformerInterface 的类
  • 使用 UrlTest::addResponseBodyTransformer() 注册您的ResponseBodyTransformer

全局配置文件

您可以在 urltest.yml 中定义全局配置。

这些配置将应用于所有测试。

# you can define tests here, or abstract tests to use it in all your tests
urltest:
    abstractTestId:
        abstract: true
        url: http://test.dev

# parameters can be used in almost all urltest configurations
# define it's value here, and use it with %parameterName% in your configuration
parameters:
    domain: 'http://foo.local'