FunctionalTestCase.php 5.6 KB

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