On his Medium.com blog Laravel project lead Taylor Otwell shares some of his thoughts on expressive code and real-time facades and how they make things simpler, event for testing/mocking.
Recently, I worked on some code that surfaced my most common use-case for Laravel 5.4’s “real-time” facades. If you’re not familiar with this feature, it allows you to use any of your application’s classes as a Laravel “facade” on-demand by prefixingFacades to the namespace when importing the class. This is not a feature that is littered throughout my code, but I find it occasionally provides a clean, testable approach to writing expressive object APIs.
To illustrate he uses the code from the Laravel Forge service talking about service providers (like DigitalOcean, Linode, etc) and "service" classes to contain API methods. He then shifts over to the controller to see how he'd like to access it, making a generic Provider
class with a make
method to create the instance. This has an issue, however, with testing making it very difficult. Instead he shifts over to the real-time facades and a factory where the test can more easily manually mock the method into a stub provider (example included).