|
12 | 12 | use Symfony\Component\HttpClient\MockHttpClient;
|
13 | 13 | use Psr\Log\NullLogger;
|
14 | 14 |
|
| 15 | +// This is a mock class to eliminate the sleep from the rate limit handling |
| 16 | +class TestStoryApi extends \Storyblok\ManagementApi\Endpoints\StoryApi |
| 17 | +{ |
| 18 | + #[\Override] |
| 19 | + protected function handleRateLimit(): void |
| 20 | + { |
| 21 | + // No sleep and no logs for testing |
| 22 | + } |
| 23 | +} |
| 24 | + |
15 | 25 | test('Testing One Story, StoryData', function (): void {
|
16 | 26 | $responses = [
|
17 | 27 | \mockResponse("one-story", 200),
|
@@ -235,6 +245,190 @@ public function error(string|\Stringable $message, array $context = []): void
|
235 | 245 | foreach ($storyblokResponse as $story) {
|
236 | 246 | expect($story->name())->toBe("My third post");
|
237 | 247 | }
|
| 248 | +}); |
| 249 | + |
| 250 | +test('createBulk handles rate limiting and creates multiple stories', function (): void { |
| 251 | + $mockLogger = new class extends NullLogger { |
| 252 | + public array $logs = []; |
| 253 | + |
| 254 | + public function log($level, string|\Stringable $message, array $context = []): void |
| 255 | + { |
| 256 | + $this->logs[] = [ |
| 257 | + 'level' => $level, |
| 258 | + 'message' => $message, |
| 259 | + 'context' => $context |
| 260 | + ]; |
| 261 | + } |
| 262 | + |
| 263 | + public function warning(string|\Stringable $message, array $context = []): void |
| 264 | + { |
| 265 | + $this->log('warning', $message, $context); |
| 266 | + } |
| 267 | + }; |
| 268 | + |
| 269 | + $story1Data = [ |
| 270 | + 'story' => [ |
| 271 | + 'name' => 'Story 1', |
| 272 | + 'slug' => 'story-1', |
| 273 | + 'content' => ['component' => 'blog'], |
| 274 | + 'created_at' => '2024-02-08 09:40:59.123', |
| 275 | + 'published_at' => null, |
| 276 | + 'id' => 1, |
| 277 | + 'uuid' => '1234-5678' |
| 278 | + ] |
| 279 | + ]; |
| 280 | + |
| 281 | + $story2Data = [ |
| 282 | + 'story' => [ |
| 283 | + 'name' => 'Story 2', |
| 284 | + 'slug' => 'story-2', |
| 285 | + 'content' => ['component' => 'blog'], |
| 286 | + 'created_at' => '2024-02-08 09:41:59.123', |
| 287 | + 'published_at' => null, |
| 288 | + 'id' => 2, |
| 289 | + 'uuid' => '8765-4321' |
| 290 | + ] |
| 291 | + ]; |
| 292 | + |
| 293 | + $responses = [ |
| 294 | + // First story - Rate limit hit, then success |
| 295 | + \mockResponse('empty-story', 429, ['error' => 'Rate limit exceeded']), |
| 296 | + new MockResponse(json_encode($story1Data), [ |
| 297 | + 'http_code' => 201, |
| 298 | + 'response_headers' => ['Content-Type: application/json'], |
| 299 | + ]), |
| 300 | + // Second story - Immediate success |
| 301 | + new MockResponse(json_encode($story2Data), [ |
| 302 | + 'http_code' => 201, |
| 303 | + 'response_headers' => ['Content-Type: application/json'], |
| 304 | + ]), |
| 305 | + ]; |
| 306 | + |
| 307 | + $client = new MockHttpClient($responses); |
| 308 | + $mapiClient = ManagementApiClient::initTest($client); |
| 309 | + |
| 310 | + // Use TestStoryApi instead of regular StoryApi |
| 311 | + $storyApi = new TestStoryApi($client, '222', $mockLogger); |
| 312 | + |
| 313 | + // Create test stories |
| 314 | + $stories = [ |
| 315 | + StoryData::make([ |
| 316 | + 'name' => 'Story 1', |
| 317 | + 'slug' => 'story-1', |
| 318 | + 'content' => ['component' => 'blog'] |
| 319 | + ]), |
| 320 | + StoryData::make([ |
| 321 | + 'name' => 'Story 2', |
| 322 | + 'slug' => 'story-2', |
| 323 | + 'content' => ['component' => 'blog'] |
| 324 | + ]), |
| 325 | + ]; |
| 326 | + |
| 327 | + // Execute bulk creation |
| 328 | + $createdStories = iterator_to_array($storyApi->createBulk($stories)); |
| 329 | + |
| 330 | + // Verify number of created stories |
| 331 | + expect($createdStories)->toHaveCount(2); |
| 332 | + |
| 333 | + // Verify rate limit warning was logged |
| 334 | + $hasRateLimitWarning = false; |
| 335 | + foreach ($mockLogger->logs as $log) { |
| 336 | + if ($log['level'] === 'warning' && $log['message'] === 'Rate limit reached while creating story, retrying...') { |
| 337 | + $hasRateLimitWarning = true; |
| 338 | + break; |
| 339 | + } |
| 340 | + } |
| 341 | + |
| 342 | + expect($hasRateLimitWarning)->toBeTrue(); |
| 343 | + |
| 344 | + // Verify created stories |
| 345 | + expect($createdStories[0]->name())->toBe('Story 1'); |
| 346 | + expect($createdStories[1]->name())->toBe('Story 2'); |
| 347 | + expect($createdStories[0]->slug())->toBe('story-1'); |
| 348 | + expect($createdStories[1]->slug())->toBe('story-2'); |
| 349 | +}); |
| 350 | + |
| 351 | +test('createBulk throws exception when max retries is reached', function (): void { |
| 352 | + $mockLogger = new class extends NullLogger { |
| 353 | + public array $logs = []; |
| 354 | + |
| 355 | + public function log($level, string|\Stringable $message, array $context = []): void |
| 356 | + { |
| 357 | + $this->logs[] = [ |
| 358 | + 'level' => $level, |
| 359 | + 'message' => $message, |
| 360 | + 'context' => $context |
| 361 | + ]; |
| 362 | + } |
| 363 | + |
| 364 | + public function warning(string|\Stringable $message, array $context = []): void |
| 365 | + { |
| 366 | + $this->log('warning', $message, $context); |
| 367 | + } |
| 368 | + |
| 369 | + public function error(string|\Stringable $message, array $context = []): void |
| 370 | + { |
| 371 | + $this->log('error', $message, $context); |
| 372 | + } |
| 373 | + }; |
| 374 | + |
| 375 | + // Create responses that always return rate limit error (429) |
| 376 | + // We need MAX_RETRIES + 1 responses to trigger the exception |
| 377 | + $responses = array_fill(0, 4, new MockResponse(json_encode([ |
| 378 | + 'error' => 'Rate limit exceeded' |
| 379 | + ]), [ |
| 380 | + 'http_code' => 429, |
| 381 | + 'response_headers' => ['Content-Type: application/json'], |
| 382 | + ])); |
| 383 | + |
| 384 | + $client = new MockHttpClient($responses); |
| 385 | + $mapiClient = ManagementApiClient::initTest($client); |
| 386 | + |
| 387 | + // Use TestStoryApi instead of regular StoryApi |
| 388 | + $storyApi = new TestStoryApi($client, '222', $mockLogger); |
| 389 | + |
| 390 | + // Create test story |
| 391 | + $stories = [ |
| 392 | + StoryData::make([ |
| 393 | + 'name' => 'Story 1', |
| 394 | + 'slug' => 'story-1', |
| 395 | + 'content' => ['component' => 'blog'] |
| 396 | + ]), |
| 397 | + ]; |
| 398 | + |
| 399 | + // Execute bulk creation and expect exception |
| 400 | + expect(fn (): array => iterator_to_array($storyApi->createBulk($stories))) |
| 401 | + ->toThrow( |
| 402 | + \Storyblok\ManagementApi\Exceptions\StoryblokApiException::class, |
| 403 | + 'Rate limit exceeded maximum retries' |
| 404 | + ); |
| 405 | + |
| 406 | + // Verify warning logs for each retry |
| 407 | + $warningCount = 0; |
| 408 | + $hasErrorLog = false; |
| 409 | + |
| 410 | + foreach ($mockLogger->logs as $log) { |
| 411 | + if ($log['level'] === 'warning' && |
| 412 | + $log['message'] === 'Rate limit reached while creating story, retrying...' |
| 413 | + ) { |
| 414 | + ++$warningCount; |
| 415 | + } |
| 416 | + |
| 417 | + if ($log['level'] === 'error' && |
| 418 | + $log['message'] === 'Max retries reached while creating story' |
| 419 | + ) { |
| 420 | + $hasErrorLog = true; |
| 421 | + } |
| 422 | + } |
| 423 | + |
| 424 | + // We should see MAX_RETRIES number of warning logs |
| 425 | + expect($warningCount)->toBe(3) |
| 426 | + ->and($hasErrorLog)->toBeTrue(); |
238 | 427 |
|
| 428 | + // Verify the last log context contains story information |
| 429 | + $lastErrorLog = array_filter($mockLogger->logs, fn($log): bool => $log['level'] === 'error'); |
| 430 | + $lastErrorLog = end($lastErrorLog); |
239 | 431 |
|
| 432 | + expect($lastErrorLog['context'])->toHaveKey('story_name') |
| 433 | + ->and($lastErrorLog['context']['story_name'])->toBe('Story 1'); |
240 | 434 | });
|
0 commit comments