Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alganet/env_deprecated

Lightweight, transparent, testable and awesome enviroment wrapper
https://github.com/alganet/env_deprecated

Last synced: about 1 month ago
JSON representation

Lightweight, transparent, testable and awesome enviroment wrapper

Awesome Lists containing this project

README

        

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

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

h2. 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:

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

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

Class file: