后台场景Symfony FOSTRestBundle Rest Behat/guzzle issue,Code 500,发送时request body变为null

Background scenario Symfony FOSTRestBundle Rest Behat/guzzle issue, Code 500, request body becomes null when sending

I had a similar issue named FeatureContext Behat issue, Code 400 Bad request, validation returning “This value should not be blank.” which was solved with the same answer.

使用 Behat 我想使用 Rest API.

用多个测试对象填充数据库 table 在执行 POST 命令时,behat-api-extension 以某种方式丢失了请求正文内容,因此改为发送 null 并且我最终创建了空对象或收到代码 500 错误说我可以't 将空值插入 REST 对象。

我有以下 Behat 功能:

album.feature

Feature: Provide a consistent standard JSON API endpoint

  In order to build interchangeable front ends
  As a JSON API developer
  I need to allow Create, Read, Update, and Delete functionality

  Background:
    Given there are Albums with the following details:
      | title                              | track_count | release_date              |
      | The Dark Side of the Moon          | 12          | 1973-03-24T00:00:00+00:00 |
      | Back in Black                      | 9           | 1980-06-25T23:22:21+00:00 |
      | Thriller                           | 23          | 1982-11-30T11:10:09+00:00 |
    And the "Content-Type" request header is "application/json"
...

和以下函数:

FeatureContext.php

...

/**
 * @Given there are Albums with the following details:
 */
public function thereAreAlbumsWithTheFollowingDetails(TableNode $albums) {

    foreach ($albums->getColumnsHash() as $album) {
        $this->apiContext->setRequestBody(json_encode($album));
        $this->apiContext->requestPath("/api/album", "POST");
        $expectedResult = ["{",'"    status": "ok",',"}"];
        $this->apiContext->assertResponseBodyIs(new \Behat\Gherkin\Node\PyStringNode($expectedResult,0));
          echo 'passed.';
    }
}

...

我收到的错误如下:

  Expected response body "{
  "    status": "ok",
  }", got "{
      "code": 500,
      "message": "Unexpected error occured: An exception occurred while executing 
      'INSERT INTO Album (title, release_date, track_count) 
      VALUES (?, ?, ?)' with params [null, null, null]:\n\nSQLSTATE [23000, 515]: 
      [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]
      Cannot insert the value NULL into column 'title', table 'LUNCH_QR_TEST.dbo.Album'; 
      column does not allow nulls. INSERT fails.\n
      SQLSTATE [01000, 3621]: [Microsoft][ODBC Driver 11 for SQL Server]
      [SQL Server]The statement has been terminated."}".             
      (Imbo\BehatApiExtension\Exception\AssertionFailedException)

ApiContext.php 中使用 print_r($this->request->getBody()->getContents()); 我已经能够确定第一张专辑的请求正文内容是: {"title":"The Dark Side of the Moon","track_count":"12","release_date":"1973-03-24T00:00:00+00:00"} 直到 https://github.com/imbo/behat-api-extension/blob/develop/src/Context/ApiContext.php#L986

在发送相册之前必须设置header:

album.feature

  ...
  Background:
    Given the "Content-Type" request header is "application/json"
    And there are Albums with the following details:
  ...