|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file has been auto generated |
| 6 | + * Do not change it. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Commercetools\Api\Test\Client\Resource; |
| 10 | + |
| 11 | +use Commercetools\Api\Client\ApiRequestBuilder; |
| 12 | +use Commercetools\Base\JsonObject; |
| 13 | +use Commercetools\Client\ApiRequest; |
| 14 | +use Commercetools\Exception\ApiClientException; |
| 15 | +use Commercetools\Exception\ApiServerException; |
| 16 | +use GuzzleHttp\ClientInterface; |
| 17 | +use GuzzleHttp\Exception\ClientException; |
| 18 | +use GuzzleHttp\Exception\ServerException; |
| 19 | +use GuzzleHttp\Psr7\Response; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Psr\Http\Message\RequestInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * @covers \Commercetools\Api\Client\Resource\ByProjectKeyCustomObjectsByContainerGet |
| 25 | + * @covers \Commercetools\Api\Client\Resource\ResourceByProjectKeyCustomObjectsByContainer |
| 26 | + */ |
| 27 | +class ResourceByProjectKeyCustomObjectsByContainerTest extends TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @dataProvider getRequests() |
| 31 | + */ |
| 32 | + public function testBuilder(callable $builderFunction, string $method, string $relativeUri, string $body = null) |
| 33 | + { |
| 34 | + $builder = new ApiRequestBuilder(); |
| 35 | + $request = $builderFunction($builder); |
| 36 | + $this->assertSame(strtolower($method), strtolower($request->getMethod())); |
| 37 | + $this->assertSame($relativeUri, (string) $request->getUri()); |
| 38 | + if (!is_null($body)) { |
| 39 | + $this->assertJsonStringEqualsJsonString($body, (string) $request->getBody()); |
| 40 | + } else { |
| 41 | + $this->assertSame("", (string) $request->getBody()); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + /** |
| 48 | + * @dataProvider getRequestBuilderResponses() |
| 49 | + */ |
| 50 | + public function testMapFromResponse(callable $builderFunction, $statusCode) |
| 51 | + { |
| 52 | + $builder = new ApiRequestBuilder(); |
| 53 | + $request = $builderFunction($builder); |
| 54 | + $this->assertInstanceOf(ApiRequest::class, $request); |
| 55 | + |
| 56 | + $response = new Response($statusCode, [], "{}"); |
| 57 | + $this->assertInstanceOf(JsonObject::class, $request->mapFromResponse($response)); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dataProvider getRequestBuilders() |
| 62 | + */ |
| 63 | + public function testExecuteClientException(callable $builderFunction) |
| 64 | + { |
| 65 | + $client = $this->createMock(ClientInterface::class); |
| 66 | + |
| 67 | + $builder = new ApiRequestBuilder($client); |
| 68 | + $request = $builderFunction($builder); |
| 69 | + $client->method("send")->willThrowException(new ClientException("Oops!", $request, new Response(400))); |
| 70 | + |
| 71 | + $this->expectException(ApiClientException::class); |
| 72 | + $request->execute(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @dataProvider getRequestBuilders() |
| 77 | + */ |
| 78 | + public function testExecuteServerException(callable $builderFunction) |
| 79 | + { |
| 80 | + $client = $this->createMock(ClientInterface::class); |
| 81 | + |
| 82 | + $builder = new ApiRequestBuilder($client); |
| 83 | + $request = $builderFunction($builder); |
| 84 | + $client->method("send")->willThrowException(new ServerException("Oops!", $request, new Response(500))); |
| 85 | + |
| 86 | + $this->expectException(ApiServerException::class); |
| 87 | + $request->execute(); |
| 88 | + } |
| 89 | + |
| 90 | + public function getRequests() |
| 91 | + { |
| 92 | + return [ |
| 93 | + 'ByProjectKeyCustomObjectsByContainerGet_withExpand' => [ |
| 94 | + function (ApiRequestBuilder $builder): RequestInterface { |
| 95 | + return $builder |
| 96 | + ->withProjectKey('test_projectKey') |
| 97 | + ->customObjects() |
| 98 | + ->withContainer('test_container') |
| 99 | + ->get() |
| 100 | + ->withExpand('expand'); |
| 101 | + }, |
| 102 | + 'get', |
| 103 | + 'test_projectKey/custom-objects/test_container?expand=expand', |
| 104 | + ], |
| 105 | + 'ByProjectKeyCustomObjectsByContainerGet' => [ |
| 106 | + function (ApiRequestBuilder $builder): RequestInterface { |
| 107 | + return $builder |
| 108 | + ->withProjectKey("test_projectKey") |
| 109 | + ->customObjects() |
| 110 | + ->withContainer("test_container") |
| 111 | + ->get(); |
| 112 | + }, |
| 113 | + 'get', |
| 114 | + 'test_projectKey/custom-objects/test_container', |
| 115 | + ] |
| 116 | + ]; |
| 117 | + } |
| 118 | + |
| 119 | + public function getResources() |
| 120 | + { |
| 121 | + return [ |
| 122 | + ]; |
| 123 | + } |
| 124 | + |
| 125 | + public function getRequestBuilders() |
| 126 | + { |
| 127 | + return [ |
| 128 | + 'ByProjectKeyCustomObjectsByContainerGet' => [ |
| 129 | + function (ApiRequestBuilder $builder): RequestInterface { |
| 130 | + return $builder |
| 131 | + ->withProjectKey("projectKey") |
| 132 | + ->customObjects() |
| 133 | + ->withContainer("container") |
| 134 | + ->get(); |
| 135 | + } |
| 136 | + ] |
| 137 | + ]; |
| 138 | + } |
| 139 | + |
| 140 | + public function getRequestBuilderResponses() |
| 141 | + { |
| 142 | + return [ |
| 143 | + 'ByProjectKeyCustomObjectsByContainerGet_200' => [ |
| 144 | + function (ApiRequestBuilder $builder): RequestInterface { |
| 145 | + return $builder |
| 146 | + ->withProjectKey("projectKey") |
| 147 | + ->customObjects() |
| 148 | + ->withContainer("container") |
| 149 | + ->get(); |
| 150 | + }, |
| 151 | + 200 |
| 152 | + ], |
| 153 | + 'ByProjectKeyCustomObjectsByContainerGet_400' => [ |
| 154 | + function (ApiRequestBuilder $builder): RequestInterface { |
| 155 | + return $builder |
| 156 | + ->withProjectKey("projectKey") |
| 157 | + ->customObjects() |
| 158 | + ->withContainer("container") |
| 159 | + ->get(); |
| 160 | + }, |
| 161 | + 400 |
| 162 | + ], |
| 163 | + 'ByProjectKeyCustomObjectsByContainerGet_401' => [ |
| 164 | + function (ApiRequestBuilder $builder): RequestInterface { |
| 165 | + return $builder |
| 166 | + ->withProjectKey("projectKey") |
| 167 | + ->customObjects() |
| 168 | + ->withContainer("container") |
| 169 | + ->get(); |
| 170 | + }, |
| 171 | + 401 |
| 172 | + ], |
| 173 | + 'ByProjectKeyCustomObjectsByContainerGet_403' => [ |
| 174 | + function (ApiRequestBuilder $builder): RequestInterface { |
| 175 | + return $builder |
| 176 | + ->withProjectKey("projectKey") |
| 177 | + ->customObjects() |
| 178 | + ->withContainer("container") |
| 179 | + ->get(); |
| 180 | + }, |
| 181 | + 403 |
| 182 | + ], |
| 183 | + 'ByProjectKeyCustomObjectsByContainerGet_404' => [ |
| 184 | + function (ApiRequestBuilder $builder): RequestInterface { |
| 185 | + return $builder |
| 186 | + ->withProjectKey("projectKey") |
| 187 | + ->customObjects() |
| 188 | + ->withContainer("container") |
| 189 | + ->get(); |
| 190 | + }, |
| 191 | + 404 |
| 192 | + ], |
| 193 | + 'ByProjectKeyCustomObjectsByContainerGet_500' => [ |
| 194 | + function (ApiRequestBuilder $builder): RequestInterface { |
| 195 | + return $builder |
| 196 | + ->withProjectKey("projectKey") |
| 197 | + ->customObjects() |
| 198 | + ->withContainer("container") |
| 199 | + ->get(); |
| 200 | + }, |
| 201 | + 500 |
| 202 | + ], |
| 203 | + 'ByProjectKeyCustomObjectsByContainerGet_503' => [ |
| 204 | + function (ApiRequestBuilder $builder): RequestInterface { |
| 205 | + return $builder |
| 206 | + ->withProjectKey("projectKey") |
| 207 | + ->customObjects() |
| 208 | + ->withContainer("container") |
| 209 | + ->get(); |
| 210 | + }, |
| 211 | + 503 |
| 212 | + ], |
| 213 | + 'ByProjectKeyCustomObjectsByContainerGet_599' => [ |
| 214 | + function (ApiRequestBuilder $builder): RequestInterface { |
| 215 | + return $builder |
| 216 | + ->withProjectKey("projectKey") |
| 217 | + ->customObjects() |
| 218 | + ->withContainer("container") |
| 219 | + ->get(); |
| 220 | + }, |
| 221 | + 599 |
| 222 | + ] |
| 223 | + ]; |
| 224 | + } |
| 225 | +} |
0 commit comments