MediaTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\MediaStorage\Test\Unit\App;
  7. use Magento\Catalog\Model\View\Asset\Placeholder;
  8. use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. /**
  12. * Class MediaTest
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class MediaTest extends \PHPUnit\Framework\TestCase
  16. {
  17. const MEDIA_DIRECTORY = 'mediaDirectory';
  18. const RELATIVE_FILE_PATH = 'test/file.png';
  19. const CACHE_FILE_PATH = 'var';
  20. /**
  21. * @var \Magento\MediaStorage\App\Media
  22. */
  23. private $model;
  24. /**
  25. * @var \Magento\MediaStorage\Model\File\Storage\ConfigFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $configFactoryMock;
  28. /**
  29. * @var \Magento\MediaStorage\Model\File\Storage\SynchronizationFactory|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $syncFactoryMock;
  32. /**
  33. * @var callable
  34. */
  35. private $closure;
  36. /**
  37. * @var \PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $configMock;
  40. /**
  41. * @var \PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $sync;
  44. /**
  45. * @var \Magento\MediaStorage\Model\File\Storage\Response|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $responseMock;
  48. /**
  49. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $filesystemMock;
  52. /**
  53. * @var \Magento\Framework\Filesystem\Directory\Read|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. private $directoryMock;
  56. protected function setUp()
  57. {
  58. $this->closure = function () {
  59. return true;
  60. };
  61. $this->configMock = $this->createMock(\Magento\MediaStorage\Model\File\Storage\Config::class);
  62. $this->sync = $this->createMock(\Magento\MediaStorage\Model\File\Storage\Synchronization::class);
  63. $this->configFactoryMock = $this->createPartialMock(
  64. \Magento\MediaStorage\Model\File\Storage\ConfigFactory::class,
  65. ['create']
  66. );
  67. $this->configFactoryMock->expects($this->any())
  68. ->method('create')
  69. ->will($this->returnValue($this->configMock));
  70. $this->syncFactoryMock = $this->createPartialMock(
  71. \Magento\MediaStorage\Model\File\Storage\SynchronizationFactory::class,
  72. ['create']
  73. );
  74. $this->syncFactoryMock->expects($this->any())
  75. ->method('create')
  76. ->will($this->returnValue($this->sync));
  77. $this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
  78. $this->directoryMock = $this->getMockForAbstractClass(
  79. \Magento\Framework\Filesystem\Directory\WriteInterface::class
  80. );
  81. $this->filesystemMock->expects($this->any())
  82. ->method('getDirectoryWrite')
  83. ->with(DirectoryList::PUB)
  84. ->will($this->returnValue($this->directoryMock));
  85. $this->responseMock = $this->createMock(\Magento\MediaStorage\Model\File\Storage\Response::class);
  86. $objectManager = new ObjectManager($this);
  87. $this->model = $objectManager->getObject(
  88. \Magento\MediaStorage\App\Media::class,
  89. [
  90. 'configFactory' => $this->configFactoryMock,
  91. 'syncFactory' => $this->syncFactoryMock,
  92. 'response' => $this->responseMock,
  93. 'isAllowed' => $this->closure,
  94. 'mediaDirectory' => false,
  95. 'configCacheFile' => self::CACHE_FILE_PATH,
  96. 'relativeFileName' => self::RELATIVE_FILE_PATH,
  97. 'filesystem' => $this->filesystemMock,
  98. 'placeholderFactory' => $this->createConfiguredMock(
  99. PlaceholderFactory::class,
  100. [
  101. 'create' => $this->createMock(Placeholder::class)
  102. ]
  103. ),
  104. ]
  105. );
  106. }
  107. protected function tearDown()
  108. {
  109. unset($this->model);
  110. }
  111. public function testProcessRequestCreatesConfigFileMediaDirectoryIsNotProvided()
  112. {
  113. $objectManager = new ObjectManager($this);
  114. $this->model = $objectManager->getObject(
  115. \Magento\MediaStorage\App\Media::class,
  116. [
  117. 'configFactory' => $this->configFactoryMock,
  118. 'syncFactory' => $this->syncFactoryMock,
  119. 'response' => $this->responseMock,
  120. 'isAllowed' => $this->closure,
  121. 'mediaDirectory' => false,
  122. 'configCacheFile' => self::CACHE_FILE_PATH,
  123. 'relativeFileName' => self::RELATIVE_FILE_PATH,
  124. 'filesystem' => $this->filesystemMock
  125. ]
  126. );
  127. $filePath = '/absolute/path/to/test/file.png';
  128. $this->directoryMock->expects($this->any())
  129. ->method('getAbsolutePath')
  130. ->will($this->returnValueMap(
  131. [
  132. [null, self::MEDIA_DIRECTORY],
  133. [self::RELATIVE_FILE_PATH, $filePath],
  134. ]
  135. ));
  136. $this->configMock->expects($this->once())->method('save');
  137. $this->sync->expects($this->once())->method('synchronize')->with(self::RELATIVE_FILE_PATH);
  138. $this->directoryMock->expects($this->once())
  139. ->method('isReadable')
  140. ->with(self::RELATIVE_FILE_PATH)
  141. ->will($this->returnValue(true));
  142. $this->responseMock->expects($this->once())->method('setFilePath')->with($filePath);
  143. $this->model->launch();
  144. }
  145. public function testProcessRequestReturnsFileIfItsProperlySynchronized()
  146. {
  147. $filePath = '/absolute/path/to/test/file.png';
  148. $this->sync->expects($this->once())->method('synchronize')->with(self::RELATIVE_FILE_PATH);
  149. $this->directoryMock->expects($this->once())
  150. ->method('isReadable')
  151. ->with(self::RELATIVE_FILE_PATH)
  152. ->will($this->returnValue(true));
  153. $this->directoryMock->expects($this->any())
  154. ->method('getAbsolutePath')
  155. ->will($this->returnValueMap(
  156. [
  157. [null, self::MEDIA_DIRECTORY],
  158. [self::RELATIVE_FILE_PATH, $filePath],
  159. ]
  160. ));
  161. $this->responseMock->expects($this->once())->method('setFilePath')->with($filePath);
  162. $this->assertSame($this->responseMock, $this->model->launch());
  163. }
  164. public function testProcessRequestReturnsNotFoundIfFileIsNotSynchronized()
  165. {
  166. $this->sync->expects($this->once())->method('synchronize')->with(self::RELATIVE_FILE_PATH);
  167. $this->directoryMock->expects($this->once())
  168. ->method('getAbsolutePath')
  169. ->with()
  170. ->will($this->returnValue(self::MEDIA_DIRECTORY));
  171. $this->directoryMock->expects($this->once())
  172. ->method('isReadable')
  173. ->with(self::RELATIVE_FILE_PATH)
  174. ->will($this->returnValue(false));
  175. $this->assertSame($this->responseMock, $this->model->launch());
  176. }
  177. /**
  178. * @param bool $isDeveloper
  179. * @param int $setBodyCalls
  180. *
  181. * @dataProvider catchExceptionDataProvider
  182. */
  183. public function testCatchException($isDeveloper, $setBodyCalls)
  184. {
  185. $bootstrap = $this->createMock(\Magento\Framework\App\Bootstrap::class);
  186. $exception = $this->createMock(\Exception::class);
  187. $this->responseMock->expects($this->once())
  188. ->method('setHttpResponseCode')
  189. ->with(404);
  190. $bootstrap->expects($this->once())
  191. ->method('isDeveloperMode')
  192. ->will($this->returnValue($isDeveloper));
  193. $this->responseMock->expects($this->exactly($setBodyCalls))
  194. ->method('setBody');
  195. $this->responseMock->expects($this->once())
  196. ->method('sendResponse');
  197. $this->model->catchException($bootstrap, $exception);
  198. }
  199. /**
  200. * @return array
  201. */
  202. public function catchExceptionDataProvider()
  203. {
  204. return [
  205. 'default mode' => [false, 0],
  206. 'developer mode' => [true, 1],
  207. ];
  208. }
  209. }