ServiceMetadataTest.php 17 KB

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