AzureMultiContainerBlobStorageTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace Gaufrette\Functional\Adapter;
  3. use Gaufrette\Adapter\AzureBlobStorage;
  4. use Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactory;
  5. use Gaufrette\Filesystem;
  6. /**
  7. * Class AzureMultiContainerBlobStorageTest
  8. * @group AzureBlobStorage
  9. * @group AzureMultiContainerBlobStorage
  10. */
  11. class AzureMultiContainerBlobStorageTest extends FunctionalTestCase
  12. {
  13. private $adapter;
  14. private $containers = [];
  15. public function setUp()
  16. {
  17. $this->markTestSkipped(__CLASS__ . ' is flaky.');
  18. $account = getenv('AZURE_ACCOUNT');
  19. $key = getenv('AZURE_KEY');
  20. if (empty($account) || empty($key)) {
  21. $this->markTestSkipped('Either AZURE_ACCOUNT and/or AZURE_KEY env variables are not defined.');
  22. }
  23. $connection = sprintf('BlobEndpoint=http://%1$s.blob.core.windows.net/;AccountName=%1$s;AccountKey=%2$s', $account, $key);
  24. $this->adapter = new AzureBlobStorage(new BlobProxyFactory($connection));
  25. $this->filesystem = new Filesystem($this->adapter);
  26. }
  27. /**
  28. * @test
  29. * @group functional
  30. */
  31. public function shouldWriteAndRead()
  32. {
  33. $path1 = $this->createUniqueContainerName('container') . '/foo';
  34. $path2 = $this->createUniqueContainerName('test') . '/subdir/foo';
  35. $this->assertEquals(12, $this->filesystem->write($path1, 'Some content'));
  36. $this->assertEquals(13, $this->filesystem->write($path2, 'Some content1', true));
  37. $this->assertEquals('Some content', $this->filesystem->read($path1));
  38. $this->assertEquals('Some content1', $this->filesystem->read($path2));
  39. }
  40. /**
  41. * @test
  42. * @group functional
  43. */
  44. public function shouldUpdateFileContent()
  45. {
  46. $path = $this->createUniqueContainerName('container') . '/foo';
  47. $this->filesystem->write($path, 'Some content');
  48. $this->filesystem->write($path, 'Some content updated', true);
  49. $this->assertEquals('Some content updated', $this->filesystem->read($path));
  50. }
  51. /**
  52. * @test
  53. * @group functional
  54. */
  55. public function shouldCheckIfFileExists()
  56. {
  57. $path1 = $this->createUniqueContainerName('container') . '/foo';
  58. $path2 = $this->createUniqueContainerName('test') . '/somefile';
  59. $this->assertFalse($this->filesystem->has($path1));
  60. $this->filesystem->write($path1, 'Some content');
  61. $this->assertTrue($this->filesystem->has($path1));
  62. // @TODO: why is it done two times?
  63. $this->assertFalse($this->filesystem->has($path2));
  64. $this->assertFalse($this->filesystem->has($path2));
  65. }
  66. /**
  67. * @test
  68. * @group functional
  69. */
  70. public function shouldGetMtime()
  71. {
  72. $path = $this->createUniqueContainerName('container') . '/foo';
  73. $this->filesystem->write($path, 'Some content');
  74. $this->assertGreaterThan(0, $this->filesystem->mtime($path));
  75. }
  76. /**
  77. * @test
  78. * @group functional
  79. */
  80. public function shouldGetSize()
  81. {
  82. $path = $this->createUniqueContainerName('container') . '/foo';
  83. $contentSize = $this->filesystem->write($path, 'Some content');
  84. $this->assertEquals($contentSize, $this->filesystem->size($path));
  85. }
  86. /**
  87. * @test
  88. * @group functional
  89. */
  90. public function shouldGetMd5Hash()
  91. {
  92. $path = $this->createUniqueContainerName('container') . '/foo';
  93. $content = 'Some content';
  94. $this->filesystem->write($path, $content);
  95. $this->assertEquals(\md5($content), $this->filesystem->checksum($path));
  96. }
  97. /**
  98. * @test
  99. * @group functional
  100. * @expectedException \RuntimeException
  101. * @expectedMessage Could not get mtime for the "foo" key
  102. */
  103. public function shouldFailWhenTryMtimeForKeyWhichDoesNotExist()
  104. {
  105. $this->assertFalse($this->filesystem->mtime('container5/foo'));
  106. }
  107. /**
  108. * @test
  109. * @group functional
  110. */
  111. public function shouldRenameFile()
  112. {
  113. $somedir = $this->createUniqueContainerName('somedir');
  114. $path1 = $this->createUniqueContainerName('container') . '/foo';
  115. $path2 = $this->createUniqueContainerName('container-new') . '/boo';
  116. $path3 = $somedir . '/sub/boo';
  117. $this->filesystem->write($path1, 'Some content');
  118. $this->filesystem->rename($path1, $path2);
  119. $this->assertFalse($this->filesystem->has($path1));
  120. $this->assertEquals('Some content', $this->filesystem->read($path2));
  121. $this->filesystem->write($path1, 'Some content');
  122. $this->filesystem->rename($path1, $path3);
  123. $this->assertFalse($this->filesystem->has($somedir . '/sub/foo'));
  124. $this->assertEquals('Some content', $this->filesystem->read($path3));
  125. }
  126. /**
  127. * @test
  128. * @group functional
  129. */
  130. public function shouldDeleteFile()
  131. {
  132. $path = $this->createUniqueContainerName('container') . '/foo';
  133. $this->filesystem->write($path, 'Some content');
  134. $this->assertTrue($this->filesystem->has($path));
  135. $this->filesystem->delete($path);
  136. $this->assertFalse($this->filesystem->has($path));
  137. }
  138. /**
  139. * @test
  140. * @group functional
  141. */
  142. public function shouldFetchKeys()
  143. {
  144. $path1 = $this->createUniqueContainerName('container-1') . '/foo';
  145. $path2 = $this->createUniqueContainerName('container-2') . '/bar';
  146. $path3 = $this->createUniqueContainerName('container-3') . '/baz';
  147. $this->filesystem->write($path1, 'Some content');
  148. $this->filesystem->write($path2, 'Some content');
  149. $this->filesystem->write($path3, 'Some content');
  150. $actualKeys = $this->filesystem->keys();
  151. foreach ([$path1, $path2, $path3] as $key) {
  152. $this->assertContains($key, $actualKeys);
  153. }
  154. }
  155. /**
  156. * @test
  157. * @group functional
  158. */
  159. public function shouldWorkWithHiddenFiles()
  160. {
  161. $path = $this->createUniqueContainerName('container') . '/.foo';
  162. $this->filesystem->write($path, 'hidden');
  163. $this->assertTrue($this->filesystem->has($path));
  164. $this->assertContains($path, $this->filesystem->keys());
  165. $this->filesystem->delete($path);
  166. $this->assertFalse($this->filesystem->has($path));
  167. }
  168. /**
  169. * @test
  170. * @group functional
  171. */
  172. public function shouldKeepFileObjectInRegister()
  173. {
  174. $path = $this->createUniqueContainerName('container') . '/somefile';
  175. $FileObjectA = $this->filesystem->createFile($path);
  176. $FileObjectB = $this->filesystem->createFile($path);
  177. $this->assertSame($FileObjectB, $FileObjectA);
  178. }
  179. /**
  180. * @test
  181. * @group functional
  182. */
  183. public function shouldWriteToSameFile()
  184. {
  185. $path = $this->createUniqueContainerName('container') . '/somefile';
  186. $FileObjectA = $this->filesystem->createFile($path);
  187. $FileObjectA->setContent('ABC');
  188. $FileObjectB = $this->filesystem->createFile($path);
  189. $FileObjectB->setContent('DEF');
  190. $this->assertEquals('DEF', $FileObjectA->getContent());
  191. }
  192. private function createUniqueContainerName($prefix)
  193. {
  194. $this->containers[] = $container = uniqid($prefix);
  195. return $container;
  196. }
  197. public function tearDown()
  198. {
  199. foreach ($this->containers as $container) {
  200. $this->adapter->deleteContainer($container);
  201. }
  202. }
  203. }