2

I am trying to obtain a key from an array that Codeception is pulling back through it's REST module, more specifically, the 'grabDataFromJsonResponse' method. I would like to pull the first key from that array, as the grabDataFromJsonResponse function lets me select in far enough to only return the data I need. However, Codeception seems to convert it to an object, and thus, I get the wrong key. Below is a codesample, as well as a sample (top of) an array object that Codeception is returning:

 public function returningArrayKey(WebGuy $I)
{
    $I->sendPOST(mypostdata);
    $I->seeResponseCodeIs(200);
    $I->seeResponseContains("Success");
    $jsonListingObj = $I->grabDataFromJsonResponse("tree.traversing.traversed");
    $I->checkAgainstKey("123456789", key($jsonListingObj));

}

The function checkAgainstKey simply does an AssertEquals:

function compareListingId($listingId, $oJsonObjectData)
{
    $this->assertEquals($listingId, $oJsonObjectData);
}

However, the assertEquals will always fail, because the first key is as follows:

  Codeception\Maybe Object
  (
  [position:protected] => 0
  [val:protected] => Array
      (
          [123456] => Array
              (    etc.

Using key() as above returns 'position:protected'. How can I dig into the array and return 123456? The array key represented by 123456 will be dynamic based on the REST response.

Thanks!

4

1 回答 1

1

最终的解决方案是将对象转换为数组,对数组进行切片(因为 Codeception 的 Maybe 对象转换为数组会添加公共属性,因此我们想要剥离),然后提取所需的键:

$jsonListingObj = $I->grabDataFromJsonResponse("tree.traversing.traversed");
$jsonListingArray = (array)$jsonListingObj;
$JSONParsed = key(current(array_slice($jsonListingArray, 1,1)));

$JSONParsed 然后在上面的示例中返回“123456”。

于 2014-04-04T17:21:05.950 回答