diff --git a/src/EnvironmentSetCommand.php b/src/EnvironmentSetCommand.php index 2a27401..ab89487 100644 --- a/src/EnvironmentSetCommand.php +++ b/src/EnvironmentSetCommand.php @@ -108,7 +108,7 @@ public function setEnvVariable(string $envFileContent, string $key, string $valu public function readKeyValuePair(string $envFileContent, string $key): ?string { // Match the given key at the beginning of a line - if (preg_match("#^ *{$key} *= *[^\r\n]*$#uimU", $envFileContent, $matches)) { + if (preg_match("#^\s*{$key}\s*=\s*[^\r\n]*$#uimU", $envFileContent, $matches)) { return $matches[0]; } diff --git a/tests/Unit/EnvironmentSetCommandTest.php b/tests/Unit/EnvironmentSetCommandTest.php index 2d26542..d0d3c28 100644 --- a/tests/Unit/EnvironmentSetCommandTest.php +++ b/tests/Unit/EnvironmentSetCommandTest.php @@ -25,11 +25,6 @@ public function setUp(): void /** * @covers EnvironmentSetCommand::setEnvVariable * @dataProvider setEnvVariableDataProvider - * - * @param string $originalEnvFileContent - * @param string $key - * @param string $value - * @param string $expectedNewEnvFile */ public function testSetEnvVariable( string $originalEnvFileContent, @@ -62,15 +57,24 @@ public function testSetEnvVariableTestOfNestedKeys(): void $this->assertEquals($expectedEnv, $newEnv); } + /** + * @covers EnvironmentSetCommand::setEnvVariable + */ + public function testWhitespaceAsValueDoesntCreateNewEntry(): void + { + $env = 'APP_KEY = \t' . "\n"; + + $expectedEnv = 'APP_KEY=test' . "\n"; + + [$newEnv, $_] = $this->command->setEnvVariable($env, 'APP_KEY', 'test'); + $this->assertEquals($expectedEnv, $newEnv); + } + /** * @covers EnvironmentSetCommand::readKeyValuePair * @dataProvider readKeyValuePairDataProvider - * - * @param string $envFileContent - * @param string $key - * @param string|null $expectedKeyValuePair */ - public function testReadKeyValuePair(string $envFileContent, string $key, ?string $expectedKeyValuePair): void + public function testReadKeyValuePair(string $envFileContent, string $key, ?string $expectedKeyValuePair = null): void { $realPair = $this->command->readKeyValuePair($envFileContent, $key); $this->assertEquals($expectedKeyValuePair, $realPair); @@ -92,9 +96,6 @@ public function testParseCommandArguments(array $params, array $expectedResult): /** * @covers EnvironmentSetCommand::assertKeyIsValid * @dataProvider assertKeyIsValidDataProvider - * - * @param string $key - * @param bool $isGood */ public function testAssertKeyIsValid(string $key, bool $isGood): void {