/vendor/magento/module-customer/Test/Unit/Controller/Adminhtml/Index/ViewfileTest.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 222 lines · 138 code · 37 blank · 47 comment · 0 complexity · fef2986e06b691d88cbaa85218a6028c MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Controller\Adminhtml\Index;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class ViewfileTest extends \PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\Controller\Result\RawFactory|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $resultRawFactoryMock;
  16. /**
  17. * @var \Magento\Framework\Controller\Result\Raw|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $resultRawMock;
  20. /**
  21. * @var \Magento\Framework\Url\DecoderInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $urlDecoderMock;
  24. /**
  25. * @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $contextMock;
  28. /**
  29. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  30. */
  31. protected $objectManager;
  32. /**
  33. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $objectManagerMock;
  36. /**
  37. * @var \Magento\MediaStorage\Helper\File\Storage|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $storage;
  40. /**
  41. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $fileSystemMock;
  44. /**
  45. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $responseMock;
  48. /**
  49. * @var \Magento\Framework\Filesystem\Directory\ReadInterface|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $directoryMock;
  52. /**
  53. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. protected $requestMock;
  56. public function setUp()
  57. {
  58. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  59. $this->requestMock = $this->getMock('Magento\Framework\App\RequestInterface', [], [], '', false);
  60. $this->responseMock = $this->getMock('Magento\Framework\App\ResponseInterface', [], [], '', false);
  61. $this->directoryMock = $this->getMock(
  62. 'Magento\Framework\Filesystem\Directory\ReadInterface',
  63. [],
  64. [],
  65. '',
  66. false
  67. );
  68. $this->fileSystemMock = $this->getMock('Magento\Framework\Filesystem', [], [], '', false);
  69. $this->storage = $this->getMock('Magento\MediaStorage\Helper\File\Storage', [], [], '', false);
  70. $this->objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface', [], [], '', false);
  71. $this->contextMock = $this->getMock('Magento\Backend\App\Action\Context', [], [], '', false);
  72. $this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
  73. $this->contextMock->expects($this->any())->method('getResponse')->willReturn($this->responseMock);
  74. $this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
  75. $this->urlDecoderMock = $this->getMock('Magento\Framework\Url\DecoderInterface', [], [], '', false);
  76. $this->resultRawMock = $this->getMock('Magento\Framework\Controller\Result\Raw', [], [], '', false);
  77. $this->resultRawFactoryMock = $this->getMock(
  78. 'Magento\Framework\Controller\Result\RawFactory',
  79. ['create'],
  80. [],
  81. '',
  82. false
  83. );
  84. }
  85. /**
  86. * @throws \Magento\Framework\Exception\NotFoundException
  87. * @expectedException \Magento\Framework\Exception\NotFoundException
  88. */
  89. public function testExecuteNoParamsShouldThrowException()
  90. {
  91. /** @var \Magento\Customer\Controller\Adminhtml\Index\Viewfile $controller */
  92. $controller = $this->objectManager->getObject('Magento\Customer\Controller\Adminhtml\Index\Viewfile');
  93. $controller->execute();
  94. }
  95. public function testExecuteParamFile()
  96. {
  97. $decodedFile = 'decoded_file';
  98. $file = 'file';
  99. $fileName = 'customer/' . $file;
  100. $path = 'path';
  101. $this->requestMock->expects($this->atLeastOnce())->method('getParam')->with('file')->willReturn($decodedFile);
  102. $this->directoryMock->expects($this->once())->method('getAbsolutePath')->with($fileName)->willReturn($path);
  103. $this->fileSystemMock->expects($this->once())->method('getDirectoryRead')
  104. ->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)
  105. ->willReturn($this->directoryMock);
  106. $this->storage->expects($this->once())->method('processStorageFile')->with($path)->willReturn(true);
  107. $this->objectManagerMock->expects($this->any())->method('get')
  108. ->willReturnMap(
  109. [
  110. ['Magento\Framework\Filesystem', $this->fileSystemMock],
  111. ['Magento\MediaStorage\Helper\File\Storage', $this->storage]
  112. ]
  113. );
  114. $this->urlDecoderMock->expects($this->once())->method('decode')->with($decodedFile)->willReturn($file);
  115. $fileResponse = $this->getMock('Magento\Framework\App\ResponseInterface', [], [], '', false);
  116. $fileFactoryMock = $this->getMock('Magento\Framework\App\Response\Http\FileFactory', [], [], '', false);
  117. $fileFactoryMock->expects($this->once())->method('create')->with(
  118. $path,
  119. ['type' => 'filename', 'value' => $fileName],
  120. \Magento\Framework\App\Filesystem\DirectoryList::MEDIA
  121. )->willReturn($fileResponse);
  122. /** @var \Magento\Customer\Controller\Adminhtml\Index\Viewfile $controller */
  123. $controller = $this->objectManager->getObject(
  124. 'Magento\Customer\Controller\Adminhtml\Index\Viewfile',
  125. [
  126. 'context' => $this->contextMock,
  127. 'urlDecoder' => $this->urlDecoderMock,
  128. 'fileFactory' => $fileFactoryMock
  129. ]
  130. );
  131. $controller->execute();
  132. }
  133. public function testExecuteGetParamImage()
  134. {
  135. $decodedFile = 'decoded_file';
  136. $file = 'file';
  137. $fileName = 'customer/' . $file;
  138. $path = 'path';
  139. $stat = ['size' => 10, 'mtime' => 10];
  140. $this->requestMock->expects($this->any())->method('getParam')
  141. ->willReturnMap([['file', null, null], ['image', null, $decodedFile]]);
  142. $this->directoryMock->expects($this->once())->method('getAbsolutePath')->with($fileName)->willReturn($path);
  143. $this->directoryMock->expects($this->once())->method('stat')->with($path)->willReturn($stat);
  144. $this->fileSystemMock->expects($this->once())->method('getDirectoryRead')
  145. ->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)
  146. ->willReturn($this->directoryMock);
  147. $this->storage->expects($this->once())->method('processStorageFile')->with($path)->willReturn(true);
  148. $this->objectManagerMock->expects($this->any())->method('get')
  149. ->willReturnMap(
  150. [
  151. ['Magento\Framework\Filesystem', $this->fileSystemMock],
  152. ['Magento\MediaStorage\Helper\File\Storage', $this->storage]
  153. ]
  154. );
  155. $this->urlDecoderMock->expects($this->once())->method('decode')->with($decodedFile)->willReturn($file);
  156. $this->resultRawMock->expects($this->once())->method('setHttpResponseCode')->with(200)->willReturnSelf();
  157. $this->resultRawMock->expects($this->any())->method('setHeader')
  158. ->willReturnMap(
  159. [
  160. ['Pragma', 'public', true, $this->resultRawMock],
  161. ['Content-type', 'application/octet-stream', true, $this->resultRawMock],
  162. ['Content-Length', $stat['size'], false, $this->resultRawMock],
  163. ['Pragma', 'public', true, $this->resultRawMock],
  164. ]
  165. );
  166. $this->resultRawFactoryMock = $this->getMock(
  167. 'Magento\Framework\Controller\Result\RawFactory',
  168. ['create'],
  169. [],
  170. '',
  171. false
  172. );
  173. $this->resultRawFactoryMock->expects($this->once())->method('create')->willReturn($this->resultRawMock);
  174. /** @var \Magento\Customer\Controller\Adminhtml\Index\Viewfile $controller */
  175. $controller = $this->objectManager->getObject(
  176. 'Magento\Customer\Controller\Adminhtml\Index\Viewfile',
  177. [
  178. 'context' => $this->contextMock,
  179. 'urlDecoder' => $this->urlDecoderMock,
  180. 'resultRawFactory' => $this->resultRawFactoryMock
  181. ]
  182. );
  183. $this->assertSame($this->resultRawMock, $controller->execute());
  184. }
  185. }