FunctionalTestCase.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace Gaufrette\Functional\Adapter;
  3. use Gaufrette\Filesystem;
  4. abstract class FunctionalTestCase extends \PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * @var Filesystem
  8. */
  9. protected $filesystem;
  10. public function getAdapterName()
  11. {
  12. if (!preg_match('/\\\\(\w+)Test$/', get_class($this), $matches)) {
  13. throw new \RuntimeException(sprintf(
  14. 'Unable to guess filesystem name from class "%s", '.
  15. 'please override the ->getAdapterName() method.',
  16. get_class($this)
  17. ));
  18. }
  19. return $matches[1];
  20. }
  21. public function setUp()
  22. {
  23. $basename = $this->getAdapterName();
  24. $filename = sprintf(
  25. '%s/adapters/%s.php',
  26. dirname(__DIR__),
  27. $basename
  28. );
  29. if (!file_exists($filename)) {
  30. return $this->markTestSkipped(<<<EOF
  31. To run the {$basename} filesystem tests, you must:
  32. 1. Copy the file "{$filename}.dist" as "{$filename}"
  33. 2. Modify the copied file to fit your environment
  34. EOF
  35. );
  36. }
  37. $adapter = include $filename;
  38. $this->filesystem = new Filesystem($adapter);
  39. }
  40. public function tearDown()
  41. {
  42. if (null === $this->filesystem) {
  43. return;
  44. }
  45. $this->filesystem = null;
  46. }
  47. /**
  48. * @test
  49. * @group functional
  50. */
  51. public function shouldWriteAndRead()
  52. {
  53. $this->assertEquals(12, $this->filesystem->write('foo', 'Some content'));
  54. $this->assertEquals(13, $this->filesystem->write('test/subdir/foo', 'Some content1', true));
  55. $this->assertEquals('Some content', $this->filesystem->read('foo'));
  56. $this->assertEquals('Some content1', $this->filesystem->read('test/subdir/foo'));
  57. $this->filesystem->delete('foo');
  58. $this->filesystem->delete('test/subdir/foo');
  59. }
  60. /**
  61. * @test
  62. * @group functional
  63. */
  64. public function shouldUpdateFileContent()
  65. {
  66. $this->filesystem->write('foo', 'Some content');
  67. $this->filesystem->write('foo', 'Some content updated', true);
  68. $this->assertEquals('Some content updated', $this->filesystem->read('foo'));
  69. $this->filesystem->delete('foo');
  70. }
  71. /**
  72. * @test
  73. * @group functional
  74. */
  75. public function shouldCheckIfFileExists()
  76. {
  77. $this->assertFalse($this->filesystem->has('foo'));
  78. $this->filesystem->write('foo', 'Some content');
  79. $this->assertTrue($this->filesystem->has('foo'));
  80. $this->assertFalse($this->filesystem->has('test/somefile'));
  81. $this->assertFalse($this->filesystem->has('test/somefile'));
  82. $this->filesystem->delete('foo');
  83. }
  84. /**
  85. * @test
  86. * @group functional
  87. */
  88. public function shouldGetMtime()
  89. {
  90. $this->filesystem->write('foo', 'Some content');
  91. $this->assertGreaterThan(0, $this->filesystem->mtime('foo'));
  92. $this->filesystem->delete('foo');
  93. }
  94. /**
  95. * @test
  96. * @group functional
  97. * @expectedException \RuntimeException
  98. * @expectedMessage Could not get mtime for the "foo" key
  99. */
  100. public function shouldFailWhenTryMtimeForKeyWhichDoesNotExist()
  101. {
  102. $this->assertFalse($this->filesystem->mtime('foo'));
  103. }
  104. /**
  105. * @test
  106. * @group functional
  107. */
  108. public function shouldRenameFile()
  109. {
  110. $this->filesystem->write('foo', 'Some content');
  111. $this->filesystem->rename('foo', 'boo');
  112. $this->assertFalse($this->filesystem->has('foo'));
  113. $this->assertEquals('Some content', $this->filesystem->read('boo'));
  114. $this->filesystem->delete('boo');
  115. $this->filesystem->write('foo', 'Some content');
  116. $this->filesystem->rename('foo', 'somedir/sub/boo');
  117. $this->assertFalse($this->filesystem->has('somedir/sub/foo'));
  118. $this->assertEquals('Some content', $this->filesystem->read('somedir/sub/boo'));
  119. $this->filesystem->delete('somedir/sub/boo');
  120. }
  121. /**
  122. * @test
  123. * @group functional
  124. */
  125. public function shouldDeleteFile()
  126. {
  127. $this->filesystem->write('foo', 'Some content');
  128. $this->assertTrue($this->filesystem->has('foo'));
  129. $this->filesystem->delete('foo');
  130. $this->assertFalse($this->filesystem->has('foo'));
  131. }
  132. /**
  133. * @test
  134. * @group functional
  135. */
  136. public function shouldFetchKeys()
  137. {
  138. $this->assertEquals(array(), $this->filesystem->keys());
  139. $this->filesystem->write('foo', 'Some content');
  140. $this->filesystem->write('bar', 'Some content');
  141. $this->filesystem->write('baz', 'Some content');
  142. $actualKeys = $this->filesystem->keys();
  143. $this->assertEquals(3, count($actualKeys));
  144. foreach (array('foo', 'bar', 'baz') as $key) {
  145. $this->assertContains($key, $actualKeys);
  146. }
  147. $this->filesystem->delete('foo');
  148. $this->filesystem->delete('bar');
  149. $this->filesystem->delete('baz');
  150. }
  151. /**
  152. * @test
  153. * @group functional
  154. */
  155. public function shouldWorkWithHiddenFiles()
  156. {
  157. $this->filesystem->write('.foo', 'hidden');
  158. $this->assertTrue($this->filesystem->has('.foo'));
  159. $this->assertContains('.foo', $this->filesystem->keys());
  160. $this->filesystem->delete('.foo');
  161. $this->assertFalse($this->filesystem->has('.foo'));
  162. }
  163. /**
  164. * @test
  165. * @group functional
  166. */
  167. public function shouldKeepFileObjectInRegister()
  168. {
  169. $FileObjectA = $this->filesystem->createFile('somefile');
  170. $FileObjectB = $this->filesystem->createFile('somefile');
  171. $this->assertTrue($FileObjectA === $FileObjectB);
  172. }
  173. /**
  174. * @test
  175. * @group functional
  176. */
  177. public function shouldWrtieToSameFile()
  178. {
  179. $FileObjectA = $this->filesystem->createFile('somefile');
  180. $FileObjectA->setContent('ABC');
  181. $FileObjectB = $this->filesystem->createFile('somefile');
  182. $FileObjectB->setContent('DEF');
  183. $this->assertEquals('DEF', $FileObjectB->getContent());
  184. $this->filesystem->delete('somefile');
  185. }
  186. }