Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nondeterminism in OptionalMethodCallProcessor #1231

Merged
merged 1 commit into from
Dec 23, 2024

Conversation

brainlock
Copy link
Contributor

An optional method call (__calls : [ setLocation (80%?): [40.689269, -74.044737] ]) would be resolved using an external source of randomness, rather than the FakerGenerator instance used by all other value generators, resulting in nondeterministic behavior.

We're making the generator an optional parameter to OptionalMethodCallProcessor and falling back to random_int for backwards-compatibility. This can be made non nullable in a next major release.

(Also see OptionalValueResolver, this is handled in the same way there

// TODO: keeping mt_rand for BC purposes. The generator should be made
// non-nullable in 4.x and mt_rand usage removed
$random = null !== $this->faker ? $this->faker->numberBetween(0, 99) : random_int(0, 99);
)

Fixes gh-1230

An optional method call (`__calls : [ setLocation (80%?): [40.689269,
-74.044737] ]`) would be resolved using an external source of
randomness, rather than the FakerGenerator instance used by all other
value generators, resulting in nondeterministic behavior.

We're making the generator an optional parameter to
OptionalMethodCallProcessor and falling back to random_int for
backwards-compatibility. This can be made non nullable in a next major
release.

(Also see OptionalValueResolver, this is handled in the same way there
https://github.com/nelmio/alice/blob/a7e15b08a64d4f6fb64b4c3b58bd6a710f71050e/src/Generator/Resolver/Value/Chainable/OptionalValueResolver.php#L101-L103)

Fixes nelmiogh-1230
@brainlock brainlock force-pushed the fix-nondet-optional-calls-bc branch from 6ff527a to c55be85 Compare December 20, 2024 07:40
@theofidry theofidry merged commit 40b240d into nelmio:main Dec 23, 2024
11 checks passed
@theofidry
Copy link
Member

Thank you @brainlock!

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

Successfully merging this pull request may close these issues.

Nondeterministic optional method calls
2 participants