-
Notifications
You must be signed in to change notification settings - Fork 29
Adding the new "forceObject" flag #47
base: develop
Are you sure you want to change the base?
Adding the new "forceObject" flag #47
Conversation
This commit adds the new "forceObject" flag to be passed to the encoder. The implementation assumes all nested arrays to be forced to be objects.
This commit adds documentation for the new "forceObject" flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a better way to handle the tests: data providers.
You can leave the various get...()
methods in, but instead of having multiple test cases, you can have a single test case that reads:
public function forceObjectFlagRelationshipToResults() : iterable
{
$source = $this->getForceObjectTestData();
yield 'builtin-encoder, forceObject true' => [true, true, $source, $this->getExpectedForceObjectTestResult()];
yield 'custom-encoder, forceObject true' => [false, true, $source, $this->getExpectedForceObjectTestResult()];
yield 'builtin-encoder, forceObject false' => [true, false, $source, $this->getExpectedNotForcedObjectTestResult()];
yield 'custom-encoder, forceObject false' => [false, false, $source, $this->getExpectedNotForcedObjectTestResult()];
}
/**
* @dataProvider forceObjectFlagRelationshipToResults
*/
public function testForceObjectFlagImpactOnEncoding(
bool $forceObjectFlag,
bool $useBuiltinEncoderFlag,
array $source,
string $expected
) {
Json\Json::$useBuildinEncoderDecoder = $useBuiltinEncoderFlag;
$actual = Json\Json::encode($source, false, ['forceObject' => $forceObjectFlag]);
$this->assertEquals($expected, $actual);
}
For the last two tests, you'd have the following:
public function defaultForceObjectFlagResults() : iterable
{
$source = $this->getForceObjectTestData();
$expected = $this->getExpectedNotForcedObjectTestResult();
yield 'builtin encoder' => [true, $source, $expected];
yield 'custom encoder' => [false, $source, $expected];
}
/**
* @dataProvider defaultForceObjectFlagResults
*/
public function testDefaultForceObjectFlag(
bool $builtinEncoderFlag,
array $source,
string $expected
) {
Json\Json::$useBuilinEncoderDecoder = $builtinEncoderFlag;
$this->assertEquals($expected, Json\Json::encode($source));
}
Doing this makes it clear you're testing the same workflow, but with different permutations of values. Having data providers then makes it easier to identify if any permutations might be missing (as it's generally based on the possible values for each variable provided).
This repository has been closed and moved to laminas/laminas-json; a new issue has been opened at laminas/laminas-json#1. |
This repository has been moved to laminas/laminas-json. If you feel that this patch is still relevant, please re-open against that repository, and reference this issue. To re-open, we suggest the following workflow:
|
Adding the new "forceObject" flag to be passed to the encoder in the options in the manner of
['forceObject' => true,]
. This then forces the encoder output to encode to an object rather than an array. This implementation assumes all nested arrays to be forced to be objects in the manner of JSON_FORCE_OBJECT.This is the pull request for the conversation outlined in #27