-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathClientSpecificationTest.php
executable file
·129 lines (114 loc) · 4.82 KB
/
ClientSpecificationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Unleash\Client\Tests;
use GuzzleHttp\Psr7\HttpFactory;
use Unleash\Client\Configuration\UnleashConfiguration;
use Unleash\Client\Configuration\UnleashContext;
use Unleash\Client\DefaultUnleash;
use Unleash\Client\DTO\Feature;
use Unleash\Client\DTO\Variant;
use Unleash\Client\Metrics\MetricsHandler;
use Unleash\Client\Repository\DefaultUnleashRepository;
use Unleash\Client\Stickiness\MurmurHashCalculator;
use Unleash\Client\Strategy\DefaultStrategyHandler;
use Unleash\Client\Strategy\GradualRolloutRandomStrategyHandler;
use Unleash\Client\Strategy\GradualRolloutSessionIdStrategyHandler;
use Unleash\Client\Strategy\GradualRolloutStrategyHandler;
use Unleash\Client\Strategy\GradualRolloutUserIdStrategyHandler;
use Unleash\Client\Strategy\IpAddressStrategyHandler;
use Unleash\Client\Strategy\UserIdStrategyHandler;
use Unleash\Client\Tests\Traits\RealCacheImplementationTrait;
use Unleash\Client\Variant\DefaultVariantHandler;
final class ClientSpecificationTest extends AbstractHttpClientTestCase
{
use RealCacheImplementationTrait;
public function testClientSpecifications()
{
$stickinessCalculator = new MurmurHashCalculator();
$gradualRolloutStrategy = new GradualRolloutStrategyHandler($stickinessCalculator);
$configuration = (new UnleashConfiguration('', '', ''))
->setAutoRegistrationEnabled(false)
->setCache($this->getCache());
$this->repository = new DefaultUnleashRepository(
$this->httpClient,
new HttpFactory(),
$configuration
);
$unleash = new DefaultUnleash(
[
new DefaultStrategyHandler(),
$gradualRolloutStrategy,
new IpAddressStrategyHandler(),
new UserIdStrategyHandler(),
new GradualRolloutUserIdStrategyHandler($gradualRolloutStrategy),
new GradualRolloutSessionIdStrategyHandler($gradualRolloutStrategy),
new GradualRolloutRandomStrategyHandler($gradualRolloutStrategy),
],
$this->repository,
$this->registrationService,
$configuration,
new class implements MetricsHandler {
public function handleMetrics(Feature $feature, bool $successful, ?Variant $variant = null): void
{
}
},
new DefaultVariantHandler($stickinessCalculator)
);
$specificationList = $this->getJson('index.json');
$disabledFeatureTests = [];
foreach ($specificationList as $specificationFilename) {
if (in_array($specificationFilename, $disabledFeatureTests, true)) {
continue;
}
$specificationConfig = $this->getJson($specificationFilename);
foreach ($specificationConfig['tests'] ?? [] as $test) {
$this->pushResponse($specificationConfig['state']);
self::assertEquals(
$test['expectedResult'],
$unleash->isEnabled($test['toggleName'], $this->createContext($test['context'])),
$test['description']
);
$configuration->setCache($this->getFreshCacheInstance());
}
foreach ($specificationConfig['variantTests'] ?? [] as $variantTest) {
$this->pushResponse($specificationConfig['state']);
self::assertEquals(
$variantTest['expectedResult'],
$unleash
->getVariant($variantTest['toggleName'], $this->createContext($variantTest['context']))
->jsonSerialize(),
$variantTest['description']
);
$configuration->setCache($this->getFreshCacheInstance());
}
}
}
private function getJson(string $filename): array
{
return json_decode(
file_get_contents(__DIR__ . "/client-specification/specifications/{$filename}"),
true,
512,
JSON_THROW_ON_ERROR
);
}
private function createContext(array $context): UnleashContext
{
$contextObject = (new UnleashContext())
->setCurrentUserId($context['userId'] ?? null)
->setSessionId($context['sessionId'] ?? null)
->setEnvironment($context['environment'] ?? null)
->setIpAddress($context['remoteAddress'] ?? '');
if (isset($context['properties'])) {
foreach ($context['properties'] as $property => $value) {
$contextObject->setCustomProperty($property, $value);
}
}
foreach ($context as $key => $value) {
if ($key === 'properties') {
continue;
}
$contextObject->setCustomProperty($key, $value);
}
return $contextObject;
}
}