Skip to content

Commit

Permalink
Added dropbox tests, fixed rename command
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Dec 2, 2013
1 parent 9d660b2 commit 868167e
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 14 deletions.
31 changes: 18 additions & 13 deletions src/Flysystem/Adapter/Dropbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down
197 changes: 197 additions & 0 deletions tests/DropboxTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
<?php

use Flysystem\Adapter\Dropbox;

class DropboxTests extends PHPUnit_Framework_TestCase
{
public function getClientMock()
{
$mock = Mockery::mock('Dropbox\Client');
$mock->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'));
}
}
2 changes: 1 addition & 1 deletion tests/FilesystemTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Flysystem;

class FlilesystemTests extends \PHPUnit_Framework_TestCase
class FilesystemTests extends \PHPUnit_Framework_TestCase
{
public function setup()
{
Expand Down

0 comments on commit 868167e

Please sign in to comment.