Skip to content

Commit 39eb431

Browse files
committed
fix: Fix env pollution in tests
Also get rid of prophecy in new tests and make sure test files follow code style
1 parent 4a58b89 commit 39eb431

3 files changed

Lines changed: 36 additions & 30 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor
22
/composer.lock
33
/.php-cs-fixer.cache
4+
/.phpunit.result.cache

.php-cs-fixer.dist.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
// Define in which folders to search and which folders to exclude
66
// Exclude some directories that are excluded by Git anyways to speed up the sniffing
77
$finder = PhpCsFixer\Finder::create()
8-
->in(__DIR__ . '/src/');
8+
->in(__DIR__ . '/src/')
9+
->in(__DIR__ . '/tests/')
10+
;
911

1012
$configFinder = PhpCsFixer\Finder::create()
1113
->in(__DIR__)

tests/Unit/IncludeFileTest.php

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Helhum\DotEnvConnector\Config;
77
use Helhum\DotEnvConnector\IncludeFile;
88
use PHPUnit\Framework\TestCase;
9-
use Prophecy\Prophet;
109

1110
class IncludeFileTest extends TestCase
1211
{
@@ -19,6 +18,9 @@ protected function tearDown(): void
1918
}
2019
putenv('FOO');
2120
putenv('APP_ENV');
21+
putenv('DOTENV_CONNECTOR_OVERRIDE');
22+
unset($_ENV['FOO'], $_ENV['APP_ENV'], $_ENV['DOTENV_CONNECTOR_OVERRIDE'], $_ENV['SYMFONY_DOTENV_VARS']);
23+
unset($_SERVER['FOO'], $_SERVER['APP_ENV'], $_SERVER['DOTENV_CONNECTOR_OVERRIDE'], $_SERVER['SYMFONY_DOTENV_VARS']);
2224
if (file_exists(__DIR__ . '/Fixtures/foo')) {
2325
chmod(__DIR__ . '/Fixtures/foo', 777);
2426
rmdir(__DIR__ . '/Fixtures/foo');
@@ -28,14 +30,13 @@ protected function tearDown(): void
2830
/**
2931
* @test
3032
*/
31-
public function dumpDumpsFile()
33+
public function dumpDumpsFile(): void
3234
{
3335
$config = new Config();
3436
$config->merge(['extra' => ['helhum/dotenv-connector' => [
3537
'env-file' => __DIR__ . '/Fixtures/env/.env',
3638
'adapter' => SymfonyDotEnv::class
3739
]]]);
38-
/** @var ClassLoader|\PHPUnit\Framework\MockObject\MockObject $loaderMock */
3940
$loaderMock = $this->createMock(ClassLoader::class);
4041
$loaderMock->expects($this->once())->method('register');
4142
$loaderMock->expects($this->once())->method('unregister');
@@ -49,14 +50,13 @@ public function dumpDumpsFile()
4950
/**
5051
* @test
5152
*/
52-
public function includingFileExposesEnvVars()
53+
public function includingFileExposesEnvVars(): void
5354
{
5455
$config = new Config();
5556
$config->merge(['extra' => ['helhum/dotenv-connector' => [
5657
'env-file' => __DIR__ . '/Fixtures/env/.env',
5758
'adapter' => SymfonyDotEnv::class
5859
]]]);
59-
/** @var ClassLoader|\PHPUnit\Framework\MockObject\MockObject $loaderMock */
6060
$loaderMock = $this->createMock(ClassLoader::class);
6161
$loaderMock->expects($this->once())->method('register');
6262
$loaderMock->expects($this->once())->method('unregister');
@@ -72,15 +72,14 @@ public function includingFileExposesEnvVars()
7272
/**
7373
* @test
7474
*/
75-
public function includingFileDoesNothingIfEnvVarSet()
75+
public function includingFileDoesNothingIfEnvVarSet(): void
7676
{
7777
putenv('APP_ENV=1');
7878
$config = new Config();
7979
$config->merge(['extra' => ['helhum/dotenv-connector' => [
8080
'env-file' => __DIR__ . '/Fixtures/env/.env',
8181
'adapter' => SymfonyDotEnv::class
8282
]]]);
83-
/** @var ClassLoader|\PHPUnit\Framework\MockObject\MockObject $loaderMock */
8483
$loaderMock = $this->createMock(ClassLoader::class);
8584
$loaderMock->expects($this->once())->method('register');
8685
$loaderMock->expects($this->once())->method('unregister');
@@ -96,14 +95,13 @@ public function includingFileDoesNothingIfEnvVarSet()
9695
/**
9796
* @test
9897
*/
99-
public function includingFileDoesNothingIfEnvFileDoesNotExist()
98+
public function includingFileDoesNothingIfEnvFileDoesNotExist(): void
10099
{
101100
$config = new Config();
102101
$config->merge(['extra' => ['helhum/dotenv-connector' => [
103102
'env-file' => __DIR__ . '/Fixtures/env/.no-env',
104103
'adapter' => SymfonyDotEnv::class
105104
]]]);
106-
/** @var ClassLoader|\PHPUnit\Framework\MockObject\MockObject $loaderMock */
107105
$loaderMock = $this->createMock(ClassLoader::class);
108106
$loaderMock->expects($this->once())->method('register');
109107
$loaderMock->expects($this->once())->method('unregister');
@@ -119,14 +117,13 @@ public function includingFileDoesNothingIfEnvFileDoesNotExist()
119117
/**
120118
* @test
121119
*/
122-
public function dumpReturnsFalseIfFileCannotBeWritten()
120+
public function dumpReturnsFalseIfFileCannotBeWritten(): void
123121
{
124122
$config = new Config();
125123
$config->merge(['extra' => ['helhum/dotenv-connector' => [
126124
'env-file' => __DIR__ . '/Fixtures/env/.no-env',
127125
'adapter' => SymfonyDotEnv::class
128126
]]]);
129-
/** @var ClassLoader|\PHPUnit\Framework\MockObject\MockObject $loaderMock */
130127
$loaderMock = $this->createMock(ClassLoader::class);
131128
$loaderMock->expects($this->once())->method('register');
132129
$loaderMock->expects($this->once())->method('unregister');
@@ -140,42 +137,48 @@ public function dumpReturnsFalseIfFileCannotBeWritten()
140137
/**
141138
* @test
142139
*/
143-
public function includingFileDoesNotOverrideExistingEnvVars()
140+
public function includingFileDoesNotOverrideExistingEnvVars(): void
144141
{
145142
putenv('FOO=baz');
146-
$configProphecy = $this->prophesize(Config::class);
147-
$configProphecy->get('env-file')->willReturn(__DIR__ . '/Fixtures/env/.env');
148-
$configProphecy->get('adapter')->willReturn(SymfonyDotEnv::class);
149-
$loaderProphecy = $this->prophesize(ClassLoader::class);
150-
$loaderProphecy->register()->shouldBeCalled();
151-
$loaderProphecy->unregister()->shouldBeCalled();
143+
$_ENV['FOO'] = 'baz';
144+
$config = new Config();
145+
$config->merge(['extra' => ['helhum/dotenv-connector' => [
146+
'env-file' => __DIR__ . '/Fixtures/env/.env',
147+
'adapter' => SymfonyDotEnv::class
148+
]]]);
149+
$loaderMock = $this->createMock(ClassLoader::class);
150+
$loaderMock->expects($this->once())->method('register');
151+
$loaderMock->expects($this->once())->method('unregister');
152152

153153
$includeFilePath = __DIR__ . '/Fixtures/vendor/helhum/include.php';
154-
$includeFile = new IncludeFile($configProphecy->reveal(), $loaderProphecy->reveal(), $includeFilePath);
154+
$includeFile = new IncludeFile($config, $loaderMock, $includeFilePath);
155155
$includeFile->dump();
156-
$this->assertTrue(file_exists($includeFilePath));
156+
$this->assertFileExists($includeFilePath);
157157

158158
$this->assertSame('baz', getenv('FOO'));
159159
}
160160

161161
/**
162162
* @test
163163
*/
164-
public function includingFileDoesOverrideExistingEnvVars()
164+
public function includingFileDoesOverrideExistingEnvVars(): void
165165
{
166166
putenv('FOO=baz');
167+
$_ENV['FOO'] = 'baz';
167168
putenv('DOTENV_CONNECTOR_OVERRIDE=1');
168-
$configProphecy = $this->prophesize(Config::class);
169-
$configProphecy->get('env-file')->willReturn(__DIR__ . '/Fixtures/env/.env');
170-
$configProphecy->get('adapter')->willReturn(SymfonyDotEnv::class);
171-
$loaderProphecy = $this->prophesize(ClassLoader::class);
172-
$loaderProphecy->register()->shouldBeCalled();
173-
$loaderProphecy->unregister()->shouldBeCalled();
169+
$config = new Config();
170+
$config->merge(['extra' => ['helhum/dotenv-connector' => [
171+
'env-file' => __DIR__ . '/Fixtures/env/.env',
172+
'adapter' => SymfonyDotEnv::class
173+
]]]);
174+
$loaderMock = $this->createMock(ClassLoader::class);
175+
$loaderMock->expects($this->once())->method('register');
176+
$loaderMock->expects($this->once())->method('unregister');
174177

175178
$includeFilePath = __DIR__ . '/Fixtures/vendor/helhum/include.php';
176-
$includeFile = new IncludeFile($configProphecy->reveal(), $loaderProphecy->reveal(), $includeFilePath);
179+
$includeFile = new IncludeFile($config, $loaderMock, $includeFilePath);
177180
$includeFile->dump();
178-
$this->assertTrue(file_exists($includeFilePath));
181+
$this->assertFileExists($includeFilePath);
179182

180183
$this->assertSame('bar', getenv('FOO'));
181184
}

0 commit comments

Comments
 (0)