diff --git a/src/Flysystem/Adapter/Dropbox.php b/src/Flysystem/Adapter/Dropbox.php index b4709ddff..4588d9244 100644 --- a/src/Flysystem/Adapter/Dropbox.php +++ b/src/Flysystem/Adapter/Dropbox.php @@ -45,7 +45,7 @@ public function write($path, $contents, $visibility = null) public function writeStream($path, $resource, $visibility = null) { - $this->uploadStream($path, $resource, $visibility, WriteMode::add()); + return $this->uploadStream($path, $resource, $visibility, WriteMode::add()); } public function update($path, $contents) @@ -55,19 +55,23 @@ public function update($path, $contents) public function updateStream($path, $resource, $visibility = null) { - $this->uploadStream($path, $resource, $visibility, WriteMode::force()); + return $this->uploadStream($path, $resource, $visibility, WriteMode::force()); } public function upload($path, $contents, $mode) { - $result = $this->client->uploadFileFromString($this->prefix($path), $mode, $contents); + if ( ! $result = $this->client->uploadFileFromString($this->prefix($path), $mode, $contents)) { + return false; + } return $this->normalizeObject($result, $path); } protected function uploadStream($path, $resource, $mode) { - $result = $this->client->uploadFile($path, $mode, $resource); + if ( ! $result = $this->client->uploadFile($path, $mode, $resource)) { + return false; + } return $this->normalizeObject($result, $path); } @@ -101,16 +105,14 @@ public function readStream($path) public function rename($path, $newpath) { - $options = $this->getOptions($newpath, array( - 'Bucket' => $this->bucket, - 'CopySource' => $this->bucket.'/'.$this->prefix($path), - )); + $path = $this->prefix($path); + $newpath = $this->prefix($newpath); - $result = $this->client->copyObject($options)->getAll(); - $result = $this->normalizeObject($result, $newpath); - $this->delete($path); + if ( ! $result = $this->client->move($path, $newpath)) { + return false; + } - return $result; + return $this->normalizeObject($result); } public function delete($path) @@ -164,7 +166,10 @@ public function retrieveListing($dir, $recursive = true) $listing = array(); $directory = rtrim($dir, '/'); $length = strlen($directory) + 1; - $result = $this->client->getMetadataWithChildren($directory); + + if ( ! $result = $this->client->getMetadataWithChildren($directory)) { + return false; + } foreach ($result['contents'] as $object) { diff --git a/tests/DropboxTests.php b/tests/DropboxTests.php new file mode 100644 index 000000000..413587f17 --- /dev/null +++ b/tests/DropboxTests.php @@ -0,0 +1,197 @@ +shouldReceive('__toString')->andReturn('Dropbox\Client'); + + return $mock; + } + + public function testInstantiable() + { + $adapter = new Dropbox($this->getClientMock(), 'prefix'); + } + + public function dropboxProvider() + { + $mock = $this->getClientMock(); + + return array( + array(new Dropbox($mock, 'prefix'), $mock), + ); + } + + /** + * @dataProvider dropboxProvider + */ + public function testWrite($adapter, $mock) + { + $mock->shouldReceive('uploadFileFromString')->andReturn(array( + 'is_dir' => false, + 'modified' => '10 September 2000', + ), false); + + $result = $adapter->write('something', 'contents'); + $this->assertInternalType('array', $result); + $this->assertArrayHasKey('type', $result); + $this->assertEquals('file', $result['type']); + $this->assertFalse($adapter->write('something', 'something')); + } + + /** + * @dataProvider dropboxProvider + */ + public function testUpdate($adapter, $mock) + { + $mock->shouldReceive('uploadFileFromString')->andReturn(array( + 'is_dir' => false, + 'modified' => '10 September 2000', + ), false); + + $result = $adapter->update('something', 'contents'); + $this->assertInternalType('array', $result); + $this->assertArrayHasKey('type', $result); + $this->assertEquals('file', $result['type']); + $this->assertFalse($adapter->update('something', 'something')); + } + + /** + * @dataProvider dropboxProvider + */ + public function testWriteStream($adapter, $mock) + { + $mock->shouldReceive('uploadFile')->andReturn(array( + 'is_dir' => false, + 'modified' => '10 September 2000', + ), false); + + $result = $adapter->writeStream('something', 'contents'); + $this->assertInternalType('array', $result); + $this->assertArrayHasKey('type', $result); + $this->assertEquals('file', $result['type']); + $this->assertFalse($adapter->writeStream('something', 'something')); + } + + /** + * @dataProvider dropboxProvider + */ + public function testUpdateStream($adapter, $mock) + { + $mock->shouldReceive('uploadFile')->andReturn(array( + 'is_dir' => false, + 'modified' => '10 September 2000', + ), false); + + $result = $adapter->updateStream('something', 'contents'); + $this->assertInternalType('array', $result); + $this->assertArrayHasKey('type', $result); + $this->assertEquals('file', $result['type']); + $this->assertFalse($adapter->updateStream('something', 'something')); + } + + public function metadataProvider() + { + return array( + array('getMetadata'), + array('getMimetype'), + array('getTimestamp'), + array('getSize'), + array('has'), + ); + } + + /** + * @dataProvider metadataProvider + */ + public function testMetadataCalls($method) + { + $mock = $this->getClientMock(); + $mock->shouldReceive('getMetadata')->twice()->andReturn(array( + 'is_dir' => false, + 'modified' => '10 September 2000', + ), false); + + $adapter = new Dropbox($mock); + $this->assertInternalType('array', $adapter->{$method}('one', 'two')); + $this->assertFalse($adapter->{$method}('one', 'two')); + } + + /** + * @dataProvider dropboxProvider + */ + public function testRead($adapter, $mock) + { + $stream = tmpfile(); + fwrite($stream, 'something'); + $mock->shouldReceive('getFile')->andReturn($stream, false); + $this->assertInternalType('array', $adapter->read('something')); + $this->assertFalse($adapter->read('something')); + fclose($stream); + } + + /** + * @dataProvider dropboxProvider + */ + public function testReadStream($adapter, $mock) + { + $stream = tmpfile(); + fwrite($stream, 'something'); + $mock->shouldReceive('getFile')->andReturn($stream, false); + $this->assertInternalType('array', $adapter->readStream('something')); + $this->assertFalse($adapter->readStream('something')); + fclose($stream); + } + + /** + * @dataProvider dropboxProvider + */ + public function testDelete($adapter, $mock) + { + $mock->shouldReceive('delete')->andReturn(true); + $this->assertTrue($adapter->delete('something')); + $this->assertTrue($adapter->deleteDir('something')); + } + + /** + * @dataProvider dropboxProvider + */ + public function testCreateDir($adapter, $mock) + { + $this->assertInternalType('array', $adapter->createDir('something')); + } + + /** + * @dataProvider dropboxProvider + */ + public function testListContents($adapter, $mock) + { + $mock->shouldReceive('getMetadataWithChildren')->andReturn( + array('contents' => array( + array('is_dir' => true, 'path' => 'dirname'), + )), + array('contents' => array( + array('is_dir' => false, 'path' => 'dirname/file'), + )), + false + ); + + $result = $adapter->listContents('', true); + $this->assertCount(2, $result); + $this->assertFalse($adapter->listContents('', false)); + } + + /** + * @dataProvider dropboxProvider + */ + public function testRename($adapter, $mock) + { + $mock->shouldReceive('move')->andReturn(array('is_dir' => false, 'path' => 'something'), false); + $this->assertInternalType('array', $adapter->rename('something', 'something')); + $this->assertFalse($adapter->rename('something', 'something')); + } +} \ No newline at end of file diff --git a/tests/FilesystemTests.php b/tests/FilesystemTests.php index ad9db4bea..849f7ea6e 100644 --- a/tests/FilesystemTests.php +++ b/tests/FilesystemTests.php @@ -2,7 +2,7 @@ namespace Flysystem; -class FlilesystemTests extends \PHPUnit_Framework_TestCase +class FilesystemTests extends \PHPUnit_Framework_TestCase { public function setup() {