Respect\Env is a tool for wrapping up native PHP functions and resources. It should be very useful to improve testability and modularity on code that uses functions like getenv, shell_exec, filter_input and so on by mocking these functions behaviors on test conditions.
Decoupling these functionality from your code is as easy as importing and prefixing some namespaces in strategic locations, and Respect\Env is smart enough to keep state when mocking variables (ex. getenv, putenv and filter_var(INPUT_ENV)).
You can also go Wrapper::evil($ns) to eval() functions without prefixing anything, but thats just wrong and we took measures to allow that only in test environments (PHPUnit loaded).
The current supported functions are:
-getenv
-putenv
-shell_exec
-sys_get_temp_dir
-filter_input
-filter_input_array
-filter_has_var
Roadmap:
-getopt
-passthru
-exec
-system
-file_get_contents
-file_put_contents
The following code has a testability problem: It must run as root, but running tests on root is not advisable. The following code is a kind of mock/stub:
Class file:
<?phpnamespace MyNS;
use Respect\Env;
use Respect\Env\Wrapper;
Wrapper::getCurrent();class MyClass
{
public static function hasPermissions()
{
return Env\shell_exec(‘whoami’) == ‘root’;
}
}
Test file:
namespace MyNS;
use Respect\Env\Wrapper;
class MyClassTest extends \PHPUnit_Framework_TestCase
{
public function testFoobar()
{
Wrapper::set(“custom”);
Wrapper::getCurrent()→setShellCallback(
function() { return ‘root’;}
);
$this→assertTrue(MyClass::hasPermissions());
}
}
Class file:
<?phpnamespace MyNS;
Respect\Env\Wrapper::evil(‘MyNS’);
class MyClass
{
public static function hasPermissions()
{
return shell_exec(‘whoami’) == ‘root’;
}
}