ExceptionTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Test Webapi module exception.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Webapi\Test\Unit;
  9. class ExceptionTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * Test Webapi exception construct.
  13. */
  14. public function testConstruct()
  15. {
  16. $code = 1111;
  17. $details = ['key1' => 'value1', 'key2' => 'value2'];
  18. $apiException = new \Magento\Framework\Webapi\Exception(
  19. __('Message'),
  20. $code,
  21. \Magento\Framework\Webapi\Exception::HTTP_UNAUTHORIZED,
  22. $details
  23. );
  24. $this->assertEquals(
  25. $apiException->getHttpCode(),
  26. \Magento\Framework\Webapi\Exception::HTTP_UNAUTHORIZED,
  27. 'Exception code is set incorrectly in construct.'
  28. );
  29. $this->assertEquals(
  30. $apiException->getMessage(),
  31. 'Message',
  32. 'Exception message is set incorrectly in construct.'
  33. );
  34. $this->assertEquals($apiException->getCode(), $code, 'Exception code is set incorrectly in construct.');
  35. $this->assertEquals($apiException->getDetails(), $details, 'Details are set incorrectly in construct.');
  36. }
  37. /**
  38. * Test Webapi exception construct with invalid data.
  39. *
  40. * @dataProvider providerForTestConstructInvalidHttpCode
  41. */
  42. public function testConstructInvalidHttpCode($httpCode)
  43. {
  44. $this->expectException('InvalidArgumentException');
  45. $this->expectExceptionMessage("The specified HTTP code \"{$httpCode}\" is invalid.");
  46. /** Create \Magento\Framework\Webapi\Exception object with invalid code. */
  47. /** Valid codes range is from 400 to 599. */
  48. new \Magento\Framework\Webapi\Exception(__('Message'), 0, $httpCode);
  49. }
  50. public function testGetOriginatorSender()
  51. {
  52. $apiException = new \Magento\Framework\Webapi\Exception(
  53. __('Message'),
  54. 0,
  55. \Magento\Framework\Webapi\Exception::HTTP_UNAUTHORIZED
  56. );
  57. /** Check that Webapi \Exception object with code 401 matches Sender originator.*/
  58. $this->assertEquals(
  59. \Magento\Webapi\Model\Soap\Fault::FAULT_CODE_SENDER,
  60. $apiException->getOriginator(),
  61. 'Wrong Sender originator detecting.'
  62. );
  63. }
  64. public function testGetOriginatorReceiver()
  65. {
  66. $apiException = new \Magento\Framework\Webapi\Exception(
  67. __('Message'),
  68. 0,
  69. \Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR
  70. );
  71. /** Check that Webapi \Exception object with code 500 matches Receiver originator.*/
  72. $this->assertEquals(
  73. \Magento\Webapi\Model\Soap\Fault::FAULT_CODE_RECEIVER,
  74. $apiException->getOriginator(),
  75. 'Wrong Receiver originator detecting.'
  76. );
  77. }
  78. /**
  79. * Data provider for testConstructInvalidCode.
  80. *
  81. * @return array
  82. */
  83. public function providerForTestConstructInvalidHttpCode()
  84. {
  85. //Each array contains invalid \Exception code.
  86. return [[300], [600]];
  87. }
  88. }