Search code examples
phpsymfonyguzzlebehatgherkin

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.

Using Behat I want to fill a database table with multiple test objects using Rest API.

While doing a POST command, the behat-api-extension somehow loses the request body contents, so null is send instead instead and I end up creating either empty objects or recieve a code 500 error saying I can't insert null values into the REST object.

I have the following Behat feature:

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"
...

and the following function:

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.';
    }
}

...

The error I recieve is the following:

  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)

With print_r($this->request->getBody()->getContents()); in ApiContext.php I've been able to determine that the request body content for the first album is: {"title":"The Dark Side of the Moon","track_count":"12","release_date":"1973-03-24T00:00:00+00:00"} up until https://github.com/imbo/behat-api-extension/blob/develop/src/Context/ApiContext.php#L986.


Solution

  • The header has to be set before sending albums:

    album.feature

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