PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / apimatic / core / tests / Mocking / MockHelper.php
ameliabooking / vendor / apimatic / core / tests / Mocking Last commit date
Authentication 1 year ago Logger 1 year ago Other 1 year ago Response 1 year ago Types 1 year ago MockConverter.php 1 year ago MockHelper.php 1 year ago MockHttpClient.php 1 year ago
MockHelper.php
238 lines
1 <?php
2
3 namespace Core\Tests\Mocking;
4
5 use Core\ApiCall;
6 use Core\Client;
7 use Core\ClientBuilder;
8 use Core\Logger\Configuration\LoggingConfiguration;
9 use Core\Logger\Configuration\RequestConfiguration;
10 use Core\Logger\Configuration\ResponseConfiguration;
11 use Core\Request\Parameters\AdditionalHeaderParams;
12 use Core\Request\Parameters\HeaderParam;
13 use Core\Request\Parameters\TemplateParam;
14 use Core\Response\ResponseHandler;
15 use Core\Response\Types\ErrorType;
16 use Core\Tests\Mocking\Authentication\FormAuthManager;
17 use Core\Tests\Mocking\Authentication\HeaderAuthManager;
18 use Core\Tests\Mocking\Authentication\QueryAuthManager;
19 use Core\Tests\Mocking\Logger\MockLogger;
20 use Core\Tests\Mocking\Other\MockChild1;
21 use Core\Tests\Mocking\Other\MockChild2;
22 use Core\Tests\Mocking\Other\MockChild3;
23 use Core\Tests\Mocking\Other\MockClass;
24 use Core\Tests\Mocking\Other\MockException1;
25 use Core\Tests\Mocking\Other\MockException2;
26 use Core\Tests\Mocking\Response\MockResponse;
27 use Core\Tests\Mocking\Types\MockCallback;
28 use Core\Tests\Mocking\Types\MockFileWrapper;
29 use Core\Types\CallbackCatcher;
30 use Core\Utils\JsonHelper;
31 use Psr\Log\LoggerInterface;
32 use Psr\Log\LogLevel;
33
34 class MockHelper
35 {
36 /**
37 * @var Client
38 */
39 private static $client;
40
41 /**
42 * @var JsonHelper
43 */
44 private static $jsonHelper;
45
46 /**
47 * @var MockResponse
48 */
49 private static $response;
50
51 /**
52 * @var CallbackCatcher
53 */
54 private static $callbackCatcher;
55
56 /**
57 * @var MockFileWrapper
58 */
59 private static $fileWrapper;
60
61 /**
62 * @var MockFileWrapper
63 */
64 private static $urlFileWrapper;
65
66 /**
67 * @var MockLogger
68 */
69 private static $logger;
70
71 public static function getClient(): Client
72 {
73 if (!isset(self::$client)) {
74 $clientBuilder = ClientBuilder::init(new MockHttpClient())
75 ->converter(new MockConverter())
76 ->apiCallback(self::getCallbackCatcher())
77 ->loggingConfiguration(self::getLoggingConfiguration())
78 ->serverUrls([
79 'Server1' => 'http://my/path:3000/{one}',
80 'Server2' => 'https://my/path/{two}'
81 ], 'Server1')
82 ->jsonHelper(self::getJsonHelper())
83 ->globalConfig([
84 TemplateParam::init('one', 'v1')->dontEncode(),
85 TemplateParam::init('two', 'v2')->dontEncode(),
86 HeaderParam::init('additionalHead1', 'headVal1'),
87 HeaderParam::init('additionalHead2', 'headVal2')
88 ])
89 ->globalRuntimeParam(AdditionalHeaderParams::init(['key5' => 890.098]))
90 ->globalErrors([
91 strval(400) => ErrorType::init('Exception num 1', MockException1::class),
92 strval(401) => ErrorType::init('Exception num 2', MockException2::class),
93 strval(403) => ErrorType::init('Exception num 3')
94 ])
95 ->authManagers([
96 "header" => new HeaderAuthManager('someAuthToken', 'accessToken'),
97 "headerWithNull" => new HeaderAuthManager('someAuthToken', null),
98 "headerWithEmpty" => new HeaderAuthManager('', 'accessToken'),
99 "query" => new QueryAuthManager('someAuthToken', 'accessToken'),
100 "queryWithNull" => new QueryAuthManager(null, 'accessToken'),
101 "form" => new FormAuthManager('someAuthToken', 'accessToken'),
102 "formWithNull" => new FormAuthManager('newAuthToken', null)
103 ])
104 ->userAgent("{language}|{version}|{engine}|{engine-version}|{os-info}")
105 ->userAgentConfig([
106 '{language}' => 'my lang',
107 '{version}' => '1.*.*'
108 ]);
109 self::$client = $clientBuilder->build();
110 // @phan-suppress-next-next-line PhanPluginDuplicateAdjacentStatement Following duplicated line will
111 // call `addUserAgentToGlobalHeaders` again to see test if its added again or not
112 self::$client = $clientBuilder->build();
113 }
114 return self::$client;
115 }
116
117 public static function newApiCall(): ApiCall
118 {
119 return new ApiCall(self::getClient());
120 }
121
122 public static function responseHandler(): ResponseHandler
123 {
124 return self::getClient()->getGlobalResponseHandler();
125 }
126
127 public static function getJsonHelper(): JsonHelper
128 {
129 if (!isset(self::$jsonHelper)) {
130 self::$jsonHelper = new JsonHelper(
131 [MockClass::class => [MockChild1::class, MockChild2::class, MockChild3::class]],
132 ['disc' => 'my field', 'disc1' => 'This is 1', 'disc2' => 'This is 2'],
133 'addAdditionalProperty',
134 'Core\\Tests\\Mocking\\Other'
135 );
136 }
137 return self::$jsonHelper;
138 }
139
140 public static function getResponse(): MockResponse
141 {
142 if (!isset(self::$response)) {
143 self::$response = new MockResponse();
144 }
145 return self::$response;
146 }
147
148 public static function getCallback(): MockCallback
149 {
150 return new MockCallback();
151 }
152
153 public static function getCallbackCatcher(): CallbackCatcher
154 {
155 if (!isset(self::$callbackCatcher)) {
156 self::$callbackCatcher = new CallbackCatcher();
157 }
158 return self::$callbackCatcher;
159 }
160
161 public static function getFileWrapper(): MockFileWrapper
162 {
163 if (!isset(self::$fileWrapper)) {
164 $filePath = realpath(__DIR__ . '/Other/testFile.txt');
165 self::$fileWrapper = MockFileWrapper::createFromPath($filePath, 'text/plain', 'My Text');
166 }
167 return self::$fileWrapper;
168 }
169
170 public static function getFileWrapperFromUrl(): MockFileWrapper
171 {
172 if (!isset(self::$urlFileWrapper)) {
173 $filePath = MockFileWrapper::getDownloadedRealFilePath('https://gist.githubusercontent.com/asadali214' .
174 '/0a64efec5353d351818475f928c50767/raw/8ad3533799ecb4e01a753aaf04d248e6702d4947/testFile.txt');
175 self::$urlFileWrapper = MockFileWrapper::createFromPath($filePath, 'text/plain', 'My Text');
176 }
177 return self::$urlFileWrapper;
178 }
179
180 public static function getMockLogger(): MockLogger
181 {
182 if (!isset(self::$logger)) {
183 self::$logger = new MockLogger();
184 }
185 return self::$logger;
186 }
187
188 public static function getLoggingConfiguration(
189 ?string $logLevel = null,
190 ?bool $maskSensitiveHeaders = null,
191 ?RequestConfiguration $requestConfig = null,
192 ?ResponseConfiguration $responseConfig = null,
193 ?LoggerInterface $logger = null
194 ): LoggingConfiguration {
195 return new LoggingConfiguration(
196 $logger ?? self::getMockLogger(),
197 $logLevel ?? LogLevel::INFO,
198 $maskSensitiveHeaders ?? true,
199 $requestConfig ?? self::getRequestLoggingConfiguration(),
200 $responseConfig ?? self::getResponseLoggingConfiguration()
201 );
202 }
203
204 public static function getRequestLoggingConfiguration(
205 bool $includeQueryInPath = false,
206 bool $logBody = false,
207 bool $logHeaders = false,
208 array $headersToInclude = [],
209 array $headersToExclude = [],
210 array $headersToUnmask = []
211 ): RequestConfiguration {
212 return new RequestConfiguration(
213 $includeQueryInPath,
214 $logBody,
215 $logHeaders,
216 $headersToInclude,
217 $headersToExclude,
218 $headersToUnmask
219 );
220 }
221
222 public static function getResponseLoggingConfiguration(
223 bool $logBody = false,
224 bool $logHeaders = false,
225 array $headersToInclude = [],
226 array $headersToExclude = [],
227 array $headersToUnmask = []
228 ): ResponseConfiguration {
229 return new ResponseConfiguration(
230 $logBody,
231 $logHeaders,
232 $headersToInclude,
233 $headersToExclude,
234 $headersToUnmask
235 );
236 }
237 }
238