Skip to content

Commit 297d266

Browse files
authored
Merge pull request #7 from jakzal/putenv
Add support for putenv()
2 parents 6dedf4e + 83d3a49 commit 297d266

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ Supported annotations:
88

99
* `@env` for `$_ENV`
1010
* `@server` for `$_SERVER`
11+
* `@putenv` for [`putenv()`](http://php.net/putenv)
1112

1213
Global variables are set before each test case is executed,
1314
and brought to the original state after each test case has finished.
15+
The same applies to `putenv()`/`getenv()` calls.
1416

1517
## Installation
1618

@@ -73,6 +75,7 @@ class ExampleTest extends TestCase
7375
* @env APP_DEBUG=0
7476
* @server APP_ENV=bar
7577
* @server APP_DEBUG=1
78+
* @putenv APP_HOST=localhost
7679
*/
7780
public function test_global_variables()
7881
{
@@ -81,6 +84,7 @@ class ExampleTest extends TestCase
8184
$this->assertSame('0', $_ENV['APP_DEBUG']);
8285
$this->assertSame('bar', $_SERVER['APP_ENV']);
8386
$this->assertSame('1', $_SERVER['APP_DEBUG']);
87+
$this->assertSame('localhost', \getenv('APP_HOST'));
8488
}
8589
}
8690
```

src/AnnotationListener.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class AnnotationListener implements TestListener
1414

1515
private $server;
1616
private $env;
17+
private $getenv;
1718

1819
public function startTest(Test $test): void
1920
{
@@ -33,12 +34,20 @@ private function backupGlobals(): void
3334
{
3435
$this->server = $_SERVER;
3536
$this->env = $_ENV;
37+
$this->getenv = \getenv();
3638
}
3739

3840
private function restoreGlobals(): void
3941
{
4042
$_SERVER = $this->server;
4143
$_ENV = $this->env;
44+
45+
foreach (\array_diff_assoc($this->getenv, \getenv()) as $name => $value) {
46+
\putenv(\sprintf('%s=%s', $name, $value));
47+
}
48+
foreach (\array_diff_assoc(\getenv(), $this->getenv) as $name => $value) {
49+
\putenv($name);
50+
}
4251
}
4352

4453
private function readGlobalAnnotations(TestCase $test)
@@ -51,6 +60,9 @@ private function readGlobalAnnotations(TestCase $test)
5160
foreach ($globalVars['server'] as $name => $value) {
5261
$_SERVER[$name] = $value;
5362
}
63+
foreach ($globalVars['putenv'] as $name => $value) {
64+
\putenv(\sprintf('%s=%s', $name, $value));
65+
}
5466
}
5567

5668
private function parseGlobalAnnotations(TestCase $test): array
@@ -70,9 +82,9 @@ private function findGlobalVarAnnotations(TestCase $test): array
7082
$annotations = $test->getAnnotations();
7183

7284
return \array_filter(
73-
\array_merge_recursive(['env' => [], 'server' => []], $annotations['class'], $annotations['method']),
85+
\array_merge_recursive(['env' => [], 'server' => [], 'putenv' => []], $annotations['class'], $annotations['method']),
7486
function (string $annotationName) {
75-
return \in_array($annotationName, ['env', 'server']);
87+
return \in_array($annotationName, ['env', 'server', 'putenv']);
7688
},
7789
ARRAY_FILTER_USE_KEY
7890
);

tests/AnnotationListenerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
/**
1212
* @env APP_ENV=test
1313
* @server APP_DEBUG=0
14+
* @putenv APP_HOST=localhost
1415
*/
1516
class AnnotationListenerTest extends TestCase
1617
{
@@ -22,51 +23,62 @@ public function test_it_is_a_test_listener()
2223
/**
2324
* @env APP_ENV=test_foo
2425
* @server APP_DEBUG=1
26+
* @putenv APP_HOST=dev
2527
*/
2628
public function test_it_reads_global_variables_from_method_annotations()
2729
{
2830
$this->assertArraySubset(['APP_ENV' => 'test_foo'], $_ENV);
2931
$this->assertArraySubset(['APP_DEBUG' => '1'], $_SERVER);
32+
$this->assertArraySubset(['APP_HOST' => 'dev'], \getenv());
3033
}
3134

3235
public function test_it_reads_global_variables_from_class_annotations()
3336
{
3437
$this->assertArraySubset(['APP_ENV' => 'test'], $_ENV);
3538
$this->assertArraySubset(['APP_DEBUG' => '0'], $_SERVER);
39+
$this->assertArraySubset(['APP_HOST' => 'localhost'], \getenv());
3640
}
3741

3842
/**
3943
* @env FOO=foo
4044
* @server BAR=bar
45+
* @putenv BAZ=baz
4146
*/
4247
public function test_it_reads_additional_global_variables_from_methods()
4348
{
4449
$this->assertArraySubset(['APP_ENV' => 'test'], $_ENV);
4550
$this->assertArraySubset(['APP_DEBUG' => '0'], $_SERVER);
51+
$this->assertArraySubset(['APP_HOST' => 'localhost'], \getenv());
4652
$this->assertArraySubset(['FOO' => 'foo'], $_ENV);
4753
$this->assertArraySubset(['BAR' => 'bar'], $_SERVER);
54+
$this->assertArraySubset(['BAZ' => 'baz'], \getenv());
4855
}
4956

5057
/**
5158
* @env APP_ENV=test_foo
5259
* @env APP_ENV=test_foo_bar
5360
* @server APP_DEBUG=1
5461
* @server APP_DEBUG=2
62+
* @putenv APP_HOST=host1
63+
* @putenv APP_HOST=host2
5564
*/
5665
public function test_it_reads_the_latest_var_defined()
5766
{
5867
$this->assertArraySubset(['APP_ENV' => 'test_foo_bar'], $_ENV);
5968
$this->assertArraySubset(['APP_DEBUG' => '2'], $_SERVER);
69+
$this->assertArraySubset(['APP_HOST' => 'host2'], \getenv());
6070
}
6171

6272
/**
6373
* @env APP_ENV
6474
* @server APP_DEBUG
75+
* @putenv APP_HOST
6576
*/
6677
public function test_it_reads_empty_vars()
6778
{
6879
$this->assertArraySubset(['APP_ENV' => ''], $_ENV);
6980
$this->assertArraySubset(['APP_DEBUG' => ''], $_SERVER);
81+
$this->assertArraySubset(['APP_HOST' => ''], \getenv());
7082
}
7183

7284
public function test_it_backups_the_state()
@@ -75,9 +87,13 @@ public function test_it_backups_the_state()
7587

7688
$_ENV['FOO'] = 'env_foo';
7789
$_SERVER['BAR'] = 'server_bar';
90+
\putenv('FOO=putenv_foo');
91+
\putenv('USER=foobar');
7892

7993
$this->assertArrayHasKey('FOO', $_ENV);
8094
$this->assertArrayHasKey('BAR', $_SERVER);
95+
$this->assertSame('putenv_foo', \getenv('FOO'));
96+
$this->assertSame('foobar', \getenv('USER'));
8197
}
8298

8399
/**
@@ -87,6 +103,9 @@ public function test_it_cleans_up_after_itself()
87103
{
88104
$this->assertArrayNotHasKey('FOO', $_ENV);
89105
$this->assertArrayNotHasKey('BAR', $_SERVER);
106+
$this->assertFalse(\getenv('FOO'), 'It removes environment variables initialised in a test.');
107+
$this->assertNotSame('foobar', \getenv('USER'), 'It restores environment variables changed in a test.');
108+
$this->assertNotFalse(\getenv('USER'), 'It restores environment variables changed in a test.');
90109
}
91110

92111
public function test_it_ignores_non_standard_test_cases()

0 commit comments

Comments
 (0)