-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathClientFixtures.php
More file actions
116 lines (96 loc) · 3.45 KB
/
ClientFixtures.php
File metadata and controls
116 lines (96 loc) · 3.45 KB
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
<?php
namespace FlagsmithTest;
use DoppioGancio\MockedClient\HandlerBuilder;
use DoppioGancio\MockedClient\MockedGuzzleClientBuilder;
use DoppioGancio\MockedClient\Route\RouteBuilder;
use Flagsmith\Engine\Environments\EnvironmentModel;
use Flagsmith\Flagsmith;
use Flagsmith\Utils\AnalyticsProcessor;
use GuzzleHttp\Psr7\Response;
use Http\Discovery\Psr17FactoryDiscovery;
class ClientFixtures
{
private const DATA_DIR = __DIR__ . DIRECTORY_SEPARATOR . 'Data' . DIRECTORY_SEPARATOR ;
public static function analyticsProcessor($client = null)
{
return new AnalyticsProcessor('api-key', 'http://host', 5, $client);
}
public static function getHandlerBuilder()
{
return new HandlerBuilder(
Psr17FactoryDiscovery::findServerRequestFactory(),
);
}
public static function getRouteBuilder()
{
return new RouteBuilder(
Psr17FactoryDiscovery::findResponseFactory(),
Psr17FactoryDiscovery::findStreamFactory(),
);
}
public static function getMockClient($handlerBuilder = null, $addRoutes = true)
{
$handlerBuilder = $handlerBuilder ?? self::getHandlerBuilder();
if ($addRoutes) {
$rb = self::getRouteBuilder();
// Route with Response
$handlerBuilder->addRoute(
$rb->new()
->withMethod('GET')
->withPath('/api/v1/environment-document/')
->withFileResponse(self::DATA_DIR . 'environment.json')
->build()
);
// Route with Response
$handlerBuilder->addRoute(
$rb->new()
->withMethod('POST')
->withPath('/api/v1/identities/')
->withFileResponse(self::DATA_DIR . 'identities.json')
->build()
);
// Route with Response
$handlerBuilder->addRoute(
$rb->new()
->withMethod('GET')
->withPath('/api/v1/flags/')
->withFileResponse(self::DATA_DIR . 'flags.json')
->build()
);
}
$clientBuilder = new MockedGuzzleClientBuilder($handlerBuilder);
return $clientBuilder->build();
}
private static function loadFileContents(string $file)
{
return file_get_contents(self::DATA_DIR . $file);
}
public static function localEvalFlagsmith()
{
$flagsmith = (new Flagsmith('ser.api_key', Flagsmith::DEFAULT_API_URL, null, 10))
->withClient(ClientFixtures::getMockClient());
$flagsmith->updateEnvironment();
yield $flagsmith;
unset($flagsmith);
}
public static function getEnvironmentModel()
{
return EnvironmentModel::build(json_decode(self::loadFileContents('environment.json')));
}
public static function getFlags()
{
return EnvironmentModel::build(json_decode(self::loadFileContents('environment.json')));
}
public static function getMockClientWithSegmentOverride()
{
$handlerBuilder = self::getHandlerBuilder();
$handlerBuilder->addRoute(
self::getRouteBuilder()->new()
->withMethod('GET')
->withPath('/api/v1/environment-document/')
->withFileResponse(self::DATA_DIR . 'environment_with_segment_override.json')
->build()
);
return self::getMockClient($handlerBuilder, false);
}
}