This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* develop: keep loaded env variables in memory to avoid parsing .env multiple times add missing tests
- Loading branch information
Showing
6 changed files
with
509 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Innmind\CLI\Framework; | ||
|
||
use Innmind\CLI\{ | ||
Environment, | ||
Environment\ExitCode, | ||
}; | ||
use Innmind\Url\Path; | ||
use Innmind\Stream\{ | ||
Readable, | ||
Writable, | ||
}; | ||
use Innmind\Immutable\{ | ||
Map, | ||
Sequence, | ||
}; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
|
||
final class KeepVariablesInMemory implements Environment | ||
{ | ||
private Environment $env; | ||
/** @var Map<string, string>|null */ | ||
private ?Map $variables = null; | ||
|
||
public function __construct(Environment $env) | ||
{ | ||
$this->env = $env; | ||
} | ||
|
||
public function interactive(): bool | ||
{ | ||
return $this->env->interactive(); | ||
} | ||
|
||
public function input(): Readable | ||
{ | ||
return $this->env->input(); | ||
} | ||
|
||
public function output(): Writable | ||
{ | ||
return $this->env->output(); | ||
} | ||
|
||
public function error(): Writable | ||
{ | ||
return $this->env->error(); | ||
} | ||
|
||
public function arguments(): Sequence | ||
{ | ||
return $this->env->arguments(); | ||
} | ||
|
||
public function variables(): Map | ||
{ | ||
return $this->variables ??= $this->env->variables(); | ||
} | ||
|
||
public function exit(int $code): void | ||
{ | ||
$this->env->exit($code); | ||
} | ||
|
||
public function exitCode(): ExitCode | ||
{ | ||
return $this->env->exitCode(); | ||
} | ||
|
||
public function workingDirectory(): Path | ||
{ | ||
return $this->env->workingDirectory(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Tests\Innmind\CLI\Framework; | ||
|
||
use Innmind\CLI\Framework\DotEnvAware; | ||
use Innmind\CLI\{ | ||
Environment, | ||
Environment\ExitCode, | ||
}; | ||
use Innmind\OperatingSystem\Filesystem; | ||
use Innmind\Stream\{ | ||
Readable, | ||
Writable, | ||
}; | ||
use PHPUnit\Framework\TestCase; | ||
use Innmind\BlackBox\{ | ||
PHPUnit\BlackBox, | ||
Set, | ||
}; | ||
use Fixtures\Innmind\Url\Path; | ||
use Fixtures\Innmind\Immutable\Sequence; | ||
|
||
class DotEnvAwareTest extends TestCase | ||
{ | ||
use BlackBox; | ||
|
||
public function testInterface() | ||
{ | ||
$this | ||
->forAll(Path::any()) | ||
->then(function($path) { | ||
$this->assertInstanceOf( | ||
Environment::class, | ||
new DotEnvAware( | ||
$this->createMock(Environment::class), | ||
$this->createMock(Filesystem::class), | ||
$path, | ||
), | ||
); | ||
}); | ||
} | ||
|
||
public function testInteractive() | ||
{ | ||
$this | ||
->forAll( | ||
Path::any(), | ||
Set\Elements::of(true, false), | ||
) | ||
->then(function($path, $interactive) { | ||
$env = new DotEnvAware( | ||
$inner = $this->createMock(Environment::class), | ||
$this->createMock(Filesystem::class), | ||
$path, | ||
); | ||
$inner | ||
->method('interactive') | ||
->willReturn($interactive); | ||
|
||
$this->assertSame($interactive, $env->interactive()); | ||
}); | ||
} | ||
|
||
public function testInput() | ||
{ | ||
$this | ||
->forAll(Path::any()) | ||
->then(function($path) { | ||
$env = new DotEnvAware( | ||
$inner = $this->createMock(Environment::class), | ||
$this->createMock(Filesystem::class), | ||
$path, | ||
); | ||
$inner | ||
->method('input') | ||
->willReturn($input = $this->createMock(Readable::class)); | ||
|
||
$this->assertSame($input, $env->input()); | ||
}); | ||
} | ||
|
||
public function testOutput() | ||
{ | ||
$this | ||
->forAll(Path::any()) | ||
->then(function($path) { | ||
$env = new DotEnvAware( | ||
$inner = $this->createMock(Environment::class), | ||
$this->createMock(Filesystem::class), | ||
$path, | ||
); | ||
$inner | ||
->method('output') | ||
->willReturn($output = $this->createMock(Writable::class)); | ||
|
||
$this->assertSame($output, $env->output()); | ||
}); | ||
} | ||
|
||
public function testError() | ||
{ | ||
$this | ||
->forAll(Path::any()) | ||
->then(function($path) { | ||
$env = new DotEnvAware( | ||
$inner = $this->createMock(Environment::class), | ||
$this->createMock(Filesystem::class), | ||
$path, | ||
); | ||
$inner | ||
->method('error') | ||
->willReturn($error = $this->createMock(Writable::class)); | ||
|
||
$this->assertSame($error, $env->error()); | ||
}); | ||
} | ||
|
||
public function testArguments() | ||
{ | ||
$this | ||
->forAll( | ||
Path::any(), | ||
Sequence::of('string', Set\Strings::any()) | ||
) | ||
->then(function($path, $arguments) { | ||
$env = new DotEnvAware( | ||
$inner = $this->createMock(Environment::class), | ||
$this->createMock(Filesystem::class), | ||
$path, | ||
); | ||
$inner | ||
->method('arguments') | ||
->willReturn($arguments); | ||
|
||
$this->assertSame($arguments, $env->arguments()); | ||
}); | ||
} | ||
|
||
public function testExit() | ||
{ | ||
$this | ||
->forAll( | ||
Path::any(), | ||
Set\Integers::above(0), | ||
) | ||
->then(function($path, $code) { | ||
$env = new DotEnvAware( | ||
$inner = $this->createMock(Environment::class), | ||
$this->createMock(Filesystem::class), | ||
$path, | ||
); | ||
$inner | ||
->expects($this->once()) | ||
->method('exit') | ||
->with($code); | ||
|
||
$this->assertNull($env->exit($code)); | ||
}); | ||
} | ||
|
||
public function testExitCode() | ||
{ | ||
$this | ||
->forAll( | ||
Path::any(), | ||
Set\Integers::above(0), | ||
) | ||
->then(function($path, $code) { | ||
$env = new DotEnvAware( | ||
$inner = $this->createMock(Environment::class), | ||
$this->createMock(Filesystem::class), | ||
$path, | ||
); | ||
$inner | ||
->method('exitCode') | ||
->willReturn($expected = new ExitCode($code)); | ||
|
||
$this->assertSame($expected, $env->exitCode()); | ||
}); | ||
} | ||
|
||
public function testWorkingDirectory() | ||
{ | ||
$this | ||
->forAll( | ||
Path::any(), | ||
Path::any(), | ||
) | ||
->then(function($path, $workingDirectory) { | ||
$env = new DotEnvAware( | ||
$inner = $this->createMock(Environment::class), | ||
$this->createMock(Filesystem::class), | ||
$path, | ||
); | ||
$inner | ||
->method('workingDirectory') | ||
->willReturn($workingDirectory); | ||
|
||
$this->assertSame($workingDirectory, $env->workingDirectory()); | ||
}); | ||
} | ||
} |
Oops, something went wrong.