首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我可以在JSONstring的第一个深度拆分,而不需要重新编码吗?

我可以在JSONstring的第一个深度拆分,而不需要重新编码吗?
EN

Stack Overflow用户
提问于 2018-06-08 21:22:57
回答 2查看 106关注 0票数 0

因为我不知道behat是否可以使用字符串以外的任何参数来注入Behat FeatureContext函数参数,所以我想知道是否可以以这样一种方式拆分字符串,使我只剩下一个json_objects数组。

我已经用json_decodejson_encode做到了这一点,但这感觉有点重复,因为我首先对对象字符串进行解码,然后将其编码回单个对象。

每个示例都具有以下Behat功能:

代码语言:javascript
复制
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:
      """
      [{
        "artist":"Pink Floyd",
        "title":"The Dark Side of the Moon",
        "songs":[
          {"title":"Speak to Me", "length":254}
          {"title":"Breathe", "length":192}
          {"title":"On the Run", "length":188}
        ] 
      },
      {
        "artist":"AC/DC",
        "title":"Back to Black",
        "songs":[
          {"title":"Hells Bells", "length":205}
          {"title":"Shoot to Thrill", "length":302}
          {"title":"What Do You Do for Money Honey", "length":244}
        ] 
      }]    
      """
      And the "Content-Type" request header is "application/json"

以及FeatureContext.php中的以下函数:

代码语言:javascript
复制
...

public function thereAreAlbumsWithTheFollowingDetails(string $jsonString) {
    $albums = json_decode($jsonString, true);

    foreach ($albums as $album) {
        $albumJson = json_encode($album);
        $this->apiContext->setRequestBody($albumJson);
        $this->apiContext->requestPath("/api/album", "POST");
    }
}

...
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-25 21:59:07

我遵循了一个教程,该教程通过REST添加了第一个测试对象,但这并不是必须发生的。

我还了解到,Behat的嵌套表不是一个好主意。

https://joebuschmann.com/specflow-nested-tables-a-bad-idea/

behat.yml

代码语言:javascript
复制
default:
    suites:
        default:
            contexts:
                - App\Features\Bootstrap\FeatureContext:
                    container: '@service_container'
                    entityManager: "@doctrine.orm.default_entity_manager"
                - Imbo\BehatApiExtension\Context\ApiContext
    extensions:
        Imbo\BehatApiExtension:
            apiClient:
                base_uri: http://127.0.0.1:8000
        Behat\Symfony2Extension:
            kernel:
                bootstrap: config\behat\bootstrap.php
                class: App\Kernel

下面的代码并不完全正确,但已经足够接近了。

FeatureContext.php

代码语言:javascript
复制
...

/**
     * @Given there are Albums with the following details:
     */
    public function thereAreAlbumsWithTheFollowingDetails(TableNode $table) {
        foreach ($table->getColumnsHash() as $row) {
            $album = new Album();
            $album->setTitle($row['title']);
            $album->setReleaseDate(new \DateTime($row['release_date']));
            array_push($this->entities, $album);
        }
        $this->em->flush();
    }

    /**
     * @Given albums with the following Songs:
     */
    public function albumsWithTheFollowingSongs(TableNode $table) {
        $i = 0;
        foreach ($table->getColumnsHash() as $row) {
            $song = new Song($row['title']);
            $this->entities[$i]->setSong($song);
            $i = $i + 1;
        }
    }

    /**
     * @When they are saved into the database
     */
    public function theyAreSavedIntoTheDatabase() {
        foreach ($this->entities as $entity) {
            $this->em->persist($entity);
        }
        $this->em->flush();
    }
...
票数 0
EN

Stack Overflow用户

发布于 2018-06-09 05:49:44

据我所知,您希望添加一些数据来设置场景,在这种情况下,我将不再使用json,因为这是一个实现细节:

代码语言:javascript
复制
Given there are Albums with the following details:
  | artist     | title                     | songs                            |
  | Pink Floyd | The Dark Side of the Moon | Speak to Me, Breathe, On the Run |
  | AC/DC      | Back to Black             | Hells Bells, Shoot to Thrill, What Do You Do for Money Honey |
...

然后在FeatureContext上将数据转换为json (如果需要的话),但就我个人而言,如果您这样做是正确的,那么我将只注入应该在/api/ Albums控制器中用来创建专辑的相同服务。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50761612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档