Skip to content

Commit 78bf383

Browse files
committed
[player] make --blackfire-env optionnal
1 parent c6992d8 commit 78bf383

4 files changed

Lines changed: 29 additions & 56 deletions

File tree

Player/Console/PlayerCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protected function configure(): void
103103
new InputOption('disable-internal-network', '', InputOption::VALUE_NONE, 'Disable internal network'),
104104
new InputOption('sandbox', '', InputOption::VALUE_NONE, 'Enable the sandbox mode'),
105105
new InputOption('ssl-no-verify', '', InputOption::VALUE_NONE, 'Disable SSL certificate verification'),
106-
new InputOption('blackfire-env', '', InputOption::VALUE_REQUIRED, 'The blackfire environment to use'),
106+
new InputOption('blackfire-env', '', InputOption::VALUE_OPTIONAL, 'The blackfire environment to use'),
107107
new InputOption('step', '', InputOption::VALUE_NONE, 'Interactive execution. Ask user validation before every step.'),
108108
new InputOption('report', '', InputOption::VALUE_NONE, 'Display profiles reports in the console output.'),
109109
])

Player/Extension/BlackfireEnvResolver.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ public function __construct(
3535
/**
3636
* Resolves the environment from the current StepContext.
3737
*
38-
* When the return value is false, it means that we shouldn't profile the current step.
38+
* The return value tells the caller how to profile the current step:
39+
* - false: don't profile the step;
40+
* - null: profile the step without an explicit environment (the agent /
41+
* personal collab token decides where the profile lands);
42+
* - string: profile the step in that environment.
3943
*/
40-
public function resolve(StepContext $stepContext, ScenarioContext $scenarioContext, AbstractStep $step): string|false
44+
public function resolve(StepContext $stepContext, ScenarioContext $scenarioContext, AbstractStep $step): string|false|null
4145
{
4246
// let's check if the current step resolves false. In that case, it means that we won't profile the step URL
4347
$env = $stepContext->getBlackfireEnv();
@@ -60,12 +64,11 @@ public function resolve(StepContext $stepContext, ScenarioContext $scenarioConte
6064
}
6165

6266
// it resolved true: we'll profile the step using the default environment.
67+
// When no default environment is set, we still profile the step but
68+
// without an explicit environment (the agent / personal collab token
69+
// decides where the profile lands).
6370
if (true === $env) {
64-
if (null === $this->defaultEnv) {
65-
throw new \LogicException('--blackfire-env option must be set when using "blackfire: true" in a scenario.');
66-
}
67-
68-
$env = $this->defaultEnv;
71+
return $this->defaultEnv;
6972
}
7073

7174
return $env ?? false;

Player/Tests/Extension/BlackfireEnvResolverTest.php

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
class BlackfireEnvResolverTest extends TestCase
2727
{
2828
#[DataProvider('resolveAnEnvironmentProvider')]
29-
public function testResolveAnEnvironment(string|null $defaultEnv, string|null $stepBlackfire, string|null $scenarioSetEnvironment, string|bool $expectedResult): void
29+
public function testResolveAnEnvironment(string|null $defaultEnv, string|null $stepBlackfire, string|null $scenarioSetEnvironment, string|bool|null $expectedResult): void
3030
{
3131
[
3232
$resolver,
@@ -70,6 +70,13 @@ public static function resolveAnEnvironmentProvider(): \Generator
7070
true,
7171
];
7272

73+
yield 'resolves null when the blackfire step is true but no default env is set' => [
74+
null,
75+
'true',
76+
null,
77+
null,
78+
];
79+
7380
yield 'resolves false when no env were found' => [
7481
null,
7582
null,
@@ -111,31 +118,6 @@ public static function precedenceProvider(): \Generator
111118
];
112119
}
113120

114-
#[DataProvider('errorsProvider')]
115-
public function testResolveThrowsAnError(string|null $defaultEnv, string|null $stepBlackfire, string|null $scenarioSetEnvironment): void
116-
{
117-
[
118-
$resolver,
119-
$stepContext,
120-
$scenarioContext,
121-
$step,
122-
] = $this->arrange($defaultEnv, $stepBlackfire, $scenarioSetEnvironment);
123-
124-
$this->expectException(\LogicException::class);
125-
$this->expectExceptionMessage('--blackfire-env option must be set when using "blackfire: true" in a scenario.');
126-
127-
$resolver->resolve($stepContext, $scenarioContext, $step);
128-
}
129-
130-
public static function errorsProvider(): \Generator
131-
{
132-
yield 'no env were found but step resolves true' => [
133-
null,
134-
'true',
135-
null,
136-
];
137-
}
138-
139121
#[DataProvider('deprecationsProvider')]
140122
public function testDeprecationWarning(string|null $defaultEnv, string|null $stepBlackfire, string|null $scenarioSetEnvironment, string|bool $expectedResult, array $expectedDeprecations): void
141123
{

Player/Tests/Extension/BlackfireExtensionTest.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -172,38 +172,26 @@ static function (ScenarioContext $scenarioContext): void {
172172
];
173173
}
174174

175-
#[DataProvider('beforeRequestFailureProvider')]
176-
public function testBeforeRequestFailure(Step $step, HttpRequest $request, array $defaultScenarioSetExtraValues, string $exceptionClass, string $exceptionMessage): void
175+
public function testBeforeRequestProfilesWhenBlackfireTrueWithoutDefaultEnv(): void
177176
{
177+
// "blackfire: true" without a --blackfire-env must still profile the step:
178+
// the agent / personal collab token decides where the profile lands.
178179
$extension = $this->getBlackfireExtension(null);
179180

181+
$step = new VisitStep('https://app-under-test.lan');
182+
$step->blackfire('true');
183+
180184
$stepContext = new StepContext();
181185
$stepContext->update($step, []);
182186

183187
$scenarioContext = new ScenarioContext('"foo"', new ScenarioSet());
184188

185-
foreach ($defaultScenarioSetExtraValues as $k => $v) {
186-
$scenarioContext->setExtraValue($k, $v);
187-
}
188-
189-
$this->expectException($exceptionClass);
190-
$this->expectExceptionMessage($exceptionMessage);
191-
189+
$request = new HttpRequest('GET', 'https://app-under-test.lan');
192190
$extension->beforeStep(new RequestStep($request, $step), $stepContext, $scenarioContext);
193-
}
194191

195-
public static function beforeRequestFailureProvider(): \Generator
196-
{
197-
$defaultScenarioSetExtraValues = [];
198-
$step = new VisitStep('https://app-under-test.lan');
199-
$step->blackfire('true');
200-
yield 'throws when blackfire env is true but not defined at the ScenarioContext level' => [
201-
$step,
202-
new HttpRequest('GET', 'https://app-under-test.lan'),
203-
$defaultScenarioSetExtraValues,
204-
\LogicException::class,
205-
'--blackfire-env option must be set when using "blackfire: true" in a scenario.',
206-
];
192+
$this->assertArrayHasKey(BlackfireExtension::HEADER_BLACKFIRE_QUERY, $request->headers);
193+
$this->assertEquals(['1234'], $request->headers[BlackfireExtension::HEADER_BLACKFIRE_QUERY]);
194+
$this->assertEquals(['1111-2222-3333-4444'], $request->headers[BlackfireExtension::HEADER_BLACKFIRE_PROFILE_UUID]);
207195
}
208196

209197
#[DataProvider('afterResponseFailureProvider')]

0 commit comments

Comments
 (0)