【问题标题】:Slim 4 Framework: Unable to pass a payload to test a POST routeSlim 4 框架:无法传递有效负载来测试 POST 路由
【发布时间】:2022-01-19 00:36:00
【问题描述】:

我对 Slim 4 完全陌生,但我成功地创建了一个项目并编写了 API 端点来进行一些计算。

这是一个POST 路由,它需要一个 JSON 负载。在 Postman 中,我将 POST 发送到 http://localhost:8089/api/discounts/calculate,其中:

{
    "order": {
        "id": "1",
        "customer-id": "1",
        "items": [
            {
                "product-id": "B102",
                "quantity": "10",
                "unit-price": "4.99",
                "total": "49.90"
            }
        ],
        "total": "49.90"
    },
    "discount_strategy": "overall_percentage_from_total"
}

在回复中我得到HTTP 200 OK,这是我所期望的。一切正常,但在 PHPUnit 中不行。

我想为此端点创建一个测试,所以我创建了扩展 TestCase 的新测试类,它可以访问这个受保护的方法:https://github.com/slimphp/Slim-Skeleton/blob/master/tests/TestCase.php#L71

所以我写了:

public function testOrder1AgainstOverallPercentageFromTotal()
{
    $app = $this->getAppInstance();

    $payload = [
        'order' => [
            'id' => 1,
            'customer-id' => 1,
            'items' => [
                'product-id' => 'B102',
                'quantity' => '10',
                'unit-price' => '4.99',
                'total' => '49.90',
            ],
            'total' => '49.90',
        ],
        'discount_strategy' => 'overall_percentage_from_total',
    ];

    $req = $this->createRequest('POST', '/api/discounts/calculate');
    $request = $req->withParsedBody($payload);

    $response = $app->handle($request);

    //var_dump($response->getBody()->getContents()); die;

    $this->assertEquals(200, $response->getStatusCode());
}

但它总是给我 HTTP 400 说:

格式错误的 JSON 输入

当我转储 getBody()getContents() 时,我得到一个空心对象或空字符串作为内容。

There was 1 failure:

1) Tests\Functional\CalculateDiscountsActionTest::testOrder1AgainstOverallPercentageFromTotal
Failed asserting that 400 matches expected 200.

我做错了什么?

我的计算逻辑位于扩展App\Application\Actions\Action 的Action 类中,我可以访问我在Postman 中发送的有效负载:$input = $this->getFormData();。这是stdClass,但足以让我抓住输入并完成工作。

为什么 PHPUnit 看不到我的有效载荷?

【问题讨论】:

    标签: php phpunit slim


    【解决方案1】:

    确保将正确的 HTTP 标头和正文内容添加到请求对象。

    $request->getBody()->write((string)json_encode($data));
    
    $request = $request->withHeader('Content-Type', 'application/json');
    

    完整示例:

    protected function createJsonRequest(string $method, $uri, array $data = null): ServerRequestInterface
    {
        $request = $this->createRequest($method, $uri);
    
        if ($data !== null) {
            $request->getBody()->write((string)json_encode($data));
        }
    
        return $request->withHeader('Content-Type', 'application/json');
    }
    

    用法:

    $payload = [];
    
    $request = $this->createJsonRequest('POST', '/api/discounts/calculate', $payload);
    
    $response = $app->handle($request);
    
    // Assert the response
    // ...
    

    More examples

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 2013-08-26
      • 2019-09-15
      • 2014-08-11
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      相关资源
      最近更新 更多