OpenCloudSpec.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace spec\Gaufrette\Adapter;
  3. use Guzzle\Http\Exception\BadResponseException;
  4. use OpenCloud\Common\Collection;
  5. use OpenCloud\Common\Exceptions\CreateUpdateError;
  6. use OpenCloud\Common\Exceptions\DeleteError;
  7. use OpenCloud\ObjectStore\Exception\ObjectNotFoundException;
  8. use OpenCloud\ObjectStore\Resource\Container;
  9. use OpenCloud\ObjectStore\Resource\DataObject;
  10. use OpenCloud\ObjectStore\Service;
  11. use PhpSpec\ObjectBehavior;
  12. /**
  13. * OpenCloudSpec
  14. *
  15. * @author Chris Warner <cdw.lighting@gmail.com>
  16. * @author Daniel Richter <nexyz9@gmail.com>
  17. */
  18. class OpenCloudSpec extends ObjectBehavior
  19. {
  20. /**
  21. * @param OpenCloud\ObjectStore\Service $objectStore
  22. * @param OpenCloud\ObjectStore\Resource\Container $container
  23. */
  24. function let(Service $objectStore, Container $container)
  25. {
  26. $objectStore->getContainer("test")->willReturn($container);
  27. $this->beConstructedWith($objectStore, 'test', false);
  28. }
  29. function it_is_adapter()
  30. {
  31. $this->shouldHaveType('Gaufrette\Adapter');
  32. }
  33. /**
  34. * @param OpenCloud\ObjectStore\Resource\Container $container
  35. * @param OpenCloud\ObjectStore\Resource\DataObject $object
  36. */
  37. function it_reads_file(Container $container, DataObject $object)
  38. {
  39. $object->getContent()->willReturn("Hello World");
  40. $container->getObject("test")->willReturn($object);
  41. $this->read('test')->shouldReturn('Hello World');
  42. }
  43. /**
  44. * @param OpenCloud\ObjectStore\Resource\Container $container
  45. */
  46. function it_reads_file_on_error_returns_false(Container $container)
  47. {
  48. $container->getObject("test")->willThrow(new ObjectNotFoundException());
  49. $this->read('test')->shouldReturn(false);
  50. }
  51. /**
  52. * @param OpenCloud\ObjectStore\Resource\Container $container
  53. * @param OpenCloud\ObjectStore\Resource\DataObject $object
  54. */
  55. function it_writes_file_returns_size(Container $container, DataObject $object)
  56. {
  57. $testData = "Hello World!";
  58. $testDataSize = strlen($testData);
  59. $object->getContentLength()->willReturn($testDataSize);
  60. $container->uploadObject('test', $testData)->willReturn($object);
  61. $this->write('test', $testData)->shouldReturn($testDataSize);
  62. }
  63. /**
  64. * @param OpenCloud\ObjectStore\Resource\Container $container
  65. */
  66. function it_writes_file_and_write_fails_returns_false(Container $container)
  67. {
  68. $testData = "Hello World!";
  69. $container->uploadObject('test', $testData)->willThrow(new CreateUpdateError());
  70. $this->write('test', $testData)->shouldReturn(false);
  71. }
  72. /**
  73. * @param OpenCloud\ObjectStore\Resource\Container $container
  74. * @param OpenCloud\ObjectStore\Resource\DataObject $object
  75. */
  76. function it_returns_true_if_key_exists(Container $container, DataObject $object)
  77. {
  78. $container->getPartialObject('test')->willReturn($object);
  79. $this->exists('test')->shouldReturn(true);
  80. }
  81. /**
  82. * @param OpenCloud\ObjectStore\Resource\Container $container
  83. */
  84. function it_returns_false_if_key_does_not_exist(Container $container)
  85. {
  86. $container->getPartialObject('test')->willThrow(new BadResponseException());
  87. $this->exists('test')->shouldReturn(false);
  88. }
  89. /**
  90. * @param OpenCloud\ObjectStore\Resource\Container $container
  91. * @param OpenCloud\ObjectStore\Resource\DataObject $object
  92. */
  93. function it_deletes_file_on_success_returns_true(Container $container, DataObject $object)
  94. {
  95. $object->delete()->willReturn(null);
  96. $container->getObject("test")->willReturn($object);
  97. $this->delete('test')->shouldReturn(true);
  98. }
  99. /**
  100. * @param OpenCloud\ObjectStore\Resource\Container $container
  101. * @param OpenCloud\ObjectStore\Resource\DataObject $object
  102. */
  103. function it_deletes_file_returns_false_on_failure(Container $container, DataObject $object)
  104. {
  105. $object->delete()->willThrow(new DeleteError());
  106. $container->getObject("test")->willReturn($object);
  107. $this->delete('test')->shouldReturn(false);
  108. }
  109. /**
  110. * @param OpenCloud\ObjectStore\Resource\Container $container
  111. */
  112. function it_deletes_file_if_file_does_not_exist_returns_false(Container $container)
  113. {
  114. $container->getObject("test")->willThrow(new ObjectNotFoundException());
  115. $this->delete('test')->shouldReturn(false);
  116. }
  117. /**
  118. * @param OpenCloud\ObjectStore\Resource\Container $container
  119. * @param OpenCloud\ObjectStore\Resource\DataObject $object
  120. */
  121. function it_returns_checksum_if_file_exists(Container $container, DataObject $object)
  122. {
  123. $object->getEtag()->willReturn("test String");
  124. $container->getObject("test")->willReturn($object);
  125. $this->checksum('test')->shouldReturn("test String");
  126. }
  127. /**
  128. * @param OpenCloud\ObjectStore\Resource\Container $container
  129. */
  130. function it_returns_false_when_file_does_not_exist(Container $container)
  131. {
  132. $container->getObject("test")->willThrow(new ObjectNotFoundException());
  133. $this->checksum('test')->shouldReturn(false);
  134. }
  135. /**
  136. * @param OpenCloud\ObjectStore\Resource\Container $container
  137. * @param OpenCloud\Common\Collection $objectList
  138. * @param OpenCloud\ObjectStore\Resource\DataObject $object1
  139. * @param OpenCloud\ObjectStore\Resource\DataObject $object2
  140. * @param OpenCloud\ObjectStore\Resource\DataObject $object3
  141. */
  142. function it_returns_files_as_sorted_array(Container $container, Collection $objectList, DataObject $object1, DataObject $object2, DataObject $object3)
  143. {
  144. $outputArray = array('key1', 'key2', 'key5');
  145. $index = 0;
  146. $object1->getName()->willReturn('key5');
  147. $object2->getName()->willReturn('key2');
  148. $object3->getName()->willReturn('key1');
  149. $objects = array($object1, $object2, $object3);
  150. $objectList->next()->will(
  151. function () use ($objects, &$index) {
  152. if ($index < count($objects)) {
  153. $index++;
  154. return $objects[$index - 1];
  155. }
  156. }
  157. ) ->shouldBeCalledTimes(count($objects) + 1);
  158. $container->objectList()->willReturn($objectList);
  159. $this->keys()->shouldReturn($outputArray);
  160. }
  161. /**
  162. * @param OpenCloud\ObjectStore\Service $objectStore
  163. */
  164. function it_throws_exception_if_container_does_not_exist(Service $objectStore)
  165. {
  166. $containerName = 'container-does-not-exist';
  167. $objectStore->getContainer($containerName)->willThrow(new BadResponseException());
  168. $this->beConstructedWith($objectStore, $containerName);
  169. $this->shouldThrow('\RuntimeException')->duringExists('test');
  170. }
  171. /**
  172. * @param OpenCloud\ObjectStore\Service $objectStore
  173. * @param OpenCloud\ObjectStore\Resource\Container $container
  174. */
  175. function it_creates_container(Service $objectStore, Container $container)
  176. {
  177. $containerName = 'container-does-not-yet-exist';
  178. $filename = 'test';
  179. $objectStore->getContainer($containerName)->willThrow(new BadResponseException());
  180. $objectStore->createContainer($containerName)->willReturn($container);
  181. $container->getPartialObject($filename)->willThrow(new BadResponseException());
  182. $this->beConstructedWith($objectStore, $containerName, true);
  183. $this->exists($filename)->shouldReturn(false);
  184. }
  185. /**
  186. * @param OpenCloud\ObjectStore\Service $objectStore
  187. */
  188. function it_throws_exeption_if_container_creation_fails(Service $objectStore)
  189. {
  190. $containerName = 'container-does-not-yet-exist';
  191. $objectStore->getContainer($containerName)->willThrow(new BadResponseException());
  192. $objectStore->createContainer($containerName)->willReturn(false);
  193. $this->beConstructedWith($objectStore, $containerName, true);
  194. $this->shouldThrow('\RuntimeException')->duringExists('test');
  195. }
  196. /**
  197. * @param OpenCloud\ObjectStore\Resource\Container $container
  198. */
  199. function it_returns_false_if_the_object_does_not_exists_when_fetching_mtime(Container $container)
  200. {
  201. $container->getObject('foo')->willThrow(ObjectNotFoundException::class);
  202. $this->mtime('foo')->shouldReturn(false);
  203. }
  204. /**
  205. * @param OpenCloud\ObjectStore\Resource\DataObject $object
  206. * @param OpenCloud\ObjectStore\Resource\Container $container
  207. */
  208. function it_fetches_file_mtime(DataObject $object, Container $container)
  209. {
  210. $container->getObject('foo')->willReturn($object);
  211. $object->getLastModified()->willReturn('Tue, 13 Jun 2017 22:02:34 GMT');
  212. $this->mtime('foo')->shouldReturn('1497391354');
  213. }
  214. }