ClassReflectorTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Config;
  7. /**
  8. * Test for class reflector.
  9. */
  10. class ClassReflectorTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /** @var \Magento\Framework\Reflection\TypeProcessor|\PHPUnit_Framework_MockObject_MockObject */
  13. protected $_typeProcessor;
  14. /** @var \Magento\Webapi\Model\Config\ClassReflector */
  15. protected $_classReflector;
  16. /**
  17. * Set up helper.
  18. */
  19. protected function setUp()
  20. {
  21. $this->_typeProcessor = $this->createPartialMock(
  22. \Magento\Framework\Reflection\TypeProcessor::class,
  23. ['process']
  24. );
  25. $this->_typeProcessor->expects(
  26. $this->any()
  27. )->method(
  28. 'process'
  29. )->will(
  30. $this->returnValueMap([['string', 'str'], ['int', 'int']])
  31. );
  32. $this->_classReflector = new \Magento\Webapi\Model\Config\ClassReflector($this->_typeProcessor);
  33. }
  34. public function testReflectClassMethods()
  35. {
  36. $data = $this->_classReflector->reflectClassMethods(
  37. \Magento\Webapi\Test\Unit\Model\Config\TestServiceForClassReflector::class,
  38. ['generateRandomString' => ['method' => 'generateRandomString']]
  39. );
  40. $this->assertEquals(['generateRandomString' => $this->_getSampleReflectionData()], $data);
  41. }
  42. public function testExtractMethodData()
  43. {
  44. $classReflection = new \Zend\Code\Reflection\ClassReflection(
  45. \Magento\Webapi\Test\Unit\Model\Config\TestServiceForClassReflector::class
  46. );
  47. /** @var $methodReflection \Zend\Code\Reflection\MethodReflection */
  48. $methodReflection = $classReflection->getMethods()[0];
  49. $methodData = $this->_classReflector->extractMethodData($methodReflection);
  50. $expectedResponse = $this->_getSampleReflectionData();
  51. $this->assertEquals($expectedResponse, $methodData);
  52. }
  53. /**
  54. * Expected reflection data for TestServiceForClassReflector generateRandomString method
  55. *
  56. * @return array
  57. */
  58. protected function _getSampleReflectionData()
  59. {
  60. return [
  61. 'documentation' => 'Basic random string generator. This line is short description ' .
  62. 'This line is long description. This is still the long description.',
  63. 'interface' => [
  64. 'in' => [
  65. 'parameters' => [
  66. 'length' => [
  67. 'type' => 'int',
  68. 'required' => true,
  69. 'documentation' => 'length of the random string',
  70. ],
  71. ],
  72. ],
  73. 'out' => [
  74. 'parameters' => [
  75. 'result' => ['type' => 'string', 'documentation' => 'random string', 'required' => true],
  76. ],
  77. ],
  78. ]
  79. ];
  80. }
  81. }