Permalink
Browse files

Normalize the Dropbox adapter

  • Loading branch information...
1 parent dfd7247 commit a4caf2b4b723a4a4328ff0d247ff4f6cbb197d26 @Herzult Herzult committed May 29, 2012
Showing with 134 additions and 210 deletions.
  1. +71 −32 src/Gaufrette/Adapter/Dropbox.php
  2. +1 −178 tests/Gaufrette/Adapter/DropboxTest.php
  3. +59 −0 tests/adapters/Dropbox.php.dist
  4. +3 −0 tests/bootstrap.php.dist
@@ -2,35 +2,39 @@
namespace Gaufrette\Adapter;
+use Gaufrette\Util;
+use Gaufrette\Exception;
+
/**
* Dropbox adapter
*
- * @package Gaufrette
* @author Markus Bachmann <markus.bachmann@bachi.biz>
+ * @author Antoine Hérault <antoine.herault@gmail.com>
*/
class Dropbox extends Base
{
- /**
- * @var \Dropbox_API
- */
- protected $dropbox;
+ protected $client;
/**
* Constructor
*
- * @param \Dropbox_API $dropbox
+ * @param \Dropbox_API $client The Dropbox API client
*/
- public function __construct(\Dropbox_API $dropbox)
+ public function __construct(\Dropbox_API $client)
{
- $this->dropbox = $dropbox;
+ $this->client = $client;
}
/**
* {@inheritDoc}
*/
public function read($key)
{
- return $this->dropbox->getFile($key);
+ try {
+ return $this->client->getFile($key);
+ } catch (\Dropbox_Exception_NotFound $e) {
+ throw new Exception\FileNotFound($key, 0, $e);
+ }
}
/**
@@ -42,76 +46,111 @@ public function write($key, $content, array $metadata = null)
fwrite($resource, $content);
fseek($resource, 0);
- if (!$this->dropbox->putFile($key, $resource)) {
+ try {
+ $this->client->putFile($key, $resource);
+ } catch (\Exception $e) {
fclose($resource);
- throw new \RuntimeException(sprintf('Unable to write file %s', $key));
+ throw $e;
}
fclose($resource);
- return strlen($content);
+ return Util\Size::fromContent($content);
}
/**
* {@inheritDoc}
*/
public function delete($key)
{
- $this->dropbox->delete($key);
+ try {
+ $this->client->delete($key);
+ } catch (\Dropbox_Exception_NotFound $e) {
+ throw new Exception\FileNotFound($key, 0, $e);
+ }
}
/**
* {@inheritDoc}
*/
- public function rename($key, $new)
+ public function rename($sourceKey, $targetKey)
{
- $this->dropbox->move($key, $new);
+ try {
+ $this->client->move($sourceKey, $targetKey);
+ } catch (\Dropbox_Exception_NotFound $e) {
+ throw new Exception\FileNotFound($sourceKey, 0, $e);
+ } catch (\Dropbox_Exception_Forbidden $e) {
+ // TODO find a better way to be sure it's because the target file
+ // exists
+ if ($this->exists($targetKey)) {
+ throw new Exception\UnexpectedFile($targetKey);
+ }
+
+ throw $e;
+ }
}
/**
* {@inheritDoc}
*/
public function checksum($key)
{
- return md5($this->dropbox->getFile($key));
+ return Util\Checksum::fromContent($this->read($key));
}
/**
* {@inheritDoc}
*/
public function mtime($key)
{
- $info = $this->dropbox->getMetaData($key);
- return strtotime($info['modified']);
+ $metadata = $this->getMetadata($key);
+
+ return strtotime($metadata['modified']);
}
/**
- * Returns an array of all keys matching the specified pattern
- *
- * @return array
+ * {@inheritDoc}
*/
public function keys()
{
- $metadata = $this->dropbox->getMetaData('/', true);
+ $metadata = $this->client->getMetaData('/', true);
$files = isset($metadata['contents']) ? $metadata['contents'] : array();
- return array_map(function($value) {
- return ltrim($value['path'], '/');
- }, $files);
+ return array_map(
+ function($value) {
+ return ltrim($value['path'], '/');
+ },
+ $files
+ );
}
/**
- * Indicates whether the file exists
- *
- * @param string $key
- *
- * @return boolean
+ * {@inheritDoc}
*/
public function exists($key)
{
- $results = $this->dropbox->search($key);
- return !empty($key);
+ try {
+ $this->getMetadata($key);
+
+ return true;
+ } catch (Exception\FileNotFound $e) {
+ return false;
+ }
}
+ private function getMetadata($key)
+ {
+ try {
+ $metadata = $this->client->getMetaData($key, false);
+ } catch (\Dropbox_Exception_NotFound $e) {
+ throw new Exception\FileNotFound($key, 0, $e);
+ }
+
+ // TODO find a way to exclude deleted files
+ if (isset($metadata['is_deleted']) && $metadata['is_deleted']) {
+ throw new Exception\FileNotFound($key);
+ }
+ return $metadata;
+ }
}
@@ -2,183 +2,6 @@
namespace Gaufrette\Adapter;
-/**
- * Dropbox testcase
- *
- * @package Gaufrette
- * @author Markus Bachmann <markus.bachmann@bachi.biz>
- */
-class DropboxTest extends \PHPUnit_Framework_TestCase
+class DropboxTest extends FunctionalTestCase
{
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $dropbox;
-
- /**
- * @var Dropbox
- */
- protected $adapter;
-
- protected function setup()
- {
- $this->dropbox = $this->getMockBuilder('Dropbox_API')->disableOriginalConstructor()->getMock();
- $this->adapter = new Dropbox($this->dropbox);
- }
-
- public function testWriteFile()
- {
- $content = str_repeat('foobar', 100);
-
- $this->dropbox->expects($this->once())
- ->method('putFile')
- ->will($this->returnValue(true));
-
- $this->assertEquals(strlen($content), $this->adapter->write('foobar', $content));
- }
-
- public function testReadFile()
- {
- $this->dropbox->expects($this->once())
- ->method('getFile')
- ->with($this->equalTo('foo'))
- ->will($this->returnValue('foobar'));
-
- $this->assertEquals('foobar', $this->adapter->read('foo'));
- }
-
- public function testDeleteFile()
- {
- $this->dropbox->expects($this->once())
- ->method('delete')
- ->will($this->returnValue(array(
- "size" => "0 bytes",
- "is_deleted" => true,
- "bytes" => 0,
- "thumb_exists" => false,
- "rev" => "1f33043551f",
- "modified" => "Wed, 10 Aug 2011 18:21:30 +0000",
- "path" => "/test.txt",
- "is_dir" => false,
- "icon" => "page_white_text",
- "root" => "dropbox",
- "mime_type" => "text/plain",
- "revision" => 492341
- )));
-
- $this->assertNull($this->adapter->delete('test.txt'));
- }
-
- public function testExists()
- {
- $this->dropbox->expects($this->once())
- ->method('search')
- ->will($this->returnValue(array(
- array(
- "size" => "0 bytes",
- "rev" => "35c1f029684fe",
- "thumb_exists" => false,
- "bytes" => 0,
- "modified" => "Mon, 18 Jul 2011 20:13:43 +0000",
- "path" => "/Public/latest.txt",
- "is_dir" => false,
- "icon" => "page_white_text",
- "root" => "dropbox",
- "mime_type" => "text/plain",
- "revision" => 220191
- )
- )
- ));
-
- $this->assertEquals(true, $this->adapter->exists('foo'));
- }
-
- public function testMtime()
- {
- $mtime = strtotime('Tue, 19 Jul 2011 21:55:38 +0000');
- $this->dropbox->expects($this->once())
- ->method('getMetadata')
- ->will($this->returnValue(array(
- "size" => "225.4KB",
- "rev" => "35e97029684fe",
- "thumb_exists" => false,
- "bytes" => 230783,
- "modified" => "Tue, 19 Jul 2011 21:55:38 +0000",
- "client_mtime" => "Mon, 18 Jul 2011 18:04:35 +0000",
- "path" => "/Getting_Started.pdf",
- "is_dir" => false,
- "icon" => "page_white_acrobat",
- "root" => "dropbox",
- "mime_type" => "application/pdf",
- "revision" => 220823
- )));
-
- $this->assertEquals($mtime, $this->adapter->mtime('foobar'));
- }
-
- public function testChecksum()
- {
- $checksum = md5('foobar');
- $this->dropbox->expects($this->once())
- ->method('getFile')
- ->will($this->returnValue('foobar'));
-
- $this->assertEquals($checksum, $this->adapter->checksum('foobar'));
- }
-
- public function testKeys()
- {
- $data = array(
- "size" => "0 bytes",
- "hash" => "37eb1ba1849d4b0fb0b28caf7ef3af52",
- "bytes" => 0,
- "thumb_exists" => false,
- "rev" => "714f029684fe",
- "modified" => "Wed, 27 Apr 2011 22:18:51 +0000",
- "path" => "/Public",
- "is_dir" => true,
- "icon" => "folder_public",
- "root" => "dropbox",
- "revision" => 29007,
- "contents" => array(
- array(
- "size" => "0 bytes",
- "rev" => "35c1f029684fe",
- "thumb_exists" => false,
- "bytes" => 0,
- "modified" => "Mon, 18 Jul 2011 20:13:43 +0000",
- "client_mtime" => "Wed, 20 Apr 2011 16:20:19 +0000",
- "path" => "/Public/newest",
- "is_dir" => true,
- "icon" => "page_white_text",
- "root" => "dropbox",
- "mime_type" => "text/plain",
- "revision" => 220191,
- ),
- array(
- "size" => "0 bytes",
- "rev" => "35c1f029684fe",
- "thumb_exists" => false,
- "bytes" => 0,
- "modified" => "Mon, 18 Jul 2011 20:13:43 +0000",
- "client_mtime" => "Wed, 20 Apr 2011 16:20:19 +0000",
- "path" => "/Public/latest.txt",
- "is_dir" => false,
- "icon" => "page_white_text",
- "root" => "dropbox",
- "mime_type" => "text/plain",
- "revision" => 220191,
- )
- )
- );
-
- $this->dropbox->expects($this->once())
- ->method('getMetadata')
- ->will($this->returnValue($data));
-
- $keys = $this->adapter->keys();
-
- $this->assertCount(2, $keys);
- $this->assertEquals(array('Public/newest', "Public/latest.txt"), $keys);
- }
}
Oops, something went wrong.

0 comments on commit a4caf2b

Please sign in to comment.