PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tests/integration/testsuite/Magento/Framework/Filesystem/File/WriteTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 210 lines | 108 code | 15 blank | 87 comment | 0 complexity | 75df324ab3a787bb8e2619b403fc91c8 MD5 | raw file
  1. <?php
  2. /**
  3. * Test for \Magento\Framework\Filesystem\File\Write
  4. *
  5. * Copyright © 2016 Magento. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Filesystem\File;
  9. use Magento\TestFramework\Helper\Bootstrap;
  10. class WriteTest extends \PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * Current file path
  14. *
  15. * @var string
  16. */
  17. private $currentFilePath;
  18. /**
  19. * Test instance of Write.
  20. */
  21. public function testInstance()
  22. {
  23. $file = $this->getFileInstance('popup.csv', 'r');
  24. $this->assertTrue($file instanceof ReadInterface);
  25. $this->assertTrue($file instanceof WriteInterface);
  26. $file->close();
  27. }
  28. /**
  29. * Test exceptions on attempt to open existing file with x mode
  30. *
  31. * @dataProvider fileExistProvider
  32. * @param $path
  33. * @param $mode
  34. * @expectedException \Magento\Framework\Exception\FileSystemException
  35. */
  36. public function testFileExistException($path, $mode)
  37. {
  38. $this->getFileInstance($path, $mode);
  39. }
  40. /**
  41. * Data provider for modeProvider
  42. *
  43. * @return array
  44. */
  45. public function fileExistProvider()
  46. {
  47. return [['popup.csv', 'x'], ['popup.csv', 'x+']];
  48. }
  49. /**
  50. * Test for write method
  51. *
  52. * @dataProvider writeProvider
  53. * @param string $path
  54. * @param string $mode
  55. * @param string $write
  56. * @param string $expectedResult
  57. */
  58. public function testWriteOnly($path, $mode, $write, $expectedResult)
  59. {
  60. $file = $this->getFileInstance($path, $mode);
  61. $result = $file->write($write);
  62. $file->close();
  63. $this->removeCurrentFile();
  64. $this->assertEquals($expectedResult, $result);
  65. }
  66. /**
  67. * Data provider for modeProvider
  68. *
  69. * @return array
  70. */
  71. public function writeProvider()
  72. {
  73. return [
  74. ['new1.csv', 'w', 'write check', 11],
  75. ['new3.csv', 'a', 'write check', 11],
  76. ['new5.csv', 'x', 'write check', 11],
  77. ['new7.csv', 'c', 'write check', 11],
  78. ];
  79. }
  80. /**
  81. * Test for write method
  82. *
  83. * @dataProvider writeAndReadProvider
  84. * @param string $path
  85. * @param string $mode
  86. * @param string $write
  87. * @param string $expectedResult
  88. */
  89. public function testWriteAndRead($path, $mode, $write, $expectedResult)
  90. {
  91. $file = $this->getFileInstance($path, $mode);
  92. $result = $file->write($write);
  93. $file->seek(0);
  94. $read = $file->read($result);
  95. $file->close();
  96. $this->removeCurrentFile();
  97. $this->assertEquals($expectedResult, $result);
  98. $this->assertEquals($write, $read);
  99. }
  100. /**
  101. * Data provider for modeProvider
  102. *
  103. * @return array
  104. */
  105. public function writeAndReadProvider()
  106. {
  107. return [
  108. ['new2.csv', 'w+', 'write check', 11],
  109. ['new4.csv', 'a+', 'write check', 11],
  110. ['new6.csv', 'x+', 'write check', 11],
  111. ['new8.csv', 'c+', 'write check', 11],
  112. ];
  113. }
  114. /**
  115. * Writes one CSV row to the file.
  116. *
  117. * @dataProvider csvDataProvider
  118. * @param array $expectedData
  119. * @param string $path
  120. * @param array $data
  121. * @param string $delimiter
  122. * @param string $enclosure
  123. */
  124. public function testWriteCsv($expectedData, $path, array $data, $delimiter = ',', $enclosure = '"')
  125. {
  126. $file = $this->getFileInstance($path, 'w+');
  127. $result = $file->writeCsv($data, $delimiter, $enclosure);
  128. $file->seek(0);
  129. $read = $file->readCsv($result, $delimiter, $enclosure);
  130. $file->close();
  131. $this->removeCurrentFile();
  132. $this->assertEquals($expectedData, $read);
  133. }
  134. /**
  135. * Data provider for testWriteCsv
  136. *
  137. * @return array
  138. */
  139. public function csvDataProvider()
  140. {
  141. return [
  142. [['field1', 'field2'], 'newcsv1.csv', ['field1', 'field2'], ',', '"'],
  143. [['field1', 'field2'], 'newcsv1.csv', ['field1', 'field2'], '%', '@'],
  144. [[' =field1', 'field2'], 'newcsv1.csv', ['=field1', 'field2'], '%', '@'],
  145. ];
  146. }
  147. /**
  148. * Test for lock and unlock functions
  149. */
  150. public function testLockUnlock()
  151. {
  152. $file = $this->getFileInstance('locked.csv', 'w+');
  153. $this->assertTrue($file->lock());
  154. $this->assertTrue($file->unlock());
  155. $file->close();
  156. $this->removeCurrentFile();
  157. }
  158. /**
  159. * Test for flush method
  160. */
  161. public function testFlush()
  162. {
  163. $file = $this->getFileInstance('locked.csv', 'w+');
  164. $this->assertTrue($file->flush());
  165. $file->close();
  166. $this->removeCurrentFile();
  167. }
  168. /**
  169. * Remove current file
  170. */
  171. private function removeCurrentFile()
  172. {
  173. unlink($this->currentFilePath);
  174. }
  175. /**
  176. * Get readable file instance
  177. * Get full path for files located in _files directory
  178. *
  179. * @param string $path
  180. * @param string $mode
  181. * @return Write
  182. */
  183. private function getFileInstance($path, $mode)
  184. {
  185. $this->currentFilePath = __DIR__ . '/../_files/' . $path;
  186. return Bootstrap::getObjectManager()->create(
  187. 'Magento\Framework\Filesystem\File\Write',
  188. [
  189. 'path' => $this->currentFilePath,
  190. 'driver' => new \Magento\Framework\Filesystem\Driver\File(),
  191. 'mode' => $mode,
  192. ]
  193. );
  194. }
  195. }