Skip to content

feat: create attribute #[WithEnvironmentVariable]#6126

Closed
nikophil wants to merge 2 commits intosebastianbergmann:mainfrom
nikophil:feat/with-env-var
Closed

feat: create attribute #[WithEnvironmentVariable]#6126
nikophil wants to merge 2 commits intosebastianbergmann:mainfrom
nikophil:feat/with-env-var

Conversation

@nikophil
Copy link
Copy Markdown
Contributor

@nikophil nikophil commented Feb 9, 2025

fixes #6116

Comment thread src/Framework/TestCase.php Outdated
foreach ($environmentVariables as $environmentVariableName => $environmentVariableValue) {
assert($metadata instanceof WithEnvironmentVariable);

$this->backupEnvironmentVariables[$environmentVariableName] = $_ENV[$environmentVariableName] ?? getenv($environmentVariableName);
Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, yes.

Copy link
Copy Markdown
Contributor Author

@nikophil nikophil Feb 11, 2025

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?

Copy link
Copy Markdown
Contributor Author

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)

Comment on lines +108 to +111
#[WithEnvironmentVariable('BAZ', '1')]
#[WithEnvironmentVariable('BAZ', '2')]
#[WithEnvironmentVariable('BAZ', '3')]
public function testMultipleAttributesKeepTheLastValue(): void
Copy link
Copy Markdown
Contributor Author

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
Copy link
Copy Markdown

codecov Bot commented Feb 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.30%. Comparing base (f5fe9d1) to head (71dc843).
Report is 20 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #6126      +/-   ##
============================================
+ Coverage     95.29%   95.30%   +0.01%     
- Complexity     6755     6774      +19     
============================================
  Files           726      728       +2     
  Lines         21300    21361      +61     
============================================
+ Hits          20298    20359      +61     
  Misses         1002     1002              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +23 to +39
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();
}
Copy link
Copy Markdown
Contributor Author

@nikophil nikophil Feb 9, 2025

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 🤔

Comment on lines +117 to +123
#[WithEnvironmentVariable('BAZ', '1')]
#[RequiresEnvironmentVariable('BAZ', '1')]
public function testUsingAlongWithRequiresEnvironmentVariableAttribute(): void
{
$this->assertSame('1', $_ENV['BAZ']);
$this->assertSame('1', getenv('BAZ'));
}
Copy link
Copy Markdown
Contributor Author

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...

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, PHPUnit would emit a warning when WithEnvironmentVariable and RequiresEnvironmentVariable are 1) used on the same test and 2) refer to the same environment variable. But this can be done in a future PR.

@nikophil nikophil changed the title feat: create attributei #[WithEnvironmentVariable] feat: create attribute #[WithEnvironmentVariable] Feb 9, 2025
@sebastianbergmann sebastianbergmann added this to the PHPUnit 12.1 milestone Feb 10, 2025
@sebastianbergmann sebastianbergmann added type/enhancement A new idea that should be implemented feature/test-runner CLI test runner feature/metadata/attributes labels Feb 10, 2025
@sebastianbergmann
Copy link
Copy Markdown
Owner

Merged manually, thanks.

@nikophil
Copy link
Copy Markdown
Contributor Author

nikophil commented Mar 4, 2025

hey @sebastianbergmann

Merged manually, thanks.

cool, thanks :)

out of curiosity: why do you sometimes close the MR and merge manually? BTW, the tests were failing 🤔

@sebastianbergmann
Copy link
Copy Markdown
Owner

In this case I did it manually because I wanted to fix the build 😄

@nikophil
Copy link
Copy Markdown
Contributor Author

nikophil commented Mar 4, 2025

haha sorry for this, I was waiting for your approval about the implementation before fixing the tests 😅

anyway, I'm glad this was merged 👍

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

feature/metadata/attributes feature/test-runner CLI test runner type/enhancement A new idea that should be implemented

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide #[WithEnvironmentVariable()] attribute to set env variables

2 participants