Question

In a test, is there a way to temporarily override what Environment::get() returns in a class that extends StaticObject?

I'm writing a feature toggle plugin for Lithium. I want to test switching features on and off based on the environment. For example, the feature new_ui should be on in staging but off in production.

In my test I want to be able to do something like this:

Features::add('new_ui', array('production' => false, 'staging' => true));
// Magic that makes Environment::get() in the Features class return 'staging'
$this->assertTrue(Feature::check('new_ui'));
// Magic that makes Environment::get() in the Features class return 'production'
$this->assertFalse(Feature::check('new_ui'));

I could probably hack something together using a MockEnvironment but is there a more pure Lithium way to achieve this?

Was it helpful?

Solution

If you're writing a test, mocks would be the correct way to go, or else an injectable closure that's used to return the environment state.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top