-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathAbstractHttpClientTestCase.php
executable file
·132 lines (111 loc) · 3.46 KB
/
AbstractHttpClientTestCase.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
130
131
132
<?php
namespace Unleash\Client\Tests;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\HttpFactory;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Throwable;
use Unleash\Client\Client\DefaultRegistrationService;
use Unleash\Client\Client\RegistrationService;
use Unleash\Client\Configuration\Context;
use Unleash\Client\Configuration\UnleashConfiguration;
use Unleash\Client\DTO\DefaultVariant;
use Unleash\Client\DTO\Feature;
use Unleash\Client\DTO\Variant;
use Unleash\Client\Metrics\MetricsHandler;
use Unleash\Client\Repository\DefaultUnleashRepository;
use Unleash\Client\Tests\Traits\FakeCacheImplementationTrait;
use Unleash\Client\Variant\VariantHandler;
abstract class AbstractHttpClientTestCase extends TestCase
{
use FakeCacheImplementationTrait;
/**
* @var MockHandler
*/
protected $mockHandler;
/**
* @var DefaultUnleashRepository
*/
protected $repository;
/**
* @var array[]
*/
protected $requestHistory = [];
/**
* @var HandlerStack
*/
protected $handlerStack;
/**
* @var Client
*/
protected $httpClient;
/**
* @var RegistrationService
*/
protected $registrationService;
/**
* @var MetricsHandler
*/
protected $metricsHandler;
/**
* @var VariantHandler
*/
protected $variantHandler;
protected function setUp(): void
{
$this->mockHandler = new MockHandler();
$this->handlerStack = HandlerStack::create($this->mockHandler);
$this->handlerStack->push(Middleware::history($this->requestHistory));
$this->httpClient = new Client([
'handler' => $this->handlerStack,
]);
$this->registrationService = new DefaultRegistrationService(
$this->httpClient,
new HttpFactory(),
(new UnleashConfiguration('', '', ''))
->setCache($this->getCache())
);
$this->repository = new DefaultUnleashRepository(
$this->httpClient,
new HttpFactory(),
(new UnleashConfiguration('', '', ''))
->setCache($this->getCache())
);
$this->metricsHandler = new class implements MetricsHandler {
public function handleMetrics(Feature $feature, bool $successful, ?Variant $variant = null): void
{
}
};
$this->variantHandler = new class implements VariantHandler {
public function getDefaultVariant(): Variant
{
return new DefaultVariant('test', false);
}
public function selectVariant(array $variants, string $groupId, Context $context): ?Variant
{
return null;
}
};
}
protected function tearDown(): void
{
self::assertEquals(0, $this->mockHandler->count(), 'Some responses are leftover in the mock queue');
}
/**
* @param array|Throwable $response
*/
protected function pushResponse($response, int $count = 1, int $statusCode = 200): void
{
for ($i = 0; $i < $count; ++$i) {
if (is_array($response)) {
$mocked = new Response($statusCode, ['Content-Type' => 'application/json'], json_encode($response));
} else {
$mocked = $response;
}
$this->mockHandler->append($mocked);
}
}
}