Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

About

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).

Support

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

Usage (prefixed)

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:


<?php

namespace 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());
}
}

Evil usage (no prefix, works only when PHPUnit is loaded)

Class file:


<?php

namespace MyNS;

Respect\Env\Wrapper::evil(‘MyNS’);

class MyClass
{
public static function hasPermissions()
{
return shell_exec(‘whoami’) == ‘root’;
}
}

About

Lightweight, transparent, testable and awesome enviroment wrapper

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages