|
6 | 6 |
|
7 | 7 | use Exception; |
8 | 8 | use InvalidArgumentException; |
9 | | -use Tests\TestCase; |
10 | 9 | use Storyblok\ManagementApi\Data\Story; |
11 | 10 | use Storyblok\ManagementApi\Data\StoryComponent; |
12 | 11 | use Storyblok\ManagementApi\Endpoints\StoryApi; |
| 12 | +use Storyblok\ManagementApi\Exceptions\InvalidStoryDataException; |
13 | 13 | use Storyblok\ManagementApi\Exceptions\StoryblokFormatException; |
14 | 14 | use Storyblok\ManagementApi\ManagementApiClient; |
15 | 15 | use Storyblok\ManagementApi\QueryParameters\Filters\Filter; |
|
18 | 18 | use Storyblok\ManagementApi\QueryParameters\StoriesParams; |
19 | 19 | use Symfony\Component\HttpClient\MockHttpClient; |
20 | 20 | use Symfony\Component\HttpClient\Response\MockResponse; |
| 21 | +use Tests\TestCase; |
21 | 22 |
|
22 | 23 | final class StoryApiTest extends TestCase |
23 | 24 | { |
@@ -383,4 +384,258 @@ public function testGetUuids(): void |
383 | 384 | $this->assertSame("e656e146-f4ed-44a2-8017-013e5a9d9395", $array[1]); |
384 | 385 | $this->assertSame("e656e146-f4ed-44a2-8017-013e5a9d9396", $array[0]); |
385 | 386 | } |
| 387 | + |
| 388 | + public function testGetStoryWithEmptyIdThrowsException(): void |
| 389 | + { |
| 390 | + $client = new MockHttpClient([]); |
| 391 | + $mapiClient = ManagementApiClient::initTest($client); |
| 392 | + $storyApi = new StoryApi($mapiClient, "222"); |
| 393 | + |
| 394 | + $this->expectException(InvalidArgumentException::class); |
| 395 | + $this->expectExceptionMessage("Story ID cannot be empty"); |
| 396 | + |
| 397 | + $storyApi->get(""); |
| 398 | + } |
| 399 | + |
| 400 | + public function testGetStoryWithZeroIdThrowsException(): void |
| 401 | + { |
| 402 | + $client = new MockHttpClient([]); |
| 403 | + $mapiClient = ManagementApiClient::initTest($client); |
| 404 | + $storyApi = new StoryApi($mapiClient, "222"); |
| 405 | + |
| 406 | + $this->expectException(InvalidArgumentException::class); |
| 407 | + $this->expectExceptionMessage("Story ID cannot be empty"); |
| 408 | + |
| 409 | + $storyApi->get("0"); |
| 410 | + } |
| 411 | + |
| 412 | + public function testUpdateStoryWithEmptyIdThrowsException(): void |
| 413 | + { |
| 414 | + $client = new MockHttpClient([]); |
| 415 | + $mapiClient = ManagementApiClient::initTest($client); |
| 416 | + $storyApi = new StoryApi($mapiClient, "222"); |
| 417 | + |
| 418 | + $storyData = new Story("test", "test-slug", new StoryComponent("page")); |
| 419 | + |
| 420 | + $this->expectException(InvalidArgumentException::class); |
| 421 | + $this->expectExceptionMessage("Story ID cannot be empty"); |
| 422 | + |
| 423 | + $storyApi->update("", $storyData); |
| 424 | + } |
| 425 | + |
| 426 | + public function testPublishStoryWithEmptyIdThrowsException(): void |
| 427 | + { |
| 428 | + $client = new MockHttpClient([]); |
| 429 | + $mapiClient = ManagementApiClient::initTest($client); |
| 430 | + $storyApi = new StoryApi($mapiClient, "222"); |
| 431 | + |
| 432 | + $this->expectException(InvalidArgumentException::class); |
| 433 | + $this->expectExceptionMessage("Story ID cannot be empty"); |
| 434 | + |
| 435 | + $storyApi->publish(""); |
| 436 | + } |
| 437 | + |
| 438 | + public function testUnpublishStoryWithEmptyIdThrowsException(): void |
| 439 | + { |
| 440 | + $client = new MockHttpClient([]); |
| 441 | + $mapiClient = ManagementApiClient::initTest($client); |
| 442 | + $storyApi = new StoryApi($mapiClient, "222"); |
| 443 | + |
| 444 | + $this->expectException(InvalidArgumentException::class); |
| 445 | + $this->expectExceptionMessage("Story ID cannot be empty"); |
| 446 | + |
| 447 | + $storyApi->unpublish(""); |
| 448 | + } |
| 449 | + |
| 450 | + public function testCreateStoryWithInvalidDataThrowsException(): void |
| 451 | + { |
| 452 | + $client = new MockHttpClient([]); |
| 453 | + $mapiClient = ManagementApiClient::initTest($client); |
| 454 | + $storyApi = new StoryApi($mapiClient, "222"); |
| 455 | + |
| 456 | + $storyData = new Story("test", "test-slug", new StoryComponent("page")); |
| 457 | + // Remove name to make it invalid |
| 458 | + $storyData->setData(["slug" => "test-slug"]); |
| 459 | + |
| 460 | + $this->expectException(InvalidStoryDataException::class); |
| 461 | + $this->expectExceptionMessage("Invalid story data provided"); |
| 462 | + |
| 463 | + $storyApi->create($storyData); |
| 464 | + } |
| 465 | + |
| 466 | + public function testCreateStoryWithPublishFlag(): void |
| 467 | + { |
| 468 | + $response = new MockResponse( |
| 469 | + json_encode([ |
| 470 | + "story" => [ |
| 471 | + "name" => "Published Story", |
| 472 | + "slug" => "published-story", |
| 473 | + "content" => ["component" => "page"], |
| 474 | + ], |
| 475 | + ], JSON_THROW_ON_ERROR), |
| 476 | + [ |
| 477 | + "http_code" => 201, |
| 478 | + "response_headers" => ["Content-Type: application/json"], |
| 479 | + ], |
| 480 | + ); |
| 481 | + |
| 482 | + $client = new MockHttpClient([$response], "https://api.storyblok.com"); |
| 483 | + $mapiClient = ManagementApiClient::initTest($client); |
| 484 | + $storyApi = new StoryApi($mapiClient, "222"); |
| 485 | + |
| 486 | + $storyData = new Story( |
| 487 | + "Published Story", |
| 488 | + "published-story", |
| 489 | + new StoryComponent("page"), |
| 490 | + ); |
| 491 | + |
| 492 | + $storyResponse = $storyApi->create($storyData, publish: true); |
| 493 | + |
| 494 | + $this->assertTrue($storyResponse->isOk()); |
| 495 | + $this->assertSame(201, $storyResponse->getResponseStatusCode()); |
| 496 | + } |
| 497 | + |
| 498 | + public function testCreateStoryWithReleaseId(): void |
| 499 | + { |
| 500 | + $response = new MockResponse( |
| 501 | + json_encode([ |
| 502 | + "story" => [ |
| 503 | + "name" => "Release Story", |
| 504 | + "slug" => "release-story", |
| 505 | + "content" => ["component" => "page"], |
| 506 | + ], |
| 507 | + ], JSON_THROW_ON_ERROR), |
| 508 | + [ |
| 509 | + "http_code" => 201, |
| 510 | + "response_headers" => ["Content-Type: application/json"], |
| 511 | + ], |
| 512 | + ); |
| 513 | + |
| 514 | + $client = new MockHttpClient([$response], "https://api.storyblok.com"); |
| 515 | + $mapiClient = ManagementApiClient::initTest($client); |
| 516 | + $storyApi = new StoryApi($mapiClient, "222"); |
| 517 | + |
| 518 | + $storyData = new Story( |
| 519 | + "Release Story", |
| 520 | + "release-story", |
| 521 | + new StoryComponent("page"), |
| 522 | + ); |
| 523 | + |
| 524 | + $storyResponse = $storyApi->create($storyData, releaseId: 12345); |
| 525 | + |
| 526 | + $this->assertTrue($storyResponse->isOk()); |
| 527 | + $this->assertSame(201, $storyResponse->getResponseStatusCode()); |
| 528 | + } |
| 529 | + |
| 530 | + public function testUpdateStoryWithAllOptionalParameters(): void |
| 531 | + { |
| 532 | + $responses = [$this->mockResponse("one-story", 200)]; |
| 533 | + |
| 534 | + $client = new MockHttpClient($responses); |
| 535 | + $mapiClient = ManagementApiClient::initTest($client); |
| 536 | + $storyApi = new StoryApi($mapiClient, "222"); |
| 537 | + |
| 538 | + $storyData = new Story("test", "test-slug", new StoryComponent("page")); |
| 539 | + |
| 540 | + $storyResponse = $storyApi->update( |
| 541 | + storyId: "111", |
| 542 | + storyData: $storyData, |
| 543 | + groupId: "group-uuid-123", |
| 544 | + forceUpdate: "1", |
| 545 | + releaseId: 12345, |
| 546 | + publish: true, |
| 547 | + lang: "de", |
| 548 | + ); |
| 549 | + |
| 550 | + $this->assertTrue($storyResponse->isOk()); |
| 551 | + $this->assertSame(200, $storyResponse->getResponseStatusCode()); |
| 552 | + } |
| 553 | + |
| 554 | + public function testPublishStoryWithoutOptionalParameters(): void |
| 555 | + { |
| 556 | + $responses = [$this->mockResponse("one-story", 200)]; |
| 557 | + |
| 558 | + $client = new MockHttpClient($responses); |
| 559 | + $mapiClient = ManagementApiClient::initTest($client); |
| 560 | + $storyApi = new StoryApi($mapiClient, "222"); |
| 561 | + |
| 562 | + $storyResponse = $storyApi->publish("111"); |
| 563 | + |
| 564 | + $this->assertTrue($storyResponse->isOk()); |
| 565 | + $this->assertSame(200, $storyResponse->getResponseStatusCode()); |
| 566 | + } |
| 567 | + |
| 568 | + public function testUnpublishStoryWithoutLanguage(): void |
| 569 | + { |
| 570 | + $responses = [$this->mockResponse("one-story", 200)]; |
| 571 | + |
| 572 | + $client = new MockHttpClient($responses); |
| 573 | + $mapiClient = ManagementApiClient::initTest($client); |
| 574 | + $storyApi = new StoryApi($mapiClient, "222"); |
| 575 | + |
| 576 | + $storyResponse = $storyApi->unpublish("111"); |
| 577 | + |
| 578 | + $this->assertTrue($storyResponse->isOk()); |
| 579 | + $this->assertSame(200, $storyResponse->getResponseStatusCode()); |
| 580 | + } |
| 581 | + |
| 582 | + public function testPageWithDefaultParameters(): void |
| 583 | + { |
| 584 | + $responses = [ |
| 585 | + $this->mockResponse("list-stories", 200, [ |
| 586 | + "total" => 2, |
| 587 | + "per-page" => 25, |
| 588 | + ]), |
| 589 | + ]; |
| 590 | + |
| 591 | + $client = new MockHttpClient($responses); |
| 592 | + $mapiClient = ManagementApiClient::initTest($client); |
| 593 | + $storyApi = new StoryApi($mapiClient, "222"); |
| 594 | + |
| 595 | + $storyResponse = $storyApi->page(); |
| 596 | + $url = $storyResponse->getLastCalledUrl(); |
| 597 | + |
| 598 | + $this->assertMatchesRegularExpression( |
| 599 | + '/.*\/v1\/spaces\/222\/stories\?page=1&per_page=25$/', |
| 600 | + $url, |
| 601 | + ); |
| 602 | + $this->assertTrue($storyResponse->isOk()); |
| 603 | + } |
| 604 | + |
| 605 | + public function testCreateStoryWithDefaultContent(): void |
| 606 | + { |
| 607 | + $response = new MockResponse( |
| 608 | + json_encode([ |
| 609 | + "story" => [ |
| 610 | + "name" => "No Content Story", |
| 611 | + "slug" => "no-content-story", |
| 612 | + "content" => ["component" => "default-type"], |
| 613 | + ], |
| 614 | + ], JSON_THROW_ON_ERROR), |
| 615 | + [ |
| 616 | + "http_code" => 201, |
| 617 | + "response_headers" => ["Content-Type: application/json"], |
| 618 | + ], |
| 619 | + ); |
| 620 | + |
| 621 | + $client = new MockHttpClient([$response], "https://api.storyblok.com"); |
| 622 | + $mapiClient = ManagementApiClient::initTest($client); |
| 623 | + $storyApi = new StoryApi($mapiClient, "222"); |
| 624 | + |
| 625 | + // Create story without content, but set default content type |
| 626 | + $storyData = new Story( |
| 627 | + "No Content Story", |
| 628 | + "no-content-story", |
| 629 | + new StoryComponent("page"), |
| 630 | + ); |
| 631 | + $storyData->setContentType("default-type"); |
| 632 | + // Remove content to trigger default content creation |
| 633 | + $data = $storyData->toArray(); |
| 634 | + unset($data["content"]); |
| 635 | + $storyData->setData($data); |
| 636 | + |
| 637 | + $storyResponse = $storyApi->create($storyData); |
| 638 | + |
| 639 | + $this->assertTrue($storyResponse->isOk()); |
| 640 | + } |
386 | 641 | } |
0 commit comments