SoapTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Test\Unit\Gateway\Http\Client;
  7. use Magento\Payment\Gateway\Http\Client\Soap;
  8. class SoapTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \PHPUnit_Framework_MockObject_MockObject
  12. */
  13. private $logger;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $clientFactory;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $converter;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $client;
  26. /**
  27. * @var Soap
  28. */
  29. private $gatewayClient;
  30. protected function setUp()
  31. {
  32. $this->logger = $this->getMockBuilder(
  33. \Magento\Payment\Model\Method\Logger::class
  34. )
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->clientFactory = $this->getMockBuilder(
  38. \Magento\Framework\Webapi\Soap\ClientFactory::class
  39. )->getMock();
  40. $this->converter = $this->getMockBuilder(
  41. \Magento\Payment\Gateway\Http\ConverterInterface::class
  42. )->getMockForAbstractClass();
  43. $this->client = $this->getMockBuilder(\SoapClient::class)
  44. ->setMethods(['__setSoapHeaders', '__soapCall', '__getLastRequest'])
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->gatewayClient = new Soap(
  48. $this->logger,
  49. $this->clientFactory,
  50. $this->converter
  51. );
  52. }
  53. public function testPlaceRequest()
  54. {
  55. $expectedResult = [
  56. 'result' => []
  57. ];
  58. $soapResult = new \StdClass();
  59. $this->logger->expects(static::at(0))
  60. ->method('debug')
  61. ->with(
  62. ['request' => ['body']]
  63. );
  64. $this->clientFactory->expects(static::once())
  65. ->method('create')
  66. ->with('path_to_wsdl', ['trace' => true])
  67. ->willReturn($this->client);
  68. $transferObject = $this->getTransferObject();
  69. $transferObject->expects(static::any())
  70. ->method('__setSoapHeaders')
  71. ->with(['headers']);
  72. $this->client->expects(static::once())
  73. ->method('__soapCall')
  74. ->with('soapMethod', [['body']])
  75. ->willReturn($soapResult);
  76. $this->converter->expects(static::once())
  77. ->method('convert')
  78. ->with($soapResult)
  79. ->willReturn($expectedResult);
  80. $this->logger->expects(static::at(1))
  81. ->method('debug')
  82. ->with(['response' => $expectedResult]);
  83. static::assertEquals(
  84. $expectedResult,
  85. $this->gatewayClient->placeRequest($transferObject)
  86. );
  87. }
  88. public function testPlaceRequestSoapException()
  89. {
  90. $this->expectException('Exception');
  91. $this->logger->expects(static::at(0))
  92. ->method('debug')
  93. ->with(
  94. ['request' => ['body']]
  95. );
  96. $this->clientFactory->expects(static::once())
  97. ->method('create')
  98. ->with('path_to_wsdl', ['trace' => true])
  99. ->willReturn($this->client);
  100. $transferObject = $this->getTransferObject();
  101. $transferObject->expects(static::any())
  102. ->method('__setSoapHeaders')
  103. ->with(['headers']);
  104. $this->client->expects(static::once())
  105. ->method('__soapCall')
  106. ->with('soapMethod', [['body']])
  107. ->willThrowException(new \Exception());
  108. $this->client->expects(static::once())
  109. ->method('__getLastRequest')
  110. ->willReturn('RequestTrace');
  111. $this->logger->expects(static::at(1))
  112. ->method('debug')
  113. ->with(
  114. ['trace' => 'RequestTrace']
  115. );
  116. $this->gatewayClient->placeRequest($transferObject);
  117. }
  118. /**
  119. * Returns prepared transfer object
  120. *
  121. * @return \PHPUnit_Framework_MockObject_MockObject
  122. */
  123. private function getTransferObject()
  124. {
  125. $transferObject = $this->getMockBuilder(
  126. \Magento\Payment\Gateway\Http\TransferInterface::class
  127. )->setMethods(['__setSoapHeaders', 'getBody', 'getClientConfig', 'getMethod'])->getMockForAbstractClass();
  128. $transferObject->expects(static::any())
  129. ->method('getBody')
  130. ->willReturn(['body']);
  131. $transferObject->expects(static::any())
  132. ->method('getClientConfig')
  133. ->willReturn(['wsdl' => 'path_to_wsdl']);
  134. $transferObject->expects(static::any())
  135. ->method('getMethod')
  136. ->willReturn('soapMethod');
  137. return $transferObject;
  138. }
  139. }