OpenCloudSpec.php 7.5 KB

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