SampleTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Test\Unit\Controller\Download;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class SampleTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \Magento\Downloadable\Controller\Download\Sample */
  14. protected $sample;
  15. /** @var ObjectManagerHelper */
  16. protected $objectManagerHelper;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Request\Http
  19. */
  20. protected $request;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ResponseInterface
  23. */
  24. protected $response;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\ObjectManager\ObjectManager
  27. */
  28. protected $objectManager;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Message\ManagerInterface
  31. */
  32. protected $messageManager;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Response\RedirectInterface
  35. */
  36. protected $redirect;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Downloadable\Helper\Data
  39. */
  40. protected $helperData;
  41. /**
  42. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Downloadable\Helper\Download
  43. */
  44. protected $downloadHelper;
  45. /**
  46. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product
  47. */
  48. protected $product;
  49. /**
  50. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\UrlInterface
  51. */
  52. protected $urlInterface;
  53. /**
  54. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  55. */
  56. protected function setUp()
  57. {
  58. $this->objectManagerHelper = new ObjectManagerHelper($this);
  59. $this->request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  60. $this->response = $this->createPartialMock(
  61. \Magento\Framework\App\ResponseInterface::class,
  62. [
  63. 'setHttpResponseCode',
  64. 'clearBody',
  65. 'sendHeaders',
  66. 'sendResponse',
  67. 'setHeader',
  68. 'setRedirect'
  69. ]
  70. );
  71. $this->helperData = $this->createPartialMock(\Magento\Downloadable\Helper\Data::class, [
  72. 'getIsShareable'
  73. ]);
  74. $this->downloadHelper = $this->createPartialMock(\Magento\Downloadable\Helper\Download::class, [
  75. 'setResource',
  76. 'getFilename',
  77. 'getContentType',
  78. 'getFileSize',
  79. 'getContentDisposition',
  80. 'output'
  81. ]);
  82. $this->product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
  83. '_wakeup',
  84. 'load',
  85. 'getId',
  86. 'getProductUrl',
  87. 'getName'
  88. ]);
  89. $this->messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  90. $this->redirect = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
  91. $this->urlInterface = $this->createMock(\Magento\Framework\UrlInterface::class);
  92. $this->objectManager = $this->createPartialMock(\Magento\Framework\ObjectManager\ObjectManager::class, [
  93. 'create',
  94. 'get'
  95. ]);
  96. $this->sample = $this->objectManagerHelper->getObject(
  97. \Magento\Downloadable\Controller\Download\Sample::class,
  98. [
  99. 'objectManager' => $this->objectManager,
  100. 'request' => $this->request,
  101. 'response' => $this->response,
  102. 'messageManager' => $this->messageManager,
  103. 'redirect' => $this->redirect
  104. ]
  105. );
  106. }
  107. public function testExecuteLinkTypeUrl()
  108. {
  109. $sampleMock = $this->getMockBuilder(\Magento\Downloadable\Model\Sample::class)
  110. ->disableOriginalConstructor()
  111. ->setMethods(['getId', 'load', 'getSampleType', 'getSampleUrl'])
  112. ->getMock();
  113. $this->request->expects($this->once())->method('getParam')->with('sample_id', 0)->willReturn('some_sample_id');
  114. $this->objectManager->expects($this->once())
  115. ->method('create')
  116. ->with(\Magento\Downloadable\Model\Sample::class)
  117. ->willReturn($sampleMock);
  118. $sampleMock->expects($this->once())->method('load')->with('some_sample_id')->willReturnSelf();
  119. $sampleMock->expects($this->once())->method('getId')->willReturn('some_link_id');
  120. $sampleMock->expects($this->once())->method('getSampleType')->willReturn(
  121. \Magento\Downloadable\Helper\Download::LINK_TYPE_URL
  122. );
  123. $sampleMock->expects($this->once())->method('getSampleUrl')->willReturn('sample_url');
  124. $this->objectManager->expects($this->at(1))
  125. ->method('get')
  126. ->with(\Magento\Downloadable\Helper\Download::class)
  127. ->willReturn($this->downloadHelper);
  128. $this->response->expects($this->once())->method('setHttpResponseCode')->with(200)->willReturnSelf();
  129. $this->response->expects($this->any())->method('setHeader')->willReturnSelf();
  130. $this->downloadHelper->expects($this->once())->method('output')->willThrowException(new \Exception());
  131. $this->messageManager->expects($this->once())
  132. ->method('addError')
  133. ->with('Sorry, there was an error getting requested content. Please contact the store owner.')
  134. ->willReturnSelf();
  135. $this->redirect->expects($this->once())->method('getRedirectUrl')->willReturn('redirect_url');
  136. $this->response->expects($this->once())->method('setRedirect')->with('redirect_url')->willReturnSelf();
  137. $this->assertEquals($this->response, $this->sample->execute());
  138. }
  139. public function testExecuteLinkTypeFile()
  140. {
  141. $sampleMock = $this->getMockBuilder(\Magento\Downloadable\Model\Sample::class)
  142. ->disableOriginalConstructor()
  143. ->setMethods(['getId', 'load', 'getSampleType', 'getSampleUrl', 'getBaseSamplePath'])
  144. ->getMock();
  145. $fileHelperMock = $this->getMockBuilder(\Magento\Downloadable\Helper\File::class)
  146. ->disableOriginalConstructor()
  147. ->setMethods(['getFilePath'])
  148. ->getMock();
  149. $this->request->expects($this->once())->method('getParam')->with('sample_id', 0)->willReturn('some_sample_id');
  150. $this->objectManager->expects($this->at(0))
  151. ->method('create')
  152. ->with(\Magento\Downloadable\Model\Sample::class)
  153. ->willReturn($sampleMock);
  154. $sampleMock->expects($this->once())->method('load')->with('some_sample_id')->willReturnSelf();
  155. $sampleMock->expects($this->once())->method('getId')->willReturn('some_sample_id');
  156. $sampleMock->expects($this->any())->method('getSampleType')->willReturn(
  157. \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE
  158. );
  159. $this->objectManager->expects($this->at(1))
  160. ->method('get')
  161. ->with(\Magento\Downloadable\Helper\File::class)
  162. ->willReturn($fileHelperMock);
  163. $fileHelperMock->expects($this->once())->method('getFilePath')->willReturn('file_path');
  164. $this->objectManager->expects($this->at(2))
  165. ->method('get')
  166. ->with(\Magento\Downloadable\Helper\Download::class)
  167. ->willReturn($this->downloadHelper);
  168. $this->response->expects($this->once())->method('setHttpResponseCode')->with(200)->willReturnSelf();
  169. $this->response->expects($this->any())->method('setHeader')->willReturnSelf();
  170. $this->downloadHelper->expects($this->once())->method('output')->willThrowException(new \Exception());
  171. $this->messageManager->expects($this->once())
  172. ->method('addError')
  173. ->with('Sorry, there was an error getting requested content. Please contact the store owner.')
  174. ->willReturnSelf();
  175. $this->redirect->expects($this->once())->method('getRedirectUrl')->willReturn('redirect_url');
  176. $this->response->expects($this->once())->method('setRedirect')->with('redirect_url')->willReturnSelf();
  177. $this->assertEquals($this->response, $this->sample->execute());
  178. }
  179. }