Skip to content

Commit 634470d

Browse files
authored
ci: Run the Flagsmith SDK behavioural spec (#147)
1 parent fd4bcc9 commit 634470d

5 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎.gitattributes‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
.prettierrc export-ignore
77
tests export-ignore
88
composer.phar export-ignore
9+
sdk-spec export-ignore

‎.github/workflows/sdk-spec.yml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: SDK spec
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
sdk-spec:
13+
# Keep the ref in `uses:` and `spec-ref` in sync.
14+
uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.3.0
15+
with:
16+
spec-ref: v0.3.0
17+
language: php

‎renovate.json‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
"ignorePresets": [":semanticPrefixFixDepsChoreOthers"],
55
"semanticCommitType": "deps",
66
"git-submodules": { "enabled": true },
7+
"customManagers": [
8+
{
9+
"customType": "regex",
10+
"managerFilePatterns": ["/^\\.github/workflows/sdk-spec\\.yml$/"],
11+
"matchStrings": ["spec-ref: (?<currentValue>v[\\d.]+)"],
12+
"depNameTemplate": "Flagsmith/sdk-spec",
13+
"datasourceTemplate": "github-tags"
14+
}
15+
],
716
"packageRules": [
817
{
918
"matchUpdateTypes": [
@@ -23,6 +32,11 @@
2332
"matchManagers": ["git-submodules"],
2433
"versioning": "semver-coerced",
2534
"enabled": true
35+
},
36+
{
37+
"matchPackageNames": ["Flagsmith/sdk-spec"],
38+
"groupName": "SDK spec",
39+
"enabled": true
2640
}
2741
]
2842
}

‎sdk-spec/Dockerfile‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Runs this SDK, built from source, under the Flagsmith SDK behavioural spec.
2+
# docker build -f sdk-spec/Dockerfile --build-context harness=<sdk-spec>/harness/php .
3+
FROM docker.io/library/php:8.4-cli
4+
5+
RUN apt-get update \
6+
&& apt-get install -y --no-install-recommends libgmp-dev unzip \
7+
&& docker-php-ext-install bcmath gmp \
8+
&& rm -rf /var/lib/apt/lists/*
9+
COPY --from=docker.io/library/composer:2 /usr/bin/composer /usr/bin/composer
10+
11+
WORKDIR /sdk
12+
COPY composer.json ./
13+
COPY src ./src
14+
# Install the SDK with Guzzle as its PSR-18 HTTP client, as an application would.
15+
RUN composer require guzzlehttp/guzzle:^7.3 --update-no-dev --optimize-autoloader --no-interaction --no-progress
16+
17+
COPY --from=harness harness.php /harness/harness.php
18+
COPY sdk-spec/steps.php ./sdk-spec/steps.php
19+
20+
ENTRYPOINT ["php", "/harness/harness.php", "sdk-spec/steps.php"]

‎sdk-spec/steps.php‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* Steps for the Flagsmith SDK behavioural spec: https://github.com/Flagsmith/sdk-spec
5+
*
6+
* Each function implements the Gherkin step in the comment above it.
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
use Flagsmith\Flagsmith;
12+
use Flagsmith\Models\Flags;
13+
14+
function asSpecFlags(Flags $flags): array
15+
{
16+
return array_map(
17+
fn ($flag) => ['feature' => $flag->getFeatureName(), 'enabled' => $flag->getEnabled(), 'value' => $flag->getValue()],
18+
$flags->allFlags(),
19+
);
20+
}
21+
22+
// Create the SDK client
23+
function createClient(string $environmentKey, string $apiUrl): Flagsmith
24+
{
25+
return new Flagsmith(apiKey: $environmentKey, host: $apiUrl);
26+
}
27+
28+
// When the application gets the environment flags
29+
function getEnvironmentFlags(Flagsmith $flagsmith): array
30+
{
31+
return asSpecFlags($flagsmith->getEnvironmentFlags());
32+
}
33+
34+
// When the application gets the flags for identity "{identifier}" with traits:
35+
function getIdentityFlags(Flagsmith $flagsmith, string $identifier, object $traits): array
36+
{
37+
return asSpecFlags($flagsmith->getIdentityFlags($identifier, $traits));
38+
}

0 commit comments

Comments
 (0)