Skip to content

Latest commit

 

History

History
113 lines (80 loc) · 4.23 KB

testing.md

File metadata and controls

113 lines (80 loc) · 4.23 KB

Testing with Flipster

The doubles ship in the runtime package (src/Testing/), so your suite uses them with no extra dependency and no vendoring. They impose no test framework: everything returns data for you to assert on.

The doubles

Implementation Description
InMemoryProvider resolves from a map; reports FLAG_NOT_FOUND / TYPE_MISMATCH like a real provider
RecordingProvider decorator that records every evaluation
ThrowingProvider fails on demand, globally or per flag
ControllableCircuitBreaker pin the circuit open or closed

Pinning flag values

$provider = new InMemoryProvider(['new-checkout' => true, 'variant' => 'blue']);

$flags = Flipster::for($provider)
    ->withDefaults(['new-checkout' => false, 'variant' => 'control'])
    ->evaluator();

It behaves like a real provider including when it fails, which is deliberate: a double that only models the happy path lets the failure paths rot untested. An unseeded flag reports FLAG_NOT_FOUND; a wrongly typed one reports TYPE_MISMATCH.

It is also the one deliberately mutable class in the library, because tests need a flag to change between assertions:

$provider->set('new-checkout', false);   // a rollout being flipped mid-run
$provider->remove('new-checkout');       // the flag disappearing upstream

Seed an arbitrary reason to test your handling of a cached answer:

$provider->set('flag', true, reason: 'CACHED', variant: 'on');

Asserting which flags were consulted

$recorder = new RecordingProvider(new InMemoryProvider(['a' => true]));

// ... exercise your code ...

$recorder->wasEvaluated('a');            // bool
$recorder->timesEvaluated('a');          // int
$recorder->evaluatedFlagKeys();          // ['a'] — exactly the flags touched, in order
$recorder->lastEvaluationOf('a')?->targetingKey();

Being a decorator, it can sit inside the breaker — which is how you prove an open circuit never reached the provider:

self::assertSame(0, $recorder->timesEvaluated('a'));

Testing the failure paths

Pin the outage rather than driving a real breaker to it:

$breaker = new ControllableCircuitBreaker();
$flags = Flipster::for($provider)->withDefaults(['kill' => true])
    ->withBreaker($breaker, 'flags')->evaluator();

$breaker->open('flags');
self::assertTrue($flags->isEnabled('kill'));      // the declared default

Driving a real breaker means encoding its threshold, strategy, minimumRequests and timeWindow into a test that is about fallback behaviour and does not care about any of it. Pin the state and test one thing.

The breaker also counts what it was asked, so you can assert the decorator's bookkeeping directly:

$breaker->failureCount('flags');
$breaker->successCount('flags');
$breaker->availabilityCheckCount('flags');   // one per evaluation that reached the breaker

And to make the provider itself fail:

ThrowingProvider::always('backend down');
ThrowingProvider::forFlags(['flaky'], new InMemoryProvider(['steady' => true]));
ThrowingProvider::throwing(new MySdk\FlagNotFoundException('nope'));

The per-flag mode matters more than it looks: it is how you verify that a flag-level exception from your provider's SDK does not trip the breaker for everything else.

Determinism

None of these read the clock or sleep. A resilience suite that depends on timing fails in CI for reasons nobody investigates, and then gets muted.

If you need to test threshold behaviour rather than pinned state, inject time into your own breaker implementation rather than sleeping.

Asserting every flag you read is declared

FlagEvaluator throws for undeclared flags, so a typo fails loudly at runtime. You can also check it up front:

$declared = Flipster::for($provider)->withDefaults($myMap)->defaults()->declaredKeys();
self::assertContains('new-checkout', $declared);