Skip to content

Commit 8fe2e68

Browse files
committed
ComponentApi full coverage tests
1 parent 1640bd7 commit 8fe2e68

File tree

1 file changed

+100
-1
lines changed

1 file changed

+100
-1
lines changed

tests/Feature/ComponentApiTest.php

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace Tests\Feature;
66

7-
use Tests\TestCase;
7+
use InvalidArgumentException;
88
use Storyblok\ManagementApi\Data\Component;
99
use Storyblok\ManagementApi\Endpoints\ComponentApi;
10+
use Storyblok\ManagementApi\Exceptions\InvalidStoryDataException;
1011
use Storyblok\ManagementApi\ManagementApiClient;
1112
use Storyblok\ManagementApi\QueryParameters\ComponentsParams;
1213
use Symfony\Component\HttpClient\MockHttpClient;
14+
use Tests\TestCase;
1315

1416
final class ComponentApiTest extends TestCase
1517
{
@@ -79,4 +81,101 @@ public function testGettingOneComponent(): void
7981
$this->assertSame("2025-04-15T21:32:55.495Z", $component->updatedAt());
8082
$this->assertFalse($component->isRoot());
8183
}
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+
}
82181
}

0 commit comments

Comments
 (0)