0

The functionality of the file_unmanaged_copy is exactly what I need. The only problem is: it's a global function which uses static service calls. Since I need to unit test my class (and not to mention how I dislike hidden dependencies coming from global functions) I can't use it.

Am working with D8 since the very beginning, but am failed to find any OO alternatives. Has anyone faced the same situation? Any "Best-practises" or workarounds?

  • 1
    There is no oop replacement yet. There are a number of issues to convert those function to services but it didn't happen yet. The common approach that core uses to solve this problem so to hide this function in a protected method and in the unit test, you mock your own class so that you are able to mock that method. Or you make a separate service that just wraps that method so you can mock it – Berdir Jul 20 '17 at 13:53
2

This changed in Drupal 8.7.0 See https://www.drupal.org/node/3006851

0

As for now Berdir's comment is the right answer:

There is no oop replacement yet. There are a number of issues to convert those function to services but it didn't happen yet. The common approach that core uses to solve this problem so to hide this function in a protected method and in the unit test, you mock your own class so that you are able to mock that method. Or you make a separate service that just wraps that method so you can mock it

Personally, I prefer the later one, wrapping it the globals into a service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.