Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ServiceMetadataTest.php
Go to the documentation of this file.
1 <?php
7 
15 
16 class ServiceMetadataTest extends \PHPUnit\Framework\TestCase
17 {
21  private $serviceMetadata;
22 
26  private $cacheMock;
27 
31  private $configMock;
32 
36  private $classReflectorMock;
37 
41  private $typeProcessorMock;
42 
46  private $serializerMock;
47 
48  protected function setUp()
49  {
50  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
51 
52  $this->configMock = $this->createMock(Config::class);
53  $this->cacheMock = $this->createMock(Webapi::class);
54  $this->classReflectorMock = $this->createMock(ClassReflector::class);
55  $this->typeProcessorMock = $this->createMock(TypeProcessor::class);
56  $this->serializerMock = $this->createMock(SerializerInterface::class);
57 
58  $this->serviceMetadata = $objectManager->getObject(
59  ServiceMetadata::class,
60  [
61  'config' => $this->configMock,
62  'cache' => $this->cacheMock,
63  'classReflector' => $this->classReflectorMock,
64  'typeProcessor' => $this->typeProcessorMock,
65  'serializer' => $this->serializerMock
66  ]
67  );
68  }
69 
70  public function testGetServicesConfig()
71  {
72  $servicesConfig = ['foo' => 'bar'];
73  $typeData = ['bar' => 'foo'];
74  $serializedServicesConfig = 'serialized services config';
75  $serializedTypeData = 'serialized type data';
76  $this->cacheMock->expects($this->at(0))
77  ->method('load')
79  ->willReturn($serializedServicesConfig);
80  $this->cacheMock->expects($this->at(1))
81  ->method('load')
83  ->willReturn($serializedTypeData);
84  $this->serializerMock->expects($this->at(0))
85  ->method('unserialize')
86  ->with($serializedServicesConfig)
87  ->willReturn($servicesConfig);
88  $this->serializerMock->expects($this->at(1))
89  ->method('unserialize')
90  ->with($serializedTypeData)
91  ->willReturn($typeData);
92  $this->typeProcessorMock->expects($this->once())
93  ->method('setTypesData')
94  ->with($typeData);
95  $this->serviceMetadata->getServicesConfig();
96  $this->assertEquals($servicesConfig, $this->serviceMetadata->getServicesConfig());
97  }
98 
103  {
104  $servicesConfig = [
105  'services' => [
106  CustomerRepositoryInterface::class => [
107  'V1' => [
108  'methods' => [
109  'getById' => [
110  'resources' => [
111  [
112  'Magento_Customer::customer',
113  ]
114  ],
115  'secure' => false
116  ]
117  ]
118  ]
119  ]
120  ]
121  ];
122  $methodsReflectionData = [
123  'getById' => [
124  'documentation' => 'Get customer by customer ID.',
125  'interface' => [
126  'in' => [
127  'parameters' => [
128  'customerId' => [
129  'type' => 'int',
130  'required' => true,
131  'documentation' => null
132  ]
133  ]
134  ],
135  'out' => [
136  'parameters' => [
137  'result' => [
138  'type' => 'CustomerDataCustomerInterface',
139  'required' => true,
140  'documentation' => null
141  ]
142  ]
143  ]
144  ]
145  ]
146  ];
147  $servicesMetadata = [
148  'customerCustomerRepositoryV1' => [
149  'methods' => array_merge_recursive(
150  [
151  'getById' => [
152  'resources' => [
153  [
154  'Magento_Customer::customer',
155  ],
156  ],
157  'method' => 'getById',
158  'inputRequired' => false,
159  'isSecure' => false,
160  ]
161  ],
162  $methodsReflectionData
163  ),
164  'class' => CustomerRepositoryInterface::class,
165  'description' => 'Customer CRUD interface.'
166  ]
167  ];
168  $typeData = [
169  'CustomerDataCustomerInterface' => [
170  'documentation' => 'Customer interface.',
171  'parameters' => [
172  'id' => [
173  'type' => 'int',
174  'required' => false,
175  'documentation' => 'Customer id'
176  ]
177  ]
178  ]
179  ];
180  $serializedServicesConfig = 'serialized services config';
181  $serializedTypeData = 'serialized type data';
182  $this->cacheMock->expects($this->at(0))
183  ->method('load')
185  ->willReturn(false);
186  $this->cacheMock->expects($this->at(1))
187  ->method('load')
189  ->willReturn(false);
190  $this->serializerMock->expects($this->never())
191  ->method('unserialize');
192  $this->configMock->expects($this->once())
193  ->method('getServices')
194  ->willReturn($servicesConfig);
195  $this->classReflectorMock->expects($this->once())
196  ->method('reflectClassMethods')
197  ->willReturn($methodsReflectionData);
198  $this->classReflectorMock->expects($this->once())
199  ->method('extractClassDescription')
200  ->with(CustomerRepositoryInterface::class)
201  ->willReturn('Customer CRUD interface.');
202  $this->typeProcessorMock->expects($this->once())
203  ->method('getTypesData')
204  ->willReturn($typeData);
205  $this->serializerMock->expects($this->at(0))
206  ->method('serialize')
207  ->with($servicesMetadata)
208  ->willReturn($serializedServicesConfig);
209  $this->serializerMock->expects($this->at(1))
210  ->method('serialize')
211  ->with($typeData)
212  ->willReturn($serializedTypeData);
213  $this->cacheMock->expects($this->at(2))
214  ->method('save')
215  ->with(
216  $serializedServicesConfig,
218  );
219  $this->cacheMock->expects($this->at(3))
220  ->method('save')
221  ->with(
222  $serializedTypeData,
224  );
225  $this->serviceMetadata->getServicesConfig();
226  $this->assertEquals($servicesMetadata, $this->serviceMetadata->getServicesConfig());
227  }
228 
229  public function testGetRoutesConfig()
230  {
231  $routesConfig = ['foo' => 'bar'];
232  $typeData = ['bar' => 'foo'];
233  $serializedRoutesConfig = 'serialized routes config';
234  $serializedTypeData = 'serialized type data';
235  $this->cacheMock->expects($this->at(0))
236  ->method('load')
238  ->willReturn($serializedRoutesConfig);
239  $this->cacheMock->expects($this->at(1))
240  ->method('load')
242  ->willReturn($serializedTypeData);
243  $this->serializerMock->expects($this->at(0))
244  ->method('unserialize')
245  ->with($serializedRoutesConfig)
246  ->willReturn($routesConfig);
247  $this->serializerMock->expects($this->at(1))
248  ->method('unserialize')
249  ->with($serializedTypeData)
250  ->willReturn($typeData);
251  $this->typeProcessorMock->expects($this->once())
252  ->method('setTypesData')
253  ->with($typeData);
254  $this->serviceMetadata->getRoutesConfig();
255  $this->assertEquals($routesConfig, $this->serviceMetadata->getRoutesConfig());
256  }
257 
261  public function testGetRoutesConfigNoCache()
262  {
263  $servicesConfig = [
264  'services' => [
265  CustomerRepositoryInterface::class => [
266  'V1' => [
267  'methods' => [
268  'getById' => [
269  'resources' => [
270  [
271  'Magento_Customer::customer',
272  ]
273  ],
274  'secure' => false
275  ]
276  ]
277  ]
278  ]
279  ],
280  'routes' => [
281  '/V1/customers/:customerId' => [
282  'GET' => [
283  'secure' => false,
284  'service' => [
285  'class' => CustomerRepositoryInterface::class,
286  'method' => 'getById'
287  ],
288  'resources' => [
289  'Magento_Customer::customer' => true
290  ],
291  'parameters' => []
292  ]
293  ]
294  ],
295  'class' => CustomerRepositoryInterface::class,
296  'description' => 'Customer CRUD interface.',
297  ];
298  $methodsReflectionData = [
299  'getById' => [
300  'documentation' => 'Get customer by customer ID.',
301  'interface' => [
302  'in' => [
303  'parameters' => [
304  'customerId' => [
305  'type' => 'int',
306  'required' => true,
307  'documentation' => null
308  ]
309  ]
310  ],
311  'out' => [
312  'parameters' => [
313  'result' => [
314  'type' => 'CustomerDataCustomerInterface',
315  'required' => true,
316  'documentation' => null
317  ]
318  ]
319  ]
320  ]
321  ]
322  ];
323  $routesMetadata = [
324  'customerCustomerRepositoryV1' => [
325  'methods' => array_merge_recursive(
326  [
327  'getById' => [
328  'resources' => [
329  [
330  'Magento_Customer::customer',
331  ]
332  ],
333  'method' => 'getById',
334  'inputRequired' => false,
335  'isSecure' => false,
336  ]
337  ],
338  $methodsReflectionData
339  ),
340  'routes' => [
341  '/V1/customers/:customerId' => [
342  'GET' => [
343  'method' => 'getById',
344  'parameters' => []
345  ]
346  ]
347  ],
348  'class' => CustomerRepositoryInterface::class,
349  'description' => 'Customer CRUD interface.'
350  ]
351  ];
352  $typeData = [
353  'CustomerDataCustomerInterface' => [
354  'documentation' => 'Customer interface.',
355  'parameters' => [
356  'id' => [
357  'type' => 'int',
358  'required' => false,
359  'documentation' => 'Customer id'
360  ]
361  ]
362  ]
363  ];
364  $serializedRoutesConfig = 'serialized routes config';
365  $serializedTypeData = 'serialized type data';
366  $this->cacheMock->expects($this->at(0))
367  ->method('load')
369  ->willReturn(false);
370  $this->cacheMock->expects($this->at(1))
371  ->method('load')
373  ->willReturn(false);
374  $this->serializerMock->expects($this->never())
375  ->method('unserialize');
376  $this->configMock->expects($this->exactly(2))
377  ->method('getServices')
378  ->willReturn($servicesConfig);
379  $this->classReflectorMock->expects($this->once())
380  ->method('reflectClassMethods')
381  ->willReturn($methodsReflectionData);
382  $this->classReflectorMock->expects($this->once())
383  ->method('extractClassDescription')
384  ->with(CustomerRepositoryInterface::class)
385  ->willReturn('Customer CRUD interface.');
386  $this->typeProcessorMock->expects($this->exactly(2))
387  ->method('getTypesData')
388  ->willReturn($typeData);
389  $this->serializerMock->expects($this->at(2))
390  ->method('serialize')
391  ->with($routesMetadata)
392  ->willReturn($serializedRoutesConfig);
393  $this->serializerMock->expects($this->at(3))
394  ->method('serialize')
395  ->with($typeData)
396  ->willReturn($serializedTypeData);
397  $this->cacheMock->expects($this->at(6))
398  ->method('save')
399  ->with(
400  $serializedRoutesConfig,
402  );
403  $this->cacheMock->expects($this->at(7))
404  ->method('save')
405  ->with(
406  $serializedTypeData,
408  );
409  $this->serviceMetadata->getRoutesConfig();
410  $this->assertEquals($routesMetadata, $this->serviceMetadata->getRoutesConfig());
411  }
412 
416  public function testGetServiceName($className, $version, $preserveVersion, $expected)
417  {
418  $this->assertEquals(
419  $expected,
420  $this->serviceMetadata->getServiceName($className, $version, $preserveVersion)
421  );
422  }
423 
427  public function getServiceNameDataProvider()
428  {
429  return [
430  [
431  \Magento\Customer\Api\AccountManagementInterface::class,
432  'V1',
433  false,
434  'customerAccountManagement'
435  ],
436  [
437  \Magento\Customer\Api\AddressRepositoryInterface::class,
438  'V1',
439  true,
440  'customerAddressRepositoryV1'
441  ],
442  ];
443  }
444 
449  public function testGetServiceNameInvalidName($interfaceClassName, $version)
450  {
451  $this->serviceMetadata->getServiceName($interfaceClassName, $version);
452  }
453 
458  {
459  return [
460  ['BarV1Interface', 'V1'], // Missed vendor, module and Service
461  ['Service\\V1Interface', 'V1'], // Missed vendor and module
462  ['Magento\\Foo\\Service\\BarVxInterface', 'V1'], // Version number should be a number
463  ['Magento\\Foo\\Service\\BarInterface', 'V1'], // Missed version
464  ['Magento\\Foo\\Service\\BarV1', 'V1'], // Missed Interface
465  ['Foo\\Service\\BarV1Interface', 'V1'], // Missed module
466  ['Foo\\BarV1Interface', 'V1'] // Missed module and Service
467  ];
468  }
469 }
$objectManager
Definition: bootstrap.php:17
return false
Definition: gallery.phtml:36
testGetServiceName($className, $version, $preserveVersion, $expected)
testGetServiceNameInvalidName($interfaceClassName, $version)
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31