ReadTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\Filesystem\File\Read;
  8. /**
  9. * Class ReadTest
  10. */
  11. class ReadTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var Read
  15. */
  16. protected $file;
  17. /**
  18. * @var string
  19. */
  20. protected $path = 'path';
  21. /**
  22. * @var resource
  23. */
  24. protected $resource;
  25. /**
  26. * @var string
  27. */
  28. protected $mode = 'r';
  29. /**
  30. * @var \Magento\Framework\Filesystem\DriverInterface | \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $driver;
  33. protected function setUp()
  34. {
  35. $this->driver = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\DriverInterface::class);
  36. $this->driver->expects($this->any())
  37. ->method('isExists')
  38. ->with($this->path)
  39. ->will($this->returnValue(true));
  40. $this->driver->expects($this->once())
  41. ->method('fileOpen')
  42. ->with($this->path, $this->mode)
  43. ->willReturn(null);
  44. $this->file = new Read($this->path, $this->driver);
  45. }
  46. public function tearDown()
  47. {
  48. $this->file = null;
  49. $this->driver = null;
  50. }
  51. /**
  52. * @expectedException \Magento\Framework\Exception\FileSystemException
  53. */
  54. public function testInstanceFileNotExists()
  55. {
  56. $driver = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\DriverInterface::class);
  57. $driver->expects($this->once())
  58. ->method('isExists')
  59. ->with($this->path)
  60. ->will($this->returnValue(false));
  61. $file = new Read($this->path, $driver);
  62. $this->assertInstanceOf(\Magento\Framework\Filesystem\File\Read::class, $file);
  63. }
  64. public function testRead()
  65. {
  66. $length = 5;
  67. $result = 'content';
  68. $this->driver->expects($this->once())
  69. ->method('fileRead')
  70. ->with($this->resource, $length)
  71. ->will($this->returnValue($result));
  72. $this->assertEquals($result, $this->file->read($length));
  73. }
  74. public function testReadAll()
  75. {
  76. $flag = 5;
  77. $context = null;
  78. $result = 'content';
  79. $this->driver->expects($this->once())
  80. ->method('fileGetContents')
  81. ->with($this->path, $flag, $context)
  82. ->will($this->returnValue($result));
  83. $this->assertEquals($result, $this->file->readAll($flag, $context));
  84. }
  85. public function testReadLine()
  86. {
  87. $length = 5;
  88. $ending = '\n';
  89. $result = 'content';
  90. $this->driver->expects($this->once())
  91. ->method('fileReadLine')
  92. ->with($this->resource, $length, $ending)
  93. ->will($this->returnValue($result));
  94. $this->assertEquals($result, $this->file->readLine($length, $ending));
  95. }
  96. public function testReadCsv()
  97. {
  98. $length = 0;
  99. $delimiter = ',';
  100. $enclosure = '"';
  101. $escape = '\\';
  102. $result = 'content';
  103. $this->driver->expects($this->once())
  104. ->method('fileGetCsv')
  105. ->with($this->resource, $length, $delimiter, $enclosure, $escape)
  106. ->will($this->returnValue($result));
  107. $this->assertEquals($result, $this->file->readCsv($length, $delimiter, $enclosure, $escape));
  108. }
  109. public function testTell()
  110. {
  111. $result = 'content';
  112. $this->driver->expects($this->once())
  113. ->method('fileTell')
  114. ->with($this->resource)
  115. ->will($this->returnValue($result));
  116. $this->assertEquals($result, $this->file->tell());
  117. }
  118. public function testEof()
  119. {
  120. $result = 'content';
  121. $this->driver->expects($this->once())
  122. ->method('endOfFile')
  123. ->with($this->resource)
  124. ->will($this->returnValue($result));
  125. $this->assertEquals($result, $this->file->eof());
  126. }
  127. public function testClose()
  128. {
  129. $result = 'content';
  130. $this->driver->expects($this->once())
  131. ->method('fileClose')
  132. ->with($this->resource)
  133. ->will($this->returnValue($result));
  134. $this->assertEquals($result, $this->file->close());
  135. }
  136. public function testStat()
  137. {
  138. $result = 'content';
  139. $this->driver->expects($this->once())
  140. ->method('stat')
  141. ->with($this->path)
  142. ->will($this->returnValue($result));
  143. $this->assertEquals($result, $this->file->stat());
  144. }
  145. public function testSeek()
  146. {
  147. $offset = 5;
  148. $whence = SEEK_SET;
  149. $result = 'content';
  150. $this->driver->expects($this->once())
  151. ->method('fileSeek')
  152. ->with($this->resource, $offset, $whence)
  153. ->will($this->returnValue($result));
  154. $this->assertEquals($result, $this->file->seek($offset, $whence));
  155. }
  156. }