deozza/philarmony-api-tester-bundle

用于测试 Symfony API 的包

安装: 113

依赖: 1

建议者: 1

安全: 0

星星: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

3.2.0 2019-09-27 13:49 UTC

This package is auto-updated.

Last update: 2024-09-28 02:14:10 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 文件夹中创建一个文件夹。在其中为每个相关的 Controller 创建一个 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] ],
      ];
  }

目前,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
│   │       └── ...
│   └──
└──

路线图

路线图中没有内容!