/src/test/php/org/bovigo/vfs/vfsStreamFileTestCase.php

https://github.com/wiltave/vfsStream · PHP · 254 lines · 151 code · 14 blank · 89 comment · 0 complexity · b5818064438b17c1ca66dc0a77305675 MD5 · raw file

  1. <?php
  2. /**
  3. * Test for org::bovigo::vfs::vfsStreamFile.
  4. *
  5. * @package bovigo_vfs
  6. * @subpackage test
  7. */
  8. require_once 'org/bovigo/vfs/vfsStreamFile.php';
  9. require_once 'PHPUnit/Framework.php';
  10. /**
  11. * Test for org::bovigo::vfs::vfsStreamFile.
  12. *
  13. * @package bovigo_vfs
  14. * @subpackage test
  15. */
  16. class vfsStreamFileTestCase extends PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * instance to test
  20. *
  21. * @var vfsStreamFile
  22. */
  23. protected $file;
  24. /**
  25. * set up test environment
  26. */
  27. public function setUp()
  28. {
  29. $this->file = new vfsStreamFile('foo');
  30. }
  31. /**
  32. * test default values and methods
  33. *
  34. * @test
  35. */
  36. public function defaultValues()
  37. {
  38. $this->assertEquals(vfsStreamContent::TYPE_FILE, $this->file->getType());
  39. $this->assertEquals('foo', $this->file->getName());
  40. $this->assertTrue($this->file->appliesTo('foo'));
  41. $this->assertFalse($this->file->appliesTo('foo/bar'));
  42. $this->assertFalse($this->file->appliesTo('bar'));
  43. }
  44. /**
  45. * test setting and getting the content of a file
  46. *
  47. * @test
  48. */
  49. public function content()
  50. {
  51. $this->assertNull($this->file->getContent());
  52. $this->assertSame($this->file, $this->file->setContent('bar'));
  53. $this->assertEquals('bar', $this->file->getContent());
  54. $this->assertSame($this->file, $this->file->withContent('baz'));
  55. $this->assertEquals('baz', $this->file->getContent());
  56. }
  57. /**
  58. * test renaming the directory
  59. *
  60. * @test
  61. */
  62. public function rename()
  63. {
  64. $this->file->rename('bar');
  65. $this->assertEquals('bar', $this->file->getName());
  66. $this->assertFalse($this->file->appliesTo('foo'));
  67. $this->assertFalse($this->file->appliesTo('foo/bar'));
  68. $this->assertTrue($this->file->appliesTo('bar'));
  69. }
  70. /**
  71. * test reading contents from the file
  72. *
  73. * @test
  74. */
  75. public function readEmptyFile()
  76. {
  77. $this->assertTrue($this->file->eof());
  78. $this->assertEquals(0, $this->file->size());
  79. $this->assertEquals('', $this->file->read(5));
  80. $this->assertEquals(5, $this->file->getBytesRead());
  81. $this->assertTrue($this->file->eof());
  82. }
  83. /**
  84. * test reading contents from the file
  85. *
  86. * @test
  87. */
  88. public function read()
  89. {
  90. $this->file->setContent('foobarbaz');
  91. $this->assertFalse($this->file->eof());
  92. $this->assertEquals(9, $this->file->size());
  93. $this->assertEquals('foo', $this->file->read(3));
  94. $this->assertEquals(3, $this->file->getBytesRead());
  95. $this->assertFalse($this->file->eof());
  96. $this->assertEquals(9, $this->file->size());
  97. $this->assertEquals('bar', $this->file->read(3));
  98. $this->assertEquals(6, $this->file->getBytesRead());
  99. $this->assertFalse($this->file->eof());
  100. $this->assertEquals(9, $this->file->size());
  101. $this->assertEquals('baz', $this->file->read(3));
  102. $this->assertEquals(9, $this->file->getBytesRead());
  103. $this->assertEquals(9, $this->file->size());
  104. $this->assertTrue($this->file->eof());
  105. $this->assertEquals('', $this->file->read(3));
  106. }
  107. /**
  108. * test seeking to offset
  109. *
  110. * @test
  111. */
  112. public function seekEmptyFile()
  113. {
  114. $this->assertFalse($this->file->seek(0, 55));
  115. $this->assertTrue($this->file->seek(0, SEEK_SET));
  116. $this->assertEquals(0, $this->file->getBytesRead());
  117. $this->assertTrue($this->file->seek(5, SEEK_SET));
  118. $this->assertEquals(5, $this->file->getBytesRead());
  119. $this->assertTrue($this->file->seek(0, SEEK_CUR));
  120. $this->assertEquals(5, $this->file->getBytesRead());
  121. $this->assertTrue($this->file->seek(2, SEEK_CUR));
  122. $this->assertEquals(7, $this->file->getBytesRead());
  123. $this->assertTrue($this->file->seek(0, SEEK_END));
  124. $this->assertEquals(0, $this->file->getBytesRead());
  125. $this->assertTrue($this->file->seek(2, SEEK_END));
  126. $this->assertEquals(2, $this->file->getBytesRead());
  127. }
  128. /**
  129. * test seeking to offset
  130. *
  131. * @test
  132. */
  133. public function seekRead()
  134. {
  135. $this->file->setContent('foobarbaz');
  136. $this->assertFalse($this->file->seek(0, 55));
  137. $this->assertTrue($this->file->seek(0, SEEK_SET));
  138. $this->assertEquals('foobarbaz', $this->file->readUntilEnd());
  139. $this->assertEquals(0, $this->file->getBytesRead());
  140. $this->assertTrue($this->file->seek(5, SEEK_SET));
  141. $this->assertEquals('rbaz', $this->file->readUntilEnd());
  142. $this->assertEquals(5, $this->file->getBytesRead());
  143. $this->assertTrue($this->file->seek(0, SEEK_CUR));
  144. $this->assertEquals('rbaz', $this->file->readUntilEnd());
  145. $this->assertEquals(5, $this->file->getBytesRead(), 5);
  146. $this->assertTrue($this->file->seek(2, SEEK_CUR));
  147. $this->assertEquals('az', $this->file->readUntilEnd());
  148. $this->assertEquals(7, $this->file->getBytesRead());
  149. $this->assertTrue($this->file->seek(0, SEEK_END));
  150. $this->assertEquals('', $this->file->readUntilEnd());
  151. $this->assertEquals(9, $this->file->getBytesRead());
  152. $this->assertTrue($this->file->seek(2, SEEK_END));
  153. $this->assertEquals('', $this->file->readUntilEnd());
  154. $this->assertEquals(11, $this->file->getBytesRead());
  155. }
  156. /**
  157. * test writing data into the file
  158. *
  159. * @test
  160. */
  161. public function writeEmptyFile()
  162. {
  163. $this->assertEquals(3, $this->file->write('foo'));
  164. $this->assertEquals('foo', $this->file->getContent());
  165. $this->assertEquals(3, $this->file->size());
  166. $this->assertEquals(3, $this->file->write('bar'));
  167. $this->assertEquals('foobar', $this->file->getContent());
  168. $this->assertEquals(6, $this->file->size());
  169. }
  170. /**
  171. * test writing data into the file
  172. *
  173. * @test
  174. */
  175. public function write()
  176. {
  177. $this->file->setContent('foobarbaz');
  178. $this->assertTrue($this->file->seek(3, SEEK_SET));
  179. $this->assertEquals(3, $this->file->write('foo'));
  180. $this->assertEquals('foofoobaz', $this->file->getContent());
  181. $this->assertEquals(9, $this->file->size());
  182. $this->assertEquals(3, $this->file->write('bar'));
  183. $this->assertEquals('foofoobar', $this->file->getContent());
  184. $this->assertEquals(9, $this->file->size());
  185. }
  186. /**
  187. * setting and retrieving permissions for a file
  188. *
  189. * @test
  190. * @group permissions
  191. */
  192. public function permissions()
  193. {
  194. $this->assertEquals(0666, $this->file->getPermissions());
  195. $this->assertSame($this->file, $this->file->chmod(0644));
  196. $this->assertEquals(0644, $this->file->getPermissions());
  197. }
  198. /**
  199. * setting and retrieving permissions for a file
  200. *
  201. * @test
  202. * @group permissions
  203. */
  204. public function permissionsSet()
  205. {
  206. $this->file = new vfsStreamFile('foo', 0644);
  207. $this->assertEquals(0644, $this->file->getPermissions());
  208. $this->assertSame($this->file, $this->file->chmod(0600));
  209. $this->assertEquals(0600, $this->file->getPermissions());
  210. }
  211. /**
  212. * setting and retrieving owner of a file
  213. *
  214. * @test
  215. * @group permissions
  216. */
  217. public function owner()
  218. {
  219. $this->assertEquals(vfsStream::getCurrentUser(), $this->file->getUser());
  220. $this->assertTrue($this->file->isOwnedByUser(vfsStream::getCurrentUser()));
  221. $this->assertSame($this->file, $this->file->chown(vfsStream::OWNER_USER_1));
  222. $this->assertEquals(vfsStream::OWNER_USER_1, $this->file->getUser());
  223. $this->assertTrue($this->file->isOwnedByUser(vfsStream::OWNER_USER_1));
  224. }
  225. /**
  226. * setting and retrieving owner group of a file
  227. *
  228. * @test
  229. * @group permissions
  230. */
  231. public function group()
  232. {
  233. $this->assertEquals(vfsStream::getCurrentGroup(), $this->file->getGroup());
  234. $this->assertTrue($this->file->isOwnedByGroup(vfsStream::getCurrentGroup()));
  235. $this->assertSame($this->file, $this->file->chgrp(vfsStream::GROUP_USER_1));
  236. $this->assertEquals(vfsStream::GROUP_USER_1, $this->file->getGroup());
  237. $this->assertTrue($this->file->isOwnedByGroup(vfsStream::GROUP_USER_1));
  238. }
  239. }
  240. ?>