Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Adding the new "forceObject" flag #47

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

JohnWhitleyEkkoSense
Copy link

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

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.
@michalbundyra michalbundyra added this to the 3.2.0 milestone Sep 23, 2019
@michalbundyra michalbundyra changed the title Develop Adding the new "forceObject" Sep 23, 2019
@michalbundyra michalbundyra changed the title Adding the new "forceObject" Adding the new "forceObject" flag Sep 23, 2019
Copy link
Member

@weierophinney weierophinney left a 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).

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas/laminas-json; a new issue has been opened at laminas/laminas-json#1.

@weierophinney
Copy link
Member

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:

  • Squash all commits in your branch (git rebase -i origin/{branch})
  • Make a note of all changed files (`git diff --name-only origin/{branch}...HEAD
  • Run the laminas/laminas-migration tool on the code.
  • Clone laminas/laminas-json to another directory.
  • Copy the files from the second bullet point to the clone of laminas/laminas-json.
  • In your clone of laminas/laminas-json, commit the files, push to your fork, and open the new PR.
    We will be providing tooling via laminas/laminas-migration soon to help automate the process.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants