承接 xpat/api-test-library 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

xpat/api-test-library

最新稳定版本:v1.0.6

Composer 安装命令:

composer require xpat/api-test-library

包简介

PHP library for testing json api endpoints.

README 文档

README

Description

This PHP library is designed for testing JSON API endpoints.

Requirements

  • PHP >= 8.3
  • ext-curl
  • Composer

Installation

To install the library, use Composer:

composer require xpat/api-test-library

Usage

To run the API tests, use the provided script. You can specify the path to the test cases as an argument. If no path is provided, it defaults to the current directory. In the example below we are using symfony dependency injection container to load the services.yaml file.

#!/usr/bin/env php
<?php
declare(strict_types=1);

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Xpat\ApiTest\Attribute\Test;
use Xpat\ApiTest\FileSystem\ClassesByPath;
use Xpat\ApiTest\FileSystem\MethodsWithAttribute;
use Xpat\ApiTest\FileSystem\MethodsWithName;
use Xpat\ApiTest\FileSystem\PublicMethodsOfClasses;
use Xpat\ApiTest\FileSystem\SubClassesOf;
use Xpat\ApiTest\Output\ResultsOutput;
use Xpat\ApiTest\Test\ApiTestMethods;
use Xpat\ApiTest\Test\TestCase;
use Xpat\ApiTest\Test\TestingResults;

require_once __DIR__ . '/../vendor/autoload.php';

$directory = __DIR__;

$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../config'));
$loader->load('services.yaml');

$arg = $argv[1] ?? $directory;

$method = explode('::', $arg)[1] ?? '';
$path = explode('::', $arg)[0] ?? $directory;

(
new ResultsOutput(
    new TestingResults(
        new ApiTestMethods(
            new MethodsWithAttribute(
                new MethodsWithName(
                    new PublicMethodsOfClasses(
                        new SubClassesOf(
                            new ClassesByPath($path),
                            TestCase::class
                        ),
                    ),
                    $method
                ),
                Test::class
            )
        ),
        $container
    )
)
)->print();

Test cases example

<?php

declare(strict_types=1);

namespace ApiTest\TestCase;

use Xpat\ApiTest\Attribute\Test;
use Xpat\ApiTest\Expectation\FieldEqualityExpectation;
use Xpat\ApiTest\Expectation\StatusCodeExpectation;
use Xpat\ApiTest\Test\ApiTest;
use Xpat\ApiTest\Test\ApiTestInterface;
use Xpat\ApiTest\Test\TestCase;

readonly class CategoryTest extends TestCase
{
    #[Test]
    public function expenseCategories(): ApiTestInterface
    {
        return new ApiTest(
            $this->requestFactory->get('/expense-categories'),
            [
                new StatusCodeExpectation(200),
                new FieldEqualityExpectation(
                    '0.name',
                    'Education'
                ),
            ]
        );
    }

    #[Test]
    public function someTest(): ApiTestInterface
    {
        return new ApiTest(
            $this->requestFactory->get('/expense-categories'),
            [
                new StatusCodeExpectation(200),
                new FieldEqualityExpectation(
                    '1.name',
                    'Grocery'
                ),
            ]
        );
    }
}

To execute the script, run:

php src/api-test /path/to/test/cases

Replace /path/to/test/cases with the actual path to your test cases. If no path is provided, it will use the current directory.

License

This library is licensed under the MIT License.

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-31