|
4 | 4 |
|
5 | 5 | namespace Tests\Feature; |
6 | 6 |
|
7 | | -use Tests\TestCase; |
| 7 | +use InvalidArgumentException; |
8 | 8 | use Storyblok\ManagementApi\Data\Component; |
9 | 9 | use Storyblok\ManagementApi\Endpoints\ComponentApi; |
| 10 | +use Storyblok\ManagementApi\Exceptions\InvalidStoryDataException; |
10 | 11 | use Storyblok\ManagementApi\ManagementApiClient; |
11 | 12 | use Storyblok\ManagementApi\QueryParameters\ComponentsParams; |
12 | 13 | use Symfony\Component\HttpClient\MockHttpClient; |
| 14 | +use Tests\TestCase; |
13 | 15 |
|
14 | 16 | final class ComponentApiTest extends TestCase |
15 | 17 | { |
@@ -79,4 +81,101 @@ public function testGettingOneComponent(): void |
79 | 81 | $this->assertSame("2025-04-15T21:32:55.495Z", $component->updatedAt()); |
80 | 82 | $this->assertFalse($component->isRoot()); |
81 | 83 | } |
| 84 | + |
| 85 | + public function testCreateComponent(): void |
| 86 | + { |
| 87 | + $responses = [$this->mockResponse("one-component", 200)]; |
| 88 | + |
| 89 | + $client = new MockHttpClient($responses); |
| 90 | + $mapiClient = ManagementApiClient::initTest($client); |
| 91 | + $componentApi = new ComponentApi($mapiClient, "2222"); |
| 92 | + |
| 93 | + $componentData = new Component("new-component"); |
| 94 | + $componentData->setDisplayName("New Component"); |
| 95 | + $componentData->setRoot(true); |
| 96 | + |
| 97 | + $componentResponse = $componentApi->create($componentData); |
| 98 | + $url = $componentResponse->getLastCalledUrl(); |
| 99 | + |
| 100 | + $this->assertMatchesRegularExpression( |
| 101 | + '/.*\/v1\/spaces\/2222\/components$/', |
| 102 | + $url, |
| 103 | + ); |
| 104 | + $this->assertTrue($componentResponse->isOk()); |
| 105 | + } |
| 106 | + |
| 107 | + public function testUpdateComponent(): void |
| 108 | + { |
| 109 | + $responses = [$this->mockResponse("one-component", 200)]; |
| 110 | + |
| 111 | + $client = new MockHttpClient($responses); |
| 112 | + $mapiClient = ManagementApiClient::initTest($client); |
| 113 | + $componentApi = new ComponentApi($mapiClient, "2222"); |
| 114 | + |
| 115 | + $componentData = new Component("text-section"); |
| 116 | + $componentData->setDisplayName("Updated Text Section"); |
| 117 | + |
| 118 | + $componentResponse = $componentApi->update("7223149", $componentData); |
| 119 | + $url = $componentResponse->getLastCalledUrl(); |
| 120 | + |
| 121 | + $this->assertMatchesRegularExpression( |
| 122 | + '/.*\/v1\/spaces\/2222\/components\/7223149$/', |
| 123 | + $url, |
| 124 | + ); |
| 125 | + $this->assertTrue($componentResponse->isOk()); |
| 126 | + } |
| 127 | + |
| 128 | + public function testGetComponentWithEmptyIdThrowsException(): void |
| 129 | + { |
| 130 | + $client = new MockHttpClient([]); |
| 131 | + $mapiClient = ManagementApiClient::initTest($client); |
| 132 | + $componentApi = new ComponentApi($mapiClient, "2222"); |
| 133 | + |
| 134 | + $this->expectException(InvalidArgumentException::class); |
| 135 | + $this->expectExceptionMessage("Component ID cannot be empty"); |
| 136 | + |
| 137 | + $componentApi->get(""); |
| 138 | + } |
| 139 | + |
| 140 | + public function testGetComponentWithZeroIdThrowsException(): void |
| 141 | + { |
| 142 | + $client = new MockHttpClient([]); |
| 143 | + $mapiClient = ManagementApiClient::initTest($client); |
| 144 | + $componentApi = new ComponentApi($mapiClient, "2222"); |
| 145 | + |
| 146 | + $this->expectException(InvalidArgumentException::class); |
| 147 | + $this->expectExceptionMessage("Component ID cannot be empty"); |
| 148 | + |
| 149 | + $componentApi->get("0"); |
| 150 | + } |
| 151 | + |
| 152 | + public function testUpdateComponentWithEmptyIdThrowsException(): void |
| 153 | + { |
| 154 | + $client = new MockHttpClient([]); |
| 155 | + $mapiClient = ManagementApiClient::initTest($client); |
| 156 | + $componentApi = new ComponentApi($mapiClient, "2222"); |
| 157 | + |
| 158 | + $componentData = new Component("test-component"); |
| 159 | + |
| 160 | + $this->expectException(InvalidArgumentException::class); |
| 161 | + $this->expectExceptionMessage("Component ID cannot be empty"); |
| 162 | + |
| 163 | + $componentApi->update("", $componentData); |
| 164 | + } |
| 165 | + |
| 166 | + public function testCreateComponentWithInvalidDataThrowsException(): void |
| 167 | + { |
| 168 | + $client = new MockHttpClient([]); |
| 169 | + $mapiClient = ManagementApiClient::initTest($client); |
| 170 | + $componentApi = new ComponentApi($mapiClient, "2222"); |
| 171 | + |
| 172 | + $componentData = new Component("valid-name"); |
| 173 | + // Remove the name to make it invalid by setting empty data |
| 174 | + $componentData->setData([]); |
| 175 | + |
| 176 | + $this->expectException(InvalidStoryDataException::class); |
| 177 | + $this->expectExceptionMessage("Invalid component data provided"); |
| 178 | + |
| 179 | + $componentApi->create($componentData); |
| 180 | + } |
82 | 181 | } |
0 commit comments