ZendTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\Zend;
  8. use Magento\Payment\Gateway\Http\ConverterInterface;
  9. use Magento\Framework\HTTP\ZendClientFactory;
  10. use Magento\Framework\HTTP\ZendClient;
  11. use Magento\Payment\Gateway\Http\TransferInterface;
  12. /**
  13. * Class ZendTest
  14. */
  15. class ZendTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /** @var Zend */
  18. protected $model;
  19. /**
  20. * @var ConverterInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $converterMock;
  23. /**
  24. * @var ZendClientFactory|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $zendClientFactoryMock;
  27. /**
  28. * @var ZendClient|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $clientMock;
  31. /**
  32. * @var TransferInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $transferObjectMock;
  35. /**
  36. * @var \Magento\Payment\Model\Method\Logger|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $loggerMock;
  39. protected function setUp()
  40. {
  41. $this->converterMock = $this->getMockBuilder(\Magento\Payment\Gateway\Http\ConverterInterface::class)
  42. ->getMockForAbstractClass();
  43. $this->zendClientFactoryMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClientFactory::class)
  44. ->setMethods(['create'])
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->clientMock = $this->getMockBuilder(\Magento\Framework\HTTP\ZendClient::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->loggerMock = $this->getMockBuilder(\Magento\Payment\Model\Method\Logger::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->transferObjectMock = $this->getMockBuilder(\Magento\Payment\Gateway\Http\TransferInterface::class)
  54. ->getMockForAbstractClass();
  55. $this->model = new Zend(
  56. $this->zendClientFactoryMock,
  57. $this->loggerMock,
  58. $this->converterMock
  59. );
  60. }
  61. public function testPlaceRequest()
  62. {
  63. $this->setClientTransferObjects();
  64. $responseBody = 'Response body content';
  65. $zendHttpResponseMock = $this->getMockBuilder(
  66. \Zend_Http_Response::class
  67. )->disableOriginalConstructor()->getMock();
  68. $zendHttpResponseMock->expects($this->once())->method('getBody')->willReturn($responseBody);
  69. $this->clientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
  70. $this->converterMock->expects($this->once())->method('convert')->with($responseBody);
  71. $this->zendClientFactoryMock->expects($this->once())
  72. ->method('create')
  73. ->willReturn($this->clientMock);
  74. $this->model->placeRequest($this->transferObjectMock);
  75. }
  76. /**
  77. * Tests failing client gateway request
  78. *
  79. * @expectedException \Magento\Payment\Gateway\Http\ClientException
  80. */
  81. public function testPlaceRequestClientFail()
  82. {
  83. $this->setClientTransferObjects();
  84. $this->clientMock->expects($this->once())
  85. ->method('request')
  86. ->willThrowException(new \Zend_Http_Client_Exception);
  87. $this->converterMock->expects($this->never())->method('convert');
  88. $this->zendClientFactoryMock->expects($this->once())
  89. ->method('create')
  90. ->willReturn($this->clientMock);
  91. $this->model->placeRequest($this->transferObjectMock);
  92. }
  93. /**
  94. * Tests failing response converting
  95. *
  96. * @expectedException \Magento\Payment\Gateway\Http\ConverterException
  97. */
  98. public function testPlaceRequestConvertResponseFail()
  99. {
  100. $this->setClientTransferObjects();
  101. $responseBody = 'Response body content';
  102. $zendHttpResponseMock = $this->getMockBuilder(
  103. \Zend_Http_Response::class
  104. )->disableOriginalConstructor()->getMock();
  105. $zendHttpResponseMock->expects($this->once())->method('getBody')->willReturn($responseBody);
  106. $this->clientMock->expects($this->once())->method('request')->willReturn($zendHttpResponseMock);
  107. $this->converterMock->expects($this->once())
  108. ->method('convert')
  109. ->with($responseBody)
  110. ->willThrowException(new \Magento\Payment\Gateway\Http\ConverterException(__()));
  111. $this->zendClientFactoryMock->expects($this->once())
  112. ->method('create')
  113. ->willReturn($this->clientMock);
  114. $this->model->placeRequest($this->transferObjectMock);
  115. }
  116. private function setClientTransferObjects()
  117. {
  118. $config = ['key1' => 'value1', 'key2' => 'value2'];
  119. $method = \Zend_Http_Client::POST;
  120. $headers = ['key1' => 'value1', 'key2' => 'value2'];
  121. $body = 'Body content';
  122. $uri = 'https://example.com/listener';
  123. $shouldEncode = true;
  124. $this->transferObjectMock->expects($this->once())->method('getClientConfig')->willReturn($config);
  125. $this->transferObjectMock->expects($this->atLeastOnce())->method('getMethod')->willReturn($method);
  126. $this->transferObjectMock->expects($this->once())->method('getHeaders')->willReturn($headers);
  127. $this->transferObjectMock->expects($this->atLeastOnce())->method('getBody')->willReturn($body);
  128. $this->transferObjectMock->expects($this->once())->method('shouldEncode')->willReturn($shouldEncode);
  129. $this->transferObjectMock->expects(static::atLeastOnce())->method('getUri')->willReturn($uri);
  130. $this->clientMock->expects($this->once())->method('setConfig')->with($config)->willReturnSelf();
  131. $this->clientMock->expects($this->once())->method('setMethod')->with($method)->willReturnSelf();
  132. $this->clientMock->expects($this->once())->method('setParameterPost')->with($body)->willReturnSelf();
  133. $this->clientMock->expects($this->once())->method('setHeaders')->with($headers)->willReturnSelf();
  134. $this->clientMock->expects($this->once())->method('setUrlEncodeBody')->with($shouldEncode)->willReturnSelf();
  135. $this->clientMock->expects($this->once())->method('setUri')->with($uri)->willReturnSelf();
  136. }
  137. }