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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化