WriteTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Filesystem\Test\Unit\File;
  7. use Magento\Framework\Exception\FileSystemException;
  8. use Magento\Framework\Filesystem\File\Write;
  9. use Magento\Framework\Phrase;
  10. /**
  11. * Class WriteTest
  12. */
  13. class WriteTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var Write
  17. */
  18. protected $file;
  19. /**
  20. * @var string
  21. */
  22. protected $path = 'path';
  23. /**
  24. * @var resource
  25. */
  26. protected $resource;
  27. /**
  28. * @var string
  29. */
  30. protected $mode = 'w';
  31. /**
  32. * @var \Magento\Framework\Filesystem\DriverInterface | \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $driver;
  35. protected function setUp()
  36. {
  37. $this->driver = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\DriverInterface::class);
  38. $this->driver->expects($this->any())
  39. ->method('isExists')
  40. ->with($this->path)
  41. ->will($this->returnValue(true));
  42. $this->driver->expects($this->once())
  43. ->method('fileOpen')
  44. ->with($this->path, $this->mode)
  45. ->willReturn(null);
  46. $this->file = new Write($this->path, $this->driver, $this->mode);
  47. }
  48. public function tearDown()
  49. {
  50. $this->file = null;
  51. $this->driver = null;
  52. }
  53. /**
  54. * @expectedException \Magento\Framework\Exception\FileSystemException
  55. */
  56. public function testInstanceFileNotExists()
  57. {
  58. $driver = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\DriverInterface::class);
  59. $driver->expects($this->once())
  60. ->method('isExists')
  61. ->with($this->path)
  62. ->will($this->returnValue(false));
  63. $file = new Write($this->path, $driver, 'r');
  64. $this->assertInstanceOf(\Magento\Framework\Filesystem\File\Read::class, $file);
  65. }
  66. /**
  67. * @expectedException \Magento\Framework\Exception\FileSystemException
  68. */
  69. public function testInstanceFileAlreadyExists()
  70. {
  71. $driver = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\DriverInterface::class);
  72. $driver->expects($this->once())
  73. ->method('isExists')
  74. ->with($this->path)
  75. ->will($this->returnValue(true));
  76. $file = new Write($this->path, $driver, 'x');
  77. $this->assertInstanceOf(\Magento\Framework\Filesystem\File\Read::class, $file);
  78. }
  79. public function testWrite()
  80. {
  81. $result = 4;
  82. $data = 'data';
  83. $this->driver->expects($this->once())
  84. ->method('fileWrite')
  85. ->with($this->resource, $data)
  86. ->will($this->returnValue($result));
  87. $this->assertEquals($result, $this->file->write($data));
  88. }
  89. public function testWriteCsv()
  90. {
  91. $data = [];
  92. $delimiter = ',';
  93. $enclosure = '"';
  94. $result = 0;
  95. $this->driver->expects($this->once())
  96. ->method('filePutCsv')
  97. ->with($this->resource, $data, $delimiter, $enclosure)
  98. ->will($this->returnValue($result));
  99. $this->assertEquals($result, $this->file->writeCsv($data, $delimiter, $enclosure));
  100. }
  101. public function testFlush()
  102. {
  103. $result = true;
  104. $this->driver->expects($this->once())
  105. ->method('fileFlush')
  106. ->with($this->resource)
  107. ->will($this->returnValue($result));
  108. $this->assertEquals($result, $this->file->flush());
  109. }
  110. /**
  111. * @expectedException \Magento\Framework\Exception\FileSystemException
  112. */
  113. public function testWriteException()
  114. {
  115. $data = 'data';
  116. $emptyTranslation = '';
  117. $this->driver->expects($this->once())
  118. ->method('fileWrite')
  119. ->with($this->resource, $data)
  120. ->willThrowException(new FileSystemException(new Phrase($emptyTranslation)));
  121. $this->file->write($data);
  122. }
  123. /**
  124. * @expectedException \Magento\Framework\Exception\FileSystemException
  125. */
  126. public function testWriteCsvException()
  127. {
  128. $data = [];
  129. $delimiter = ',';
  130. $enclosure = '"';
  131. $emptyTranslation = '';
  132. $this->driver->expects($this->once())
  133. ->method('filePutCsv')
  134. ->with($this->resource, $data, $delimiter, $enclosure)
  135. ->willThrowException(new FileSystemException(new Phrase($emptyTranslation)));
  136. $this->file->writeCsv($data, $delimiter, $enclosure);
  137. }
  138. /**
  139. * @expectedException \Magento\Framework\Exception\FileSystemException
  140. */
  141. public function testFlushException()
  142. {
  143. $emptyTranslation = '';
  144. $this->driver->expects($this->once())
  145. ->method('fileFlush')
  146. ->with($this->resource)
  147. ->willThrowException(new FileSystemException(new Phrase($emptyTranslation)));
  148. $this->file->flush();
  149. }
  150. public function testLock()
  151. {
  152. $lockMode = LOCK_EX;
  153. $result = true;
  154. $this->driver->expects($this->once())
  155. ->method('fileLock')
  156. ->with($this->resource, $lockMode)
  157. ->will($this->returnValue($result));
  158. $this->assertEquals($result, $this->file->lock($lockMode));
  159. }
  160. public function testUnlock()
  161. {
  162. $result = true;
  163. $this->driver->expects($this->once())
  164. ->method('fileUnlock')
  165. ->with($this->resource)
  166. ->will($this->returnValue($result));
  167. $this->assertEquals($result, $this->file->unlock());
  168. }
  169. }