-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: create attribute #[WithEnvironmentVariable]
#6126
base: main
Are you sure you want to change the base?
Conversation
src/Framework/TestCase.php
Outdated
foreach ($environmentVariables as $environmentVariableName => $environmentVariableValue) { | ||
assert($metadata instanceof WithEnvironmentVariable); | ||
|
||
$this->backupEnvironmentVariables[$environmentVariableName] = $_ENV[$environmentVariableName] ?? getenv($environmentVariableName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was torn on this... in PHP, nothing prevents to have a different value for $_ENV['VAR']
and getenv('VAR')
.
Do you think I should store both representations, in order to have 100% the same environment afterand before the test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so, yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any hints how I should store this?
my first guess would be to store this in an array:
/**
* @var array{getenv: array<string, false|string>, global: array<string, false|string>}
*/
private array $backupEnvironmentVariables = ['getenv' => [], 'global' => []];
but I don't like it. Another idea would be to use a value object:
final readonly class BackupEnvironmentVariable
{
public static function asGetenv(): self;
public static function asSuperGlobal(): self;
}
if so, in which namespace should i put this class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the code to use a value object. (some tests are failing, I'll check them after we agree on the implementation)
#[WithEnvironmentVariable('BAZ', '1')] | ||
#[WithEnvironmentVariable('BAZ', '2')] | ||
#[WithEnvironmentVariable('BAZ', '3')] | ||
public function testMultipleAttributesKeepTheLastValue(): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we should throw an exception for this case?
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #6126 +/- ##
============================================
+ Coverage 95.08% 95.09% +0.01%
- Complexity 6738 6754 +16
============================================
Files 725 726 +1
Lines 21251 21306 +55
============================================
+ Hits 20207 20262 +55
Misses 1044 1044 ☔ View full report in Codecov by Sentry. |
9844b03
to
b582dda
Compare
public static function setUpBeforeClass(): void | ||
{ | ||
self::assertEnvironmentVariablesHaveDefaultValues(); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->assertEnvironmentVariablesHaveCustomValues(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$this->assertEnvironmentVariablesHaveCustomValues(); | ||
} | ||
|
||
public static function tearDownAfterClass(): void | ||
{ | ||
self::assertEnvironmentVariablesHaveDefaultValues(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure of what would be the desired behavior here 🤔
#[WithEnvironmentVariable('BAZ', '1')] | ||
#[RequiresEnvironmentVariable('BAZ', '1')] | ||
public function testUsingAlongWithRequiresEnvironmentVariableAttribute(): void | ||
{ | ||
$this->assertSame('1', $_ENV['BAZ']); | ||
$this->assertSame('1', getenv('BAZ')); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really sure how these two attributes should interact...
that's a bit odd to use them both on the same test, tho...
#[WithEnvironmentVariable]
#[WithEnvironmentVariable]
b582dda
to
fa7faea
Compare
fa7faea
to
48d3ead
Compare
fixes #6116