dougu5886 2018-06-08 13:22
浏览 76
已采纳

PHP:我可以在它的第一个深度上拆分JSONstring而不必重新编码吗?

As I'm not aware if behat can inject my Behat FeatureContext function parameters with anything but strings, I'd like to know if I could split strings in such a way that I am left with an array of json_objects.

I've managed to do this with json_decode and json_encode, but that feels a bit repetitive as I first decode the string of objects, only to encode it back into a single object.

Per example have the following Behat 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:
      """
      [{
        "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"

and the following function in FeatureContext.php:

...

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");
    }
}

...
  • 写回答

2条回答 默认 最新

  • duanmanmian7589 2018-06-25 13:59
    关注

    I was following a tutorial that added the first test objects via REST, but that doesn't have to happen.

    I have also learned that nested tables for Behat are a bad idea.

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

    behat.yml

    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
    

    Code below is not exactly correct, but it's close enough.

    FeatureContext.php

    ...
    
    /**
         * @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();
        }
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据