Search code examples
phpbehat

PyStringNode and json string


I have the following feature, and I am trying to test the structure of an object.

  @mynet
  Scenario: Import mynet network Transactions
    Given I Import transactions for network "mynet" from "2015-09-01" to "2015-09-01"
    Then I should have 20 transactions imported
    And The first element of the transaction should be
    """
    {
            "transaction_date": "2015-09-01 19:34:17",
            "tenant_id":  1,
            "network":  "xxx",
            "network_transaction_id":  "xxxxa630514d",
            "network_merchant_name":  "xxxxx",
            "currency":  "EUR",
            "amount":  "287.40",
            "commission":  "4.31",
            "subid":  "xxxxxxxxxx",
            "enquiry_id":  0,
            "reference":  "xxxx6a630514d",
            "network_merchant_id":  "4",
            "status":  "declined",
            "network_status":  "2",
            "meta_data":  "{"short_name":"mynet"}"
    }
    """

there is something wrong with the meta_data piece. I have the feeling PyStringNode format does not convert it properly. Here is the part in my Behat Context for handling the PyStringNode

public function theFirstElementOfTheTransactionShouldBe(PyStringNode $string)
{
    $expectedFirstElement = json_decode($string->getRaw(), true);
    $realfirstelement = $this->transactions[0];
    Assert::assertEquals($realfirstelement, $expectedFirstElement);
}

and when running the behat test, I get the error

null does not match expected type "array".

when i replace meta_data with a simple string, all is well.....is there a special case when having json objects? how do I structure my scenario/feature?


Solution

  • You have extra quotes in surrounding your object. Remove them, it should work.

    Here is the corrected version (not tested):

    @mynet
      Scenario: Import mynet network Transactions
        Given I Import transactions for network "mynet" from "2015-09-01" to "2015-09-01"
        Then I should have 20 transactions imported
        And The first element of the transaction should be
        """
        {
            "transaction_date": "2015-09-01 19:34:17",
            "tenant_id":  1,
            "network":  "xxx",
            "network_transaction_id":  "xxxxa630514d",
            "network_merchant_name":  "xxxxx",
            "currency":  "EUR",
            "amount":  "287.40",
            "commission":  "4.31",
            "subid":  "xxxxxxxxxx",
            "enquiry_id":  0,
            "reference":  "xxxx6a630514d",
            "network_merchant_id":  "4",
            "status":  "declined",
            "network_status":  "2",
            "meta_data": {
                "short_name":"mynet"
            }
        }
        """