deozza/api-tester-bundle

此包已被废弃且不再维护。作者建议使用 deozza/philarmony-api-tester-bundle 包。

用于测试 Symfony API 的包

安装: 147

依赖者: 1

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

3.2.0 2019-09-27 13:49 UTC

This package is auto-updated.

Last update: 2019-09-27 13:50:24 UTC


README

php mysql sqlite symfony Stable License: MIT

目录

关于

PhilarmonyApiTester 是一个用于测试您的 API 的包。它通过单元测试和场景测试确保您应用程序的质量和健壮性。

安装

您可以使用 composer 安装,假设它已经全局安装

composer require deozza/philarmony-api-tester-bundle

数据库准备

您需要为测试创建一个特定的数据库。为了确保测试在相同的环境和数据库中执行,此数据库将在每个测试结束时重置。

我们建议使用 doctrine/doctrine-fxtures-bundle 创建测试数据库。

您用于测试的数据库可以是 MySQL 或 SQLITE3。

  • 如果您使用的是 MySQL 数据库,需要将其导出为文件,命名为 demo.sql { 并将其存储在 /var/data/db_test 文件夹中。
  • 如果您使用的是 SQLITE3 数据库,需要将其命名为 demo.sqlite { 并将其存储在 /var/data/db_test 文件夹中。

出于性能考虑,我们建议您使用 SQLITE3 数据库进行测试。

如何使用

创建 ControllerTest

为了测试应用程序的功能,您需要在 test 文件夹中创建一个文件夹。在它内部,为每个与该功能相关的控制器创建一个 ControllerTest。ControllerTest 必须符合以下示例

<?php
namespace App\Tests\FooFeature;

use Deozza\PhilarmonyApiTester\Service\TestAsserter;

class FooControllerTest extends TestAsserter
{
    const TEST_DATABASE_PATH = __DIR__."/path/to/db.sql";
  public function setUp()
  {
      parent::setTestDatabasePath(self::TEST_DATABASE_PATH);
      parent::setUp();
  }

  /**
  * @dataProvider addDataProvider
  */
  public function testUnit($kind, $test)
  {
    parent::launchTestByKind($kind, $test);
  }

  public function addDataProvider()
  {
      return [];
  }
}

编写测试

测试以数组的形式编写,遵循以下结构

["kind" => "unit", "test" => ["method" => "GET", "url" => "/path/to/the/tested/route", "status" => 200, "token" => "auth_token", "in" => "json_payload", "out" => "json_expected_response]]

它们在 addDataProvider 函数中编写。

<?php

public function addDataProvider()
  {
      return
      [
        ["kind" => "unit", "test" => ['method'=> 'GET'   , 'url' => 'api/foos'                                    , 'status' => 200, 'out' => 'getAllFoos'] ],
        ["kind" => "unit", "test" => ['method'=> 'POST'  , 'url' => 'api/foos'                                    , 'token' => 'token_user', 'status' => 201, 'in' => 'postValidFoo' , 'out' => 'postedFoo'] ],
        ["kind" => "unit", "test" => ['method'=> 'PATCH' , 'url' => 'api/foo/00400000-0000-5000-a000-000000000000', 'token' => 'token_user', 'status' => 200, 'in' => 'patchValidFoo', 'out' => 'patchedFoo'] ],
        ["kind" => "unit", "test" => ['method'=> 'PUT'   , 'url' => 'api/foo/00400000-0000-5000-a000-000000000000', 'token' => 'token_user', 'status' => 405] ],
        ["kind" => "unit", "test" => ['method'=> 'DELETE', 'url' => 'api/foo/00400000-0000-5000-a000-000000000000', 'token' => 'token_user', 'status' => 204] ],
      ];
  }
描述 可选
kind 您正在执行的测试类型
test 测试内容
test.method 您要发送的请求方法
test.url 您要测试的路由
test.token 用于测试的授权令牌
test.status 预期的 HTTP 状态码
test.in 与请求一起发送的有效负载
test.out 预期的响应 Tes

目前,PhilarmonyApiTester 只处理 GET、POST、PUT、PATCH 和 DELETE 方法

不同种类的测试

使用PhilarmonyApiTester,您可以通过逐个发送请求("单元测试")或发送一组请求("场景")来测试您的应用程序。

在(有效载荷)中

您可以通过使用in选项发送特定的有效载荷并检查其反应来测试您的应用程序。

出(预期响应)

您可以通过使用out选项来检查您已发送的请求的响应。

使用模式进行测试

有时,您的应用程序发送的值难以测试,因为它们是不可预测的。使用模式将允许PhilarmonyApiTester断言这些值在特定的范围内,甚至可以操纵它们以供未来的测试使用。

文件夹结构

为了测试您的应用程序,您需要匹配以下文件夹结构

.
├── tests                                   #   The basic Symfony test directory
│   ├── Foo                                 #   Feature you need to test
│   │   │── FooControllerTest.php           #   Controller of the feature you want to test
│   │   │                                       
│   │   ├── Payloads                            
│   │   │   └── ...                             #   File posted to your API
│   │   │   
│   │   └── Responses                           #   All the expected responses coming from the endpoints of your API when testing it
│   │       └── ...
│   └──
└──

路线图

路线图中没有任何内容!