BaseService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Routing;
  7. use Magento\Framework\Exception\AuthorizationException;
  8. use Magento\Framework\Webapi\Exception as WebapiException;
  9. /**
  10. * Base class for all Service based routing tests
  11. */
  12. abstract class BaseService extends \Magento\TestFramework\TestCase\WebapiAbstract
  13. {
  14. /**
  15. * Check a particular adapter and assert unauthorized access
  16. *
  17. * @param array $serviceInfo
  18. * @param array|null $requestData
  19. */
  20. protected function assertUnauthorizedException($serviceInfo, $requestData = null)
  21. {
  22. if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
  23. $this->_assertSoapException(
  24. $serviceInfo,
  25. $requestData,
  26. "The consumer isn't authorized to access %resources."
  27. );
  28. } elseif (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) {
  29. $this->_assertRestUnauthorizedException($serviceInfo, $requestData);
  30. }
  31. }
  32. /**
  33. * Invoke the REST api and assert access is unauthorized
  34. *
  35. * @param array $serviceInfo
  36. * @param array|null $requestData
  37. */
  38. protected function _assertRestUnauthorizedException($serviceInfo, $requestData = null)
  39. {
  40. try {
  41. $this->_webApiCall($serviceInfo, $requestData);
  42. } catch (\Exception $e) {
  43. $this->assertContains(
  44. '{"message":"The consumer isn\'t authorized to access %resources.',
  45. $e->getMessage(),
  46. sprintf(
  47. 'REST routing did not fail as expected for the method "%s" of service "%s"',
  48. $serviceInfo['rest']['httpMethod'],
  49. $serviceInfo['rest']['resourcePath']
  50. )
  51. );
  52. $this->assertEquals(WebapiException::HTTP_UNAUTHORIZED, $e->getCode());
  53. }
  54. }
  55. /**
  56. * Check a particular adapter and assert the exception
  57. *
  58. * @param array $serviceInfo
  59. * @param array|null $requestData
  60. */
  61. protected function _assertNoRouteOrOperationException($serviceInfo, $requestData = null)
  62. {
  63. if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
  64. $this->_assertSoapException($serviceInfo, $requestData);
  65. } elseif (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) {
  66. $this->_assertNoRestRouteException($serviceInfo, $requestData);
  67. }
  68. }
  69. /**
  70. * Invoke the REST api and assert for test cases that no such REST route exist
  71. *
  72. * @param array $serviceInfo
  73. * @param array|null $requestData
  74. */
  75. protected function _assertNoRestRouteException($serviceInfo, $requestData = null)
  76. {
  77. try {
  78. $this->_webApiCall($serviceInfo, $requestData);
  79. } catch (\Exception $e) {
  80. $error = json_decode($e->getMessage(), true);
  81. $this->assertEquals('Request does not match any route.', $error['message']);
  82. $this->assertEquals(WebapiException::HTTP_NOT_FOUND, $e->getCode());
  83. }
  84. }
  85. /**
  86. * Invoke the SOAP api and assert for the NoWebApiXmlTestTest test cases that no such SOAP route exists
  87. *
  88. * @param array $serviceInfo
  89. * @param array|null $requestData
  90. * @param string $expectedMessage
  91. */
  92. protected function _assertSoapException($serviceInfo, $requestData = null, $expectedMessage = '')
  93. {
  94. try {
  95. $this->_webApiCall($serviceInfo, $requestData);
  96. } catch (\Exception $e) {
  97. if (get_class($e) !== 'SoapFault') {
  98. $this->fail(
  99. sprintf(
  100. 'Expected SoapFault exception not generated for Service - "%s" and Operation - "%s"',
  101. $serviceInfo['soap']['service'],
  102. $serviceInfo['soap']['operation']
  103. )
  104. );
  105. }
  106. if ($expectedMessage) {
  107. $this->assertContains($expectedMessage, $e->getMessage());
  108. }
  109. }
  110. }
  111. }