PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/yousafsyed/easternglamor
PHP | 312 lines | 210 code | 14 blank | 88 comment | 0 complexity | 4f7c7f21ea03c5e2ab03fd94d2e0c987 MD5 | raw file
  1. <?php
  2. /**
  3. * Test for \Magento\Framework\Filesystem\File\Read
  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 ReadTest extends \PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * Test instance of Read
  14. */
  15. public function testInstance()
  16. {
  17. $file = $this->getFileInstance('popup.csv');
  18. $this->assertTrue($file instanceof ReadInterface);
  19. }
  20. /**
  21. * Test for assertValid method
  22. * Expected exception for file that does not exist and file without access
  23. *
  24. * @dataProvider providerNotValidFiles
  25. * @param string $path
  26. * @expectedException \Magento\Framework\Exception\FileSystemException
  27. */
  28. public function testAssertValid($path)
  29. {
  30. $this->getFileInstance($path);
  31. }
  32. /**
  33. * Data provider for testAssertValid
  34. *
  35. * @return array
  36. */
  37. public function providerNotValidFiles()
  38. {
  39. return [['invalid.csv']]; //File does not exist
  40. }
  41. /**
  42. * Test for read method
  43. *
  44. * @dataProvider providerRead
  45. * @param string $path
  46. * @param int $length
  47. * @param string $expectedResult
  48. */
  49. public function testRead($path, $length, $expectedResult)
  50. {
  51. $file = $this->getFileInstance($path);
  52. $result = $file->read($length);
  53. $this->assertEquals($result, $expectedResult);
  54. }
  55. /**
  56. * Data provider for testRead
  57. *
  58. * @return array
  59. */
  60. public function providerRead()
  61. {
  62. return [['popup.csv', 10, 'var myData'], ['popup.csv', 15, 'var myData = 5;']];
  63. }
  64. /**
  65. * Test readAll
  66. *
  67. * @dataProvider readAllProvider
  68. * @param string $path
  69. * @param string $content
  70. */
  71. public function testReadAll($path, $content)
  72. {
  73. $file = $this->getFileInstance($path);
  74. $this->assertEquals($content, $file->readAll($path));
  75. }
  76. /**
  77. * Data provider for testReadFile
  78. *
  79. * @return array
  80. */
  81. public function readAllProvider()
  82. {
  83. return [
  84. ['popup.csv', 'var myData = 5;'],
  85. ['data.csv', '"field1", "field2"' . "\n" . '"field3", "field4"' . "\n"]
  86. ];
  87. }
  88. /**
  89. * Test readLine
  90. *
  91. * @dataProvider readLineProvider
  92. * @param string $path
  93. * @param array $lines
  94. * @param int $length
  95. */
  96. public function testReadLine($path, $lines, $length)
  97. {
  98. $file = $this->getFileInstance($path);
  99. foreach ($lines as $line) {
  100. $this->assertEquals($line, $file->readLine($length, "\n"));
  101. }
  102. }
  103. /**
  104. * Data provider for testReadLine
  105. *
  106. * @return array
  107. */
  108. public function readLineProvider()
  109. {
  110. return [
  111. ['popup.csv', ['var myData = 5;'], 999],
  112. ['data.csv', ['"field1", "field2"', '"field3", "field4"'], 999],
  113. ['popup.csv', ['var'], 3],
  114. ['data.csv', ['"f', 'ie', 'ld', '1"'], 2]
  115. ];
  116. }
  117. /**
  118. * Test for stat method
  119. *
  120. * @dataProvider statProvider
  121. * @param string $path
  122. */
  123. public function testStat($path)
  124. {
  125. $file = $this->getFileInstance($path);
  126. $expectedInfo = [
  127. 'dev',
  128. 'ino',
  129. 'mode',
  130. 'nlink',
  131. 'uid',
  132. 'gid',
  133. 'rdev',
  134. 'size',
  135. 'atime',
  136. 'mtime',
  137. 'ctime',
  138. 'blksize',
  139. 'blocks',
  140. ];
  141. $result = $file->stat();
  142. foreach ($expectedInfo as $key) {
  143. $this->assertTrue(array_key_exists($key, $result));
  144. }
  145. }
  146. /**
  147. * Data provider for testStat
  148. *
  149. * @return array
  150. */
  151. public function statProvider()
  152. {
  153. return [['popup.csv'], ['foo/file_three.txt']];
  154. }
  155. /**
  156. * Test for readCsv method
  157. *
  158. * @dataProvider providerCsv
  159. * @param string $path
  160. * @param int $length
  161. * @param string $delimiter
  162. * @param string $enclosure
  163. * @param string $escape
  164. * @param array $expectedRow1
  165. * @param array $expectedRow2
  166. */
  167. public function testReadCsv($path, $length, $delimiter, $enclosure, $escape, $expectedRow1, $expectedRow2)
  168. {
  169. $file = $this->getFileInstance($path);
  170. $actualRow1 = $file->readCsv($length, $delimiter, $enclosure, $escape);
  171. $actualRow2 = $file->readCsv($length, $delimiter, $enclosure, $escape);
  172. $this->assertEquals($expectedRow1, $actualRow1);
  173. $this->assertEquals($expectedRow2, $actualRow2);
  174. }
  175. /**
  176. * Data provider for testReadCsv
  177. *
  178. * @return array
  179. */
  180. public function providerCsv()
  181. {
  182. return [['data.csv', 0, ',', '"', '\\', ['field1', 'field2'], ['field3', 'field4']]];
  183. }
  184. /**
  185. * Test for tell method
  186. *
  187. * @dataProvider providerPosition
  188. * @param string $path
  189. * @param int $position
  190. */
  191. public function testTell($path, $position)
  192. {
  193. $file = $this->getFileInstance($path);
  194. $file->read($position);
  195. $this->assertEquals($position, $file->tell());
  196. }
  197. /**
  198. * Data provider for testTell
  199. *
  200. * @return array
  201. */
  202. public function providerPosition()
  203. {
  204. return [['popup.csv', 5], ['popup.csv', 10]];
  205. }
  206. /**
  207. * Test for seek method
  208. *
  209. * @dataProvider providerSeek
  210. * @param string $path
  211. * @param int $position
  212. * @param int $whence
  213. * @param int $tell
  214. */
  215. public function testSeek($path, $position, $whence, $tell)
  216. {
  217. $file = $this->getFileInstance($path);
  218. $file->seek($position, $whence);
  219. $this->assertEquals($tell, $file->tell());
  220. }
  221. /**
  222. * Data provider for testSeek
  223. *
  224. * @return array
  225. */
  226. public function providerSeek()
  227. {
  228. return [
  229. ['popup.csv', 5, SEEK_SET, 5],
  230. ['popup.csv', 10, SEEK_CUR, 10],
  231. ['popup.csv', -10, SEEK_END, 5]
  232. ];
  233. }
  234. /**
  235. * Test for eof method
  236. *
  237. * @dataProvider providerEof
  238. * @param string $path
  239. * @param int $position
  240. */
  241. public function testEofFalse($path, $position)
  242. {
  243. $file = $this->getFileInstance($path);
  244. $file->seek($position);
  245. $this->assertFalse($file->eof());
  246. }
  247. /**
  248. * Data provider for testEofTrue
  249. *
  250. * @return array
  251. */
  252. public function providerEof()
  253. {
  254. return [['popup.csv', 5, false], ['popup.csv', 10, false]];
  255. }
  256. /**
  257. * Test for eof method
  258. */
  259. public function testEofTrue()
  260. {
  261. $file = $this->getFileInstance('popup.csv');
  262. $file->seek(0, SEEK_END);
  263. $file->read(1);
  264. $this->assertTrue($file->eof());
  265. }
  266. /**
  267. * Test for close method
  268. */
  269. public function testClose()
  270. {
  271. $file = $this->getFileInstance('popup.csv');
  272. $this->assertTrue($file->close());
  273. }
  274. /**
  275. * Get readable file instance
  276. * Get full path for files located in _files directory
  277. *
  278. * @param $path
  279. * @return Read
  280. */
  281. private function getFileInstance($path)
  282. {
  283. $fullPath = __DIR__ . '/../_files/' . $path;
  284. return Bootstrap::getObjectManager()->create(
  285. 'Magento\Framework\Filesystem\File\Read',
  286. ['path' => $fullPath, 'driver' => new \Magento\Framework\Filesystem\Driver\File()]
  287. );
  288. }
  289. }