Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Ensure a null value is passed to setCreationOptions instead of empty array #210

Conversation

weierophinney
Copy link
Member

Per #159 (comment), the updates in #205 broke a previously fixed edge case whereby AbstractPluginManager::createServiceViaCallback() would inject a null value to a factory's setCreationOptions() method if no instance options were provided; it instead passed an empty array, which broke a number of plugins that relied on a null value being present.

This patch does the following:

  • Adds a regression test for the behavior.
  • Modifies AbstractPluginManager::createServiceViaCallback() to have three distinct behaviors surrounding creation options, in the following order:
    • if the factory is an InvokableFactory, it tests if the creation options are an array or non-empty; if not, it passes a null value to setCreationOptions().
    • if the factory is an MutableCreationOptionsInterface, it tests if the creation options are an array or non-empty; if not, it passes an empty array to setCreationOptions().
    • if the factory implements the setCreationOptions() and the creation options are a non-array or non-empty, it reflects the method to determine if the options argument has a default value, using that if present, or an empty array if not.

…pty array

Per zendframework#159 (comment),
the updates in zendframework#205 broke a previously fixed edge case whereby
`AbstractPluginManager::createServiceViaCallback()` would inject a
`null` value to a factory's `setCreationOptions()` method if no instance
options were provided; it instead passed an empty array, which broke a
number of plugins that relied on a null value being present.

This patch does the following:

- Adds a regression test for the behavior.
- Modifies `AbstractPluginManager::createServiceViaCallback()` to have
  three distinct behaviors surrounding creation options, in the
  following order:
  - if the factory is an `InvokableFactory`, it tests if the creation
    options are an array or non-empty; if not, it passes a `null` value
    to `setCreationOptions()`.
  - if the factory is an `MutableCreationOptionsInterface`, it tests if
    the creation options are an array or non-empty; if not, it passes an
    empty array to `setCreationOptions()`.
  - if the factory implements the `setCreationOptions()` and the
    creation options are a non-array or non-empty, it reflects the
    method to determine if the options argument has a default value,
    using that if present, or an empty array if not.
@amcsi
Copy link

amcsi commented Dec 5, 2017

How complex :o
Thanks a lot for this quick fix. I can confirm that this fix fixed our integration tests in our projects 👍

@amcsi
Copy link

amcsi commented Dec 5, 2017

One thing I don't quite get... I thought I already wrote a unit test to cover this regression. How come it didn't catch this?
https://github.com/zendframework/zend-servicemanager/pull/164/files#diff-46506c2cbbf939781cbaa62bc7771ec8R156

@weierophinney
Copy link
Member Author

@amcsi Because the behavior with an InvokableFactory is different than that for a duck-typed factory. Oddly, I had to change the order of conditionals, as InvokableFactory fulfills the same duck-type, and I got failures once I started passing a null value for the $options with a number of our duck-typed factories (which don't have a default null value for the $options).

Anyways, there's now three issues worth of tests capturing different edge cases, so hopefully it's fixed for the rest of the 2.7 lifetime.


public function testIfCreationOptionsAreEmptySetThemThemToNullWhenCreatingAService()
{
// @codingStandardsIgnoreEnd
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weierophinney Do we need this comment above here? I can't see where is start... ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odd; that must be a copy-paste artifact from a test I used as a starting point.

No, not needed, but already released.

@weierophinney weierophinney deleted the hotfix/159-creation-options-must-be-null branch January 29, 2018 16:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants