/php/sdk/third_party/vfsstream/vendor/mikey179/vfsStream/src/test/php/org/bovigo/vfs/vfsStreamFileTestCase.php

https://github.com/theosp/google_appengine · PHP · 278 lines · 164 code · 16 blank · 98 comment · 0 complexity · 91ef4bac3a0fcc531c0819481e0433b9 MD5 · raw file

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