Skip to content

Commit 84f2f6b

Browse files
committed
test: implement integration tests for Flagd provider
- provides further transformation of Gherkin test cases to required inputs such as booleans from strings
1 parent 05e29db commit 84f2f6b

File tree

1 file changed

+83
-70
lines changed

1 file changed

+83
-70
lines changed

integration/features/bootstrap/FeatureContext.php

Lines changed: 83 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
4+
35
declare(strict_types=1);
46

57
use Behat\Behat\Context\Context as BehatContext;
@@ -34,13 +36,6 @@ class FeatureContext implements BehatContext
3436
private ?EvaluationContext $inputContext = null;
3537
private ?EvaluationOptions $inputOptions = null;
3638

37-
/** @var mixed $calculatedValue */
38-
private $calculatedValue;
39-
private bool $valueWasCalculated = false;
40-
41-
private EvaluationDetails $calculatedDetails;
42-
private bool $detailsWereCalculated = false;
43-
4439
/**
4540
* Initializes context.
4641
*
@@ -125,6 +120,7 @@ public function anIntegerFlagWithKeyIsEvaluatedWithDefaultValue(string $flagKey,
125120
$this->flagType = FlagValueType::INTEGER;
126121
$this->inputFlagKey = $flagKey;
127122
$this->inputFlagDefaultValue = $defaultValue;
123+
print_r("Setting integer...\n");
128124
}
129125

130126
/**
@@ -160,9 +156,9 @@ public function theResolvedFloatValueShouldBe(float $resolvedValue)
160156
}
161157

162158
/**
163-
* @When an object flag with key :flagKey is evaluated with a :defaultValue default value
164-
*
165159
* @param mixed $defaultValue
160+
*
161+
* @When an object flag with key :flagKey is evaluated with a :defaultValue default value
166162
*/
167163
public function anObjectFlagWithKeyIsEvaluatedWithANullDefaultValue(string $flagKey, $defaultValue)
168164
{
@@ -172,11 +168,11 @@ public function anObjectFlagWithKeyIsEvaluatedWithANullDefaultValue(string $flag
172168
}
173169

174170
/**
175-
* @Then the resolved object value should be contain fields :key1, :key2, and :key3, with values :value1, :value2 and :value3, respectively
176-
*
177171
* @param mixed $value1
178172
* @param mixed $value2
179173
* @param mixed $value3
174+
*
175+
* @Then the resolved object value should be contain fields :key1, :key2, and :key3, with values :value1, :value2 and :value3, respectively
180176
*/
181177
public function theResolvedObjectValueShouldBeContainFieldsAndWithValuesAndRespectively(string $key1, string $key2, string $key3, $value1, $value2, $value3)
182178
{
@@ -279,9 +275,9 @@ public function theResolvedFloatDetailsValueShouldBeTheVariantShouldBeAndTheReas
279275
}
280276

281277
/**
282-
* @When an object flag with key :flagKey is evaluated with details and a :defaultValue default value
283-
*
284278
* @param mixed $defaultValue
279+
*
280+
* @When an object flag with key :flagKey is evaluated with details and a :defaultValue default value
285281
*/
286282
public function anObjectFlagWithKeyIsEvaluatedWithDetailsAndANullDefaultValue(string $flagKey, $defaultValue)
287283
{
@@ -291,11 +287,11 @@ public function anObjectFlagWithKeyIsEvaluatedWithDetailsAndANullDefaultValue(st
291287
}
292288

293289
/**
294-
* @Then the resolved object details value should be contain fields :key1, :key2, and :key3, with values :value1, :value2 and :value3, respectively
295-
*
296290
* @param mixed $value1
297291
* @param mixed $value2
298292
* @param mixed $value3
293+
*
294+
* @Then the resolved object details value should be contain fields :key1, :key2, and :key3, with values :value1, :value2 and :value3, respectively
299295
*/
300296
public function theResolvedObjectDetailsValueShouldBeContainFieldsAndWithValuesAndRespectively(string $key1, string $key2, string $key3, $value1, $value2, $value3)
301297
{
@@ -320,15 +316,31 @@ public function theVariantShouldBeAndTheReasonShouldBe(string $variant, string $
320316
}
321317

322318
/**
323-
* @When context contains keys :key1, :key2, :key3, :key4 with values :value1, :value2, :value3, :value4
324-
*
325319
* @param mixed $value1
326320
* @param mixed $value2
327321
* @param mixed $value3
328322
* @param mixed $value4
323+
*
324+
* @When context contains keys :key1, :key2, :key3, :key4 with values :value1, :value2, :value3, :value4
329325
*/
330326
public function contextContainsKeysWithValues(string $key1, string $key2, string $key3, string $key4, $value1, $value2, $value3, $value4)
331327
{
328+
if ($this->isBooleanLikeString($value1)) {
329+
$value1 = $this->stringAsBool($value1);
330+
}
331+
332+
if ($this->isBooleanLikeString($value2)) {
333+
$value2 = $this->stringAsBool($value2);
334+
}
335+
336+
if ($this->isBooleanLikeString($value3)) {
337+
$value3 = $this->stringAsBool($value3);
338+
}
339+
340+
if ($this->isBooleanLikeString($value4)) {
341+
$value4 = $this->stringAsBool($value4);
342+
}
343+
332344
$this->inputContext = (new MutableEvaluationContext(null, new Attributes([
333345
$key1 => $value1,
334346
$key2 => $value2,
@@ -338,9 +350,9 @@ public function contextContainsKeysWithValues(string $key1, string $key2, string
338350
}
339351

340352
/**
341-
* @When a flag with key :flagKey is evaluated with default value :defaultValue
342-
*
343353
* @param mixed $defaultValue
354+
*
355+
* @When a flag with key :flagKey is evaluated with default value :defaultValue
344356
*/
345357
public function aFlagWithKeyIsEvaluatedWithDefaultValue(string $flagKey, $defaultValue)
346358
{
@@ -358,9 +370,9 @@ public function theResolvedStringResponseShouldBe(string $resolvedValue)
358370
}
359371

360372
/**
361-
* @Then the resolved flag value is :value when the context is empty
362-
*
363373
* @param mixed $value
374+
*
375+
* @Then the resolved flag value is :value when the context is empty
364376
*/
365377
public function theResolvedFlagValueIsWhenTheContextIsEmpty($value)
366378
{
@@ -443,80 +455,68 @@ public function theReasonShouldIndicateAnErrorAndTheErrorCodeShouldIndicateAType
443455

444456
/**
445457
* Ensures the value is only calculated once the first time this is called, memoizing its value
446-
*
458+
*
447459
* @return mixed
448460
*/
449461
private function calculateValue()
450462
{
451-
if (!$this->valueWasCalculated) {
452-
$value = null;
453-
switch ($this->flagType) {
454-
case FlagValueType::BOOLEAN:
455-
$value = $this->client->getBooleanValue($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
463+
$value = null;
464+
switch ($this->flagType) {
465+
case FlagValueType::BOOLEAN:
466+
$value = $this->client->getBooleanValue($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
456467

457-
break;
458-
case FlagValueType::FLOAT:
459-
$value = $this->client->getFloatValue($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
468+
break;
469+
case FlagValueType::FLOAT:
470+
$value = $this->client->getFloatValue($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
460471

461-
break;
462-
case FlagValueType::INTEGER:
463-
$value = $this->client->getIntegerValue($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
472+
break;
473+
case FlagValueType::INTEGER:
474+
$value = $this->client->getIntegerValue($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
464475

465-
break;
466-
case FlagValueType::OBJECT:
467-
$value = $this->client->getObjectValue($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
468-
469-
break;
470-
case FlagValueType::STRING:
471-
$value = $this->client->getStringValue($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
472-
473-
break;
474-
}
476+
break;
477+
case FlagValueType::OBJECT:
478+
$value = $this->client->getObjectValue($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
475479

476-
$this->calculatedValue = $value;
480+
break;
481+
case FlagValueType::STRING:
482+
$value = $this->client->getStringValue($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
477483

478-
$this->valueWasCalculated = true;
484+
break;
479485
}
480486

481-
return $this->calculatedValue;
487+
return $value;
482488
}
483489

484490
/**
485491
* Ensures the details are only calculated once the first time this is called, memoizing its details
486492
*/
487493
private function calculateDetails(): EvaluationDetails
488494
{
489-
if (!$this->detailsWereCalculated) {
490-
$details = null;
491-
switch ($this->flagType) {
492-
case FlagValueType::BOOLEAN:
493-
$details = $this->client->getBooleanDetails($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
495+
$details = null;
496+
switch ($this->flagType) {
497+
case FlagValueType::BOOLEAN:
498+
$details = $this->client->getBooleanDetails($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
494499

495-
break;
496-
case FlagValueType::FLOAT:
497-
$details = $this->client->getFloatDetails($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
500+
break;
501+
case FlagValueType::FLOAT:
502+
$details = $this->client->getFloatDetails($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
498503

499-
break;
500-
case FlagValueType::INTEGER:
501-
$details = $this->client->getIntegerDetails($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
504+
break;
505+
case FlagValueType::INTEGER:
506+
$details = $this->client->getIntegerDetails($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
502507

503-
break;
504-
case FlagValueType::OBJECT:
505-
$details = $this->client->getObjectDetails($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
508+
break;
509+
case FlagValueType::OBJECT:
510+
$details = $this->client->getObjectDetails($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
506511

507-
break;
508-
case FlagValueType::STRING:
509-
$details = $this->client->getStringDetails($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
512+
break;
513+
case FlagValueType::STRING:
514+
$details = $this->client->getStringDetails($this->inputFlagKey, $this->inputFlagDefaultValue, $this->inputContext, $this->inputOptions);
510515

511-
break;
512-
}
513-
514-
$this->calculatedDetails = $details;
515-
516-
$this->detailsWereCalculated = true;
516+
break;
517517
}
518518

519-
return $this->calculatedDetails;
519+
return $details;
520520
}
521521

522522
/**
@@ -558,4 +558,17 @@ private function getFlagTypeOf($value): ?string
558558
return FlagValueType::BOOLEAN;
559559
}
560560
}
561+
562+
/**
563+
* @param mixed $value
564+
*/
565+
private function isBooleanLikeString($value): bool
566+
{
567+
return $value === 'true' || $value === 'false';
568+
}
569+
570+
private function stringAsBool(string $value): bool
571+
{
572+
return $value === 'true';
573+
}
561574
}

0 commit comments

Comments
 (0)