Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
JsonGenerationFromDataObjectTest.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Webapi;
8 
12 
17 {
19  protected $baseUrl = TESTS_BASE_URL;
20 
22  protected $storeCode;
23 
25  protected $isSingleService;
26 
30  protected $productMetadata;
31 
32  protected function setUp()
33  {
34  $this->_markTestAsRestOnly("JSON generation tests are intended to be executed for REST adapter only.");
35 
36  $this->storeCode = Bootstrap::getObjectManager()->get(StoreManagerInterface::class)
37  ->getStore()->getCode();
38 
39  $this->productMetadata = Bootstrap::getObjectManager()->get(ProductMetadataInterface::class);
40 
41  parent::setUp();
42  }
43 
44  public function testMultiServiceRetrieval()
45  {
46  $this->isSingleService = false;
47 
48  $resourcePath = '/schema?services=testModule5AllSoapAndRestV1,testModule5AllSoapAndRestV2';
49 
50  $serviceInfo = [
51  'rest' => [
52  'resourcePath' => $resourcePath,
54  ],
55  ];
56 
57  $schemaContent = $this->_webApiCall($serviceInfo);
58  $this->checkActualData($this->getExpectedMultiServiceData(), $schemaContent);
59  }
60 
61  public function testSingleServiceRetrieval()
62  {
63  $this->isSingleService = false;
64 
65  $resourcePath = '/schema?services=testModule5AllSoapAndRestV2';
66 
67  $serviceInfo = [
68  'rest' => [
69  'resourcePath' => $resourcePath,
71  ],
72  ];
73 
74  $schemaContent = $this->_webApiCall($serviceInfo);
75 
76  $this->checkActualData($this->getExpectedSingleServiceData(), $schemaContent);
77  }
78 
83  public function testInvalidRestUrlNoServices()
84  {
85  $resourcePath = '';
86 
87  $serviceInfo = [
88  'rest' => [
89  'resourcePath' => $resourcePath,
91  ],
92  ];
93 
94  $this->_webApiCall($serviceInfo);
95  }
96 
102  {
103  $this->isSingleService = false;
104 
105  $resourcePath = '/schema?services=invalidServiceName';
106 
107  $serviceInfo = [
108  'rest' => [
109  'resourcePath' => $resourcePath,
111  ],
112  ];
113 
114  $this->_webApiCall($serviceInfo);
115  }
116 
117  private function assertRecursiveArray($expected, $actual, $checkVal)
118  {
119  ksort($expected);
120  ksort($actual);
121  foreach ($expected as $expKey => $expVal) {
122  $this->assertArrayHasKey($expKey, $actual, 'Schema does not contain \'' . $expKey . '\' section.');
123  if (is_array($expVal)) {
124  $this->assertTrue(is_array($actual[$expKey]));
125  $this->assertRecursiveArray($expVal, $actual[$expKey], $checkVal);
126  } elseif ($checkVal) {
127  $this->assertEquals($expVal, $actual[$expKey], '\'' . $expKey . '\' section content is invalid.');
128  }
129  }
130  }
131 
132  public function checkActualData($expected, $actual)
133  {
134  $this->assertRecursiveArray($expected, $actual, true);
135  }
136 
137  public function getExpectedCommonData()
138  {
139  $versionParts = explode('.', $this->productMetadata->getVersion());
140  if (!isset($versionParts[0]) || !isset($versionParts[1])) {
141  return []; // Major and minor version are not set - return empty response
142  }
143  $majorMinorVersion = $versionParts[0] . '.' . $versionParts[1];
144  $url = str_replace('://', '', strstr($this->baseUrl, '://'));
145  $host = strpos($url, '/') ? strstr($url, '/', true) : $url;
146  $basePath = strstr(rtrim($url, '/'), '/');
147  $basePath = $basePath ? trim($basePath, '/') . '/' : '';
148  $basePath = '/' . $basePath . 'rest/' . $this->storeCode;
149  return [
150  'swagger' => '2.0',
151  'info' => [
152  'version' => $majorMinorVersion,
153  'title' => $this->productMetadata->getName() . ' ' .$this->productMetadata->getEdition(),
154  ],
155  'host' => $host,
156  'basePath' => $basePath,
157  ];
158  }
159 
164  public function getExpectedMultiServiceData()
165  {
166  $expected = [
167  'tags' => [
168  [
169  'name' => 'testModule5AllSoapAndRestV1',
170  'description' => 'Both SOAP and REST Version ONE',
171  ],
172  [
173  'name' => 'testModule5AllSoapAndRestV2',
174  'description' => 'Both SOAP and REST Version TWO',
175  ],
176  ],
177  'paths' => [
178  '/V1/TestModule5/{parentId}/nestedResource/{entityId}' => [
179  'put' => [
180  'tags' => [
181  'testModule5AllSoapAndRestV1',
182  ],
183  'description' => 'Update existing item.',
184  'operationId' => 'testModule5AllSoapAndRestV1NestedUpdatePut',
185  'parameters' => [
186  [
187  'name' => 'parentId',
188  'in' => 'path',
189  'type' => 'string',
190  'required' => true
191  ],
192  [
193  'name' => 'entityId',
194  'in' => 'path',
195  'type' => 'string',
196  'required' => true
197  ],
198  [
199  'name' => 'testModule5AllSoapAndRestV1NestedUpdatePutBody',
200  'in' => 'body',
201  'schema' => [
202  'required' => [
203  'entityItem',
204  ],
205  'properties' => [
206  'entityItem' => [
207  '$ref' => '#/definitions/test-module5-v1-entity-all-soap-and-rest',
208  ],
209  ],
210  'type' => 'object'
211  ],
212  ]
213  ],
214  'responses' => [
215  200 => [
216  'description' => '200 Success.',
217  'schema' => [
218  '$ref' => '#/definitions/test-module5-v1-entity-all-soap-and-rest',
219  ],
220  ],
221  401 => [
222  'description' => '401 Unauthorized',
223  'schema' => [
224  '$ref' => '#/definitions/error-response',
225  ],
226  ],
227  'default' => [
228  'description' => 'Unexpected error',
229  'schema' => [
230  '$ref' => '#/definitions/error-response',
231  ],
232  ],
233  ],
234  ],
235  ],
236  ],
237  'definitions' => [
238  'framework-attribute-interface' => [
239  'type' => 'object',
240  'description' => 'Interface for custom attribute value.',
241  'properties' => [
242  'attribute_code' => [
243  'type' => 'string',
244  'description' => 'Attribute code',
245  ],
246  'value' => [
247  'type' => 'string',
248  'description' => 'Attribute value',
249  ],
250  ],
251  'required' => [
252  'attribute_code',
253  'value',
254  ],
255  ],
256  'test-module5-v1-entity-all-soap-and-rest' => [
257  'type' => 'object',
258  'description' => 'Some Data Object short description. Data Object long multi line description.',
259  'properties' => [
260  'entity_id' => [
261  'type' => 'integer',
262  'description' => 'Item ID',
263  ],
264  'name' => [
265  'type' => 'string',
266  'description' => 'Item name',
267  ],
268  'enabled' => [
269  'type' => 'boolean',
270  'description' => 'If entity is enabled',
271  ],
272  'orders' => [
273  'type' => 'boolean',
274  'description' => 'If current entity has a property defined',
275  ],
276  'custom_attributes' => [
277  'type' => 'array',
278  'description' => 'Custom attributes values.',
279  'items' => [
280  '$ref' => '#/definitions/framework-attribute-interface',
281  ],
282  ],
283  ],
284  'required' => [
285  'entity_id',
286  'enabled',
287  'orders',
288  ],
289  ],
290  ],
291  ];
292  return array_merge_recursive($expected, $this->getExpectedCommonData());
293  }
294 
300  {
301  $expected = [
302  'tags' => [
303  [
304  'name' => 'testModule5AllSoapAndRestV2',
305  'description' => 'Both SOAP and REST Version TWO',
306  ],
307  ],
308  'paths' => [
309  '/V2/TestModule5/{id}' => [
310  'delete' => [
311  'tags' => [
312  'testModule5AllSoapAndRestV2',
313  ],
314  'description' => 'Delete existing item.',
315  'operationId' => 'testModule5AllSoapAndRestV2DeleteDelete',
316  'parameters' => [
317  [
318  'name' => 'id',
319  'in' => 'path',
320  'type' => 'string',
321  'required' => true
322  ],
323  ],
324  'responses' => [
325  200 => [
326  'description' => '200 Success.',
327  'schema' => [
328  '$ref' => '#/definitions/test-module5-v2-entity-all-soap-and-rest',
329  ],
330  ],
331  401 => [
332  'description' => '401 Unauthorized',
333  'schema' => [
334  '$ref' => '#/definitions/error-response',
335  ],
336  ],
337  'default' => [
338  'description' => 'Unexpected error',
339  'schema' => [
340  '$ref' => '#/definitions/error-response',
341  ],
342  ],
343  ],
344  ],
345  ],
346  ],
347  'definitions' => [
348  'test-module5-v2-entity-all-soap-and-rest' => [
349  'type' => 'object',
350  'description' => 'Some Data Object short description. Data Object long multi line description.',
351  'properties' => [
352  'price' => [
353  'type' => 'integer',
354  ],
355  ],
356  'required' => [
357  'price',
358  ],
359  ],
360  ],
361  ];
362  return array_merge($expected, $this->getExpectedCommonData());
363  }
364 }
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)