PHP:我可以在它的第一个深度上拆分 JSONstring 而不必重新编码吗?
PHP: Can I split a JSONstring on it's first depth without have to re-encode?
因为我不知道 behat 是否可以用字符串以外的任何东西注入我的 Behat FeatureContext 函数参数,所以我想知道我是否可以拆分字符串,这样我就可以得到 json_objects.
我已经设法用 json_decode
和 json_encode
做到了这一点,但是当我第一次解码对象字符串时感觉有点重复,
仅将其编码回单个对象。
每个示例具有以下 Behat 功能:
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 中的以下函数:
...
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");
}
}
...
据我了解,您想添加一些数据来设置场景,在这种情况下,我会放弃 json,因为那是一个实现细节:
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/album 控制器中使用它来创建相册。
我正在学习通过 REST 添加第一个测试对象的教程,但这并不一定会发生。
我还了解到 Behat 的嵌套表格不是一个好主意。
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
下面的代码不完全正确,但已经足够接近了。
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();
}
...
因为我不知道 behat 是否可以用字符串以外的任何东西注入我的 Behat FeatureContext 函数参数,所以我想知道我是否可以拆分字符串,这样我就可以得到 json_objects.
我已经设法用 json_decode
和 json_encode
做到了这一点,但是当我第一次解码对象字符串时感觉有点重复,
仅将其编码回单个对象。
每个示例具有以下 Behat 功能:
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 中的以下函数:
...
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");
}
}
...
据我了解,您想添加一些数据来设置场景,在这种情况下,我会放弃 json,因为那是一个实现细节:
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/album 控制器中使用它来创建相册。
我正在学习通过 REST 添加第一个测试对象的教程,但这并不一定会发生。
我还了解到 Behat 的嵌套表格不是一个好主意。
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
下面的代码不完全正确,但已经足够接近了。
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();
}
...