Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MediaTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
12 
17 class MediaTest extends \PHPUnit\Framework\TestCase
18 {
19  const MEDIA_DIRECTORY = 'mediaDirectory';
20  const RELATIVE_FILE_PATH = 'test/file.png';
21  const CACHE_FILE_PATH = 'var';
22 
26  private $model;
27 
31  private $configFactoryMock;
32 
36  private $syncFactoryMock;
37 
41  private $closure;
42 
46  private $configMock;
47 
51  private $sync;
52 
56  private $responseMock;
57 
61  private $filesystemMock;
62 
66  private $directoryMock;
67 
68  protected function setUp()
69  {
70  $this->closure = function () {
71  return true;
72  };
73  $this->configMock = $this->createMock(\Magento\MediaStorage\Model\File\Storage\Config::class);
74  $this->sync = $this->createMock(\Magento\MediaStorage\Model\File\Storage\Synchronization::class);
75  $this->configFactoryMock = $this->createPartialMock(
76  \Magento\MediaStorage\Model\File\Storage\ConfigFactory::class,
77  ['create']
78  );
79  $this->configFactoryMock->expects($this->any())
80  ->method('create')
81  ->will($this->returnValue($this->configMock));
82  $this->syncFactoryMock = $this->createPartialMock(
83  \Magento\MediaStorage\Model\File\Storage\SynchronizationFactory::class,
84  ['create']
85  );
86  $this->syncFactoryMock->expects($this->any())
87  ->method('create')
88  ->will($this->returnValue($this->sync));
89 
90  $this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
91  $this->directoryMock = $this->getMockForAbstractClass(
92  \Magento\Framework\Filesystem\Directory\WriteInterface::class
93  );
94 
95  $this->filesystemMock->expects($this->any())
96  ->method('getDirectoryWrite')
97  ->with(DirectoryList::PUB)
98  ->will($this->returnValue($this->directoryMock));
99 
100  $this->responseMock = $this->createMock(\Magento\MediaStorage\Model\File\Storage\Response::class);
101 
102  $objectManager = new ObjectManager($this);
103  $this->model = $objectManager->getObject(
104  \Magento\MediaStorage\App\Media::class,
105  [
106  'configFactory' => $this->configFactoryMock,
107  'syncFactory' => $this->syncFactoryMock,
108  'response' => $this->responseMock,
109  'isAllowed' => $this->closure,
110  'mediaDirectory' => false,
111  'configCacheFile' => self::CACHE_FILE_PATH,
112  'relativeFileName' => self::RELATIVE_FILE_PATH,
113  'filesystem' => $this->filesystemMock,
114  'placeholderFactory' => $this->createConfiguredMock(
115  PlaceholderFactory::class,
116  [
117  'create' => $this->createMock(Placeholder::class)
118  ]
119  ),
120  ]
121  );
122  }
123 
124  protected function tearDown()
125  {
126  unset($this->model);
127  }
128 
130  {
131  $objectManager = new ObjectManager($this);
132  $this->model = $objectManager->getObject(
133  \Magento\MediaStorage\App\Media::class,
134  [
135  'configFactory' => $this->configFactoryMock,
136  'syncFactory' => $this->syncFactoryMock,
137  'response' => $this->responseMock,
138  'isAllowed' => $this->closure,
139  'mediaDirectory' => false,
140  'configCacheFile' => self::CACHE_FILE_PATH,
141  'relativeFileName' => self::RELATIVE_FILE_PATH,
142  'filesystem' => $this->filesystemMock
143  ]
144  );
145  $filePath = '/absolute/path/to/test/file.png';
146  $this->directoryMock->expects($this->any())
147  ->method('getAbsolutePath')
148  ->will($this->returnValueMap(
149  [
150  [null, self::MEDIA_DIRECTORY],
151  [self::RELATIVE_FILE_PATH, $filePath],
152  ]
153  ));
154  $this->configMock->expects($this->once())->method('save');
155  $this->sync->expects($this->once())->method('synchronize')->with(self::RELATIVE_FILE_PATH);
156  $this->directoryMock->expects($this->once())
157  ->method('isReadable')
158  ->with(self::RELATIVE_FILE_PATH)
159  ->will($this->returnValue(true));
160  $this->responseMock->expects($this->once())->method('setFilePath')->with($filePath);
161  $this->model->launch();
162  }
163 
165  {
166  $filePath = '/absolute/path/to/test/file.png';
167  $this->sync->expects($this->once())->method('synchronize')->with(self::RELATIVE_FILE_PATH);
168  $this->directoryMock->expects($this->once())
169  ->method('isReadable')
170  ->with(self::RELATIVE_FILE_PATH)
171  ->will($this->returnValue(true));
172  $this->directoryMock->expects($this->any())
173  ->method('getAbsolutePath')
174  ->will($this->returnValueMap(
175  [
176  [null, self::MEDIA_DIRECTORY],
177  [self::RELATIVE_FILE_PATH, $filePath],
178  ]
179  ));
180  $this->responseMock->expects($this->once())->method('setFilePath')->with($filePath);
181  $this->assertSame($this->responseMock, $this->model->launch());
182  }
183 
185  {
186  $this->sync->expects($this->once())->method('synchronize')->with(self::RELATIVE_FILE_PATH);
187  $this->directoryMock->expects($this->once())
188  ->method('getAbsolutePath')
189  ->with()
190  ->will($this->returnValue(self::MEDIA_DIRECTORY));
191  $this->directoryMock->expects($this->once())
192  ->method('isReadable')
193  ->with(self::RELATIVE_FILE_PATH)
194  ->will($this->returnValue(false));
195  $this->assertSame($this->responseMock, $this->model->launch());
196  }
197 
204  public function testCatchException($isDeveloper, $setBodyCalls)
205  {
206  $bootstrap = $this->createMock(\Magento\Framework\App\Bootstrap::class);
207  $exception = $this->createMock(\Exception::class);
208  $this->responseMock->expects($this->once())
209  ->method('setHttpResponseCode')
210  ->with(404);
211  $bootstrap->expects($this->once())
212  ->method('isDeveloperMode')
213  ->will($this->returnValue($isDeveloper));
214  $this->responseMock->expects($this->exactly($setBodyCalls))
215  ->method('setBody');
216  $this->responseMock->expects($this->once())
217  ->method('sendResponse');
218  $this->model->catchException($bootstrap, $exception);
219  }
220 
224  public function catchExceptionDataProvider()
225  {
226  return [
227  'default mode' => [false, 0],
228  'developer mode' => [true, 1],
229  ];
230  }
231 }
$objectManager
Definition: bootstrap.php:17
if(defined('TESTS_MAGENTO_INSTALLATION') &&TESTS_MAGENTO_INSTALLATION==='enabled') $bootstrap
Definition: bootstrap.php:73
testCatchException($isDeveloper, $setBodyCalls)
Definition: MediaTest.php:204