123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- <?php
- namespace spec\Gaufrette\Adapter;
- use PHPSpec2\ObjectBehavior;
- use WindowsAzure\Blob\Models\Blob;
- use WindowsAzure\Common\ServiceException;
- class AzureBlobStorage extends ObjectBehavior
- {
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- */
- public function let($blobProxyFactory)
- {
- $this->beConstructedWith($blobProxyFactory, 'containerName');
- }
- public function it_should_be_initializable()
- {
- $this->shouldHaveType('Gaufrette\Adapter\AzureBlobStorage');
- $this->shouldHaveType('Gaufrette\Adapter');
- $this->shouldHaveType('Gaufrette\Adapter\MetadataSupporter');
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- * @param \WindowsAzure\Blob\Models\GetBlobResult $getBlobResult
- */
- public function it_should_read_file($blobProxyFactory, $blobProxy, $getBlobResult)
- {
- $getBlobResult
- ->getContentStream()
- ->shouldBeCalled()
- //azure blob content is handled as stream so we need to fake it
- ->willReturn(fopen('data://text/plain,some content','r'));
- $blobProxy
- ->getBlob('containerName', 'filename')
- ->shouldBeCalled()
- ->willReturn($getBlobResult);
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $this->read('filename')->shouldReturn('some content');
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_return_false_when_cannot_read($blobProxyFactory, $blobProxy)
- {
- $blobProxy
- ->getBlob('containerName', 'filename')
- ->shouldBeCalled()
- ->willThrow(new ServiceException(500));
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $this->read('filename')->shouldReturn(false);
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_not_mask_exception_when_read($blobProxyFactory, $blobProxy)
- {
- $blobProxy
- ->getBlob('containerName', 'filename')
- ->shouldBeCalled()
- ->willThrow(new \RuntimeException('read'));
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $this->shouldThrow(new \RuntimeException('read'))->duringRead('filename');
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_rename_file($blobProxyFactory, $blobProxy)
- {
- $blobProxy
- ->copyBlob('containerName', 'filename2', 'containerName', 'filename1')
- ->shouldBeCalled();
- $blobProxy
- ->deleteBlob('containerName', 'filename1')
- ->shouldBeCalled();
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $this->rename('filename1', 'filename2')->shouldReturn(true);
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_return_false_when_cannot_rename($blobProxyFactory, $blobProxy)
- {
- $blobProxy
- ->copyBlob('containerName', 'filename2', 'containerName', 'filename1')
- ->shouldBeCalled()
- ->willThrow(new ServiceException(500));
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $this->rename('filename1', 'filename2')->shouldReturn(false);
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_not_mask_exception_when_rename($blobProxyFactory, $blobProxy)
- {
- $blobProxy
- ->copyBlob('containerName', 'filename2', 'containerName', 'filename1')
- ->shouldBeCalled()
- ->willThrow(new \RuntimeException('rename'));
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $this->shouldThrow(new \RuntimeException('rename'))->duringRename('filename1', 'filename2');
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_write_file($blobProxyFactory, $blobProxy)
- {
- $blobProxy
- ->createBlockBlob('containerName', 'filename', 'some content',
- \Mockery::type('\WindowsAzure\Blob\Models\CreateBlobOptions'))
- ->shouldBeCalled();
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $this->write('filename', 'some content')->shouldReturn(12);
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_return_false_when_cannot_write($blobProxyFactory, $blobProxy)
- {
- $blobProxy
- ->createBlockBlob('containerName', 'filename', 'some content',
- \Mockery::type('\WindowsAzure\Blob\Models\CreateBlobOptions'))
- ->willThrow(new ServiceException(500));
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $this->write('filename', 'some content')->shouldReturn(false);
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_not_mask_exception_when_write($blobProxyFactory, $blobProxy)
- {
- $blobProxy
- ->createBlockBlob('containerName', 'filename', 'some content',
- \Mockery::type('\WindowsAzure\Blob\Models\CreateBlobOptions'))
- ->willThrow(new \RuntimeException('write'));
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $this->shouldThrow(new \RuntimeException('write'))->duringWrite('filename', 'some content');
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- * @param \WindowsAzure\Blob\Models\GetBlobResult $getBlobResult
- */
- public function it_should_check_if_file_exists($blobProxyFactory, $blobProxy, $getBlobResult)
- {
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $blobProxy
- ->getBlob('containerName', 'filename')
- ->shouldBeCalled()
- ->willThrow(new ServiceException(404));
- $this->exists('filename')->shouldReturn(false);
- $blobProxy
- ->getBlob('containerName', 'filename2')
- ->shouldBeCalled()
- ->willReturn($getBlobResult);
- $this->exists('filename2')->shouldReturn(true);
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_not_mask_exception_when_check_if_file_exists($blobProxyFactory, $blobProxy)
- {
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $blobProxy
- ->getBlob('containerName', 'filename')
- ->shouldBeCalled()
- ->willThrow(new \RuntimeException('exists'));
- $this->shouldThrow(new \RuntimeException('exists'))->duringExists('filename');
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- * @param \WindowsAzure\Blob\Models\GetBlobPropertiesResult $getBlobPropertiesResult
- * @param \WindowsAzure\Blob\Models\BlobProperties $blobProperties
- */
- public function it_should_get_file_mtime($blobProxyFactory, $blobProxy, $getBlobPropertiesResult, $blobProperties)
- {
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $blobProxy
- ->getBlobProperties('containerName', 'filename')
- ->shouldBeCalled()
- ->willReturn($getBlobPropertiesResult);
- $getBlobPropertiesResult
- ->getProperties()
- ->shouldBeCalled()
- ->willReturn($blobProperties);
- $blobProperties
- ->getLastModified()
- ->shouldBeCalled()
- ->willReturn(new \DateTime('1987-12-28 20:00:00'));
- $this->mtime('filename')->shouldReturn(strtotime('1987-12-28 20:00:00'));
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_return_false_when_cannot_mtime($blobProxyFactory, $blobProxy)
- {
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $blobProxy
- ->getBlobProperties('containerName', 'filename')
- ->shouldBeCalled()
- ->willThrow(new ServiceException(500));
- $this->mtime('filename')->shouldReturn(false);
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_not_mask_exception_when_get_mtime($blobProxyFactory, $blobProxy)
- {
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $blobProxy
- ->getBlobProperties('containerName', 'filename')
- ->shouldBeCalled()
- ->willThrow(new \RuntimeException('mtime'));
- $this->shouldThrow(new \RuntimeException('mtime'))->duringMtime('filename');
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_delete_file($blobProxyFactory, $blobProxy)
- {
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $blobProxy
- ->deleteBlob('containerName', 'filename')
- ->shouldBeCalled();
- $this->delete('filename')->shouldReturn(true);
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_return_false_when_cannot_delete_file($blobProxyFactory, $blobProxy)
- {
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $blobProxy
- ->deleteBlob('containerName', 'filename')
- ->shouldBeCalled()
- ->willThrow(new ServiceException(500));
- $this->delete('filename')->shouldReturn(false);
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_not_mask_exception_when_delete($blobProxyFactory, $blobProxy)
- {
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $blobProxy
- ->deleteBlob('containerName', 'filename')
- ->shouldBeCalled()
- ->willThrow(new \RuntimeException('delete'));
- $this->shouldThrow(new \RuntimeException('delete'))->duringDelete('filename');
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- * @param \WindowsAzure\Blob\Models\ListBlobsResult $listBlobResult
- */
- public function it_should_get_keys($blobProxyFactory, $blobProxy, $listBlobResult)
- {
- $fileNames = array('aaa', 'aaa/filename', 'filename1', 'filename2');
- $blobs = array();
- foreach ($fileNames as $fileName) {
- $blob = new Blob();
- $blob->setName($fileName);
- $blobs[] = $blob;
- }
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $blobProxy
- ->listBlobs('containerName')
- ->shouldBeCalled()
- ->willReturn($listBlobResult);
- $listBlobResult
- ->getBlobs()
- ->shouldBeCalled()
- ->willReturn($blobs);
- $this->keys()->shouldReturn(array('aaa', 'aaa/filename', 'filename1', 'filename2'));
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_not_mask_exception_when_get_keys($blobProxyFactory, $blobProxy)
- {
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $blobProxy
- ->listBlobs('containerName')
- ->shouldBeCalled()
- ->willThrow(new \RuntimeException('keys'));
- $this->shouldThrow(new \RuntimeException('keys'))->duringKeys();
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_handle_dirs($blobProxyFactory, $blobProxy)
- {
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $blobProxy
- ->getBlob('containerName', 'filename')
- ->shouldNotBeCalled();
- $blobProxy
- ->getBlob('containerName', 'filename/')
- ->shouldBeCalled()
- ->willThrow(new ServiceException(404));
- $blobProxy
- ->getBlob('containerName', 'dirname/')
- ->shouldBeCalled();
- $this->isDirectory('filename')->shouldReturn(false);
- $this->isDirectory('dirname')->shouldReturn(true);
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_create_container($blobProxyFactory, $blobProxy)
- {
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $blobProxy
- ->createContainer('containerName', null)
- ->shouldBeCalled();
- $this->createContainer('containerName');
- }
- /**
- * @param \Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
- * @param \WindowsAzure\Blob\Internal\IBlob $blobProxy
- */
- public function it_should_fail_when_cannot_create_container($blobProxyFactory, $blobProxy)
- {
- $blobProxyFactory
- ->create()
- ->shouldBeCalled()
- ->willReturn($blobProxy);
- $blobProxy
- ->createContainer('containerName', null)
- ->shouldBeCalled()
- ->willThrow(new ServiceException(500));
- $this->shouldThrow(new \RuntimeException('Failed to create the configured container "containerName": 0 ().', null))->duringCreateContainer('containerName');
- }
- }
|