Skip to content

Commit 868167e

Browse files
committed
Added dropbox tests, fixed rename command
1 parent 9d660b2 commit 868167e

File tree

3 files changed

+216
-14
lines changed

3 files changed

+216
-14
lines changed

src/Flysystem/Adapter/Dropbox.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function write($path, $contents, $visibility = null)
4545

4646
public function writeStream($path, $resource, $visibility = null)
4747
{
48-
$this->uploadStream($path, $resource, $visibility, WriteMode::add());
48+
return $this->uploadStream($path, $resource, $visibility, WriteMode::add());
4949
}
5050

5151
public function update($path, $contents)
@@ -55,19 +55,23 @@ public function update($path, $contents)
5555

5656
public function updateStream($path, $resource, $visibility = null)
5757
{
58-
$this->uploadStream($path, $resource, $visibility, WriteMode::force());
58+
return $this->uploadStream($path, $resource, $visibility, WriteMode::force());
5959
}
6060

6161
public function upload($path, $contents, $mode)
6262
{
63-
$result = $this->client->uploadFileFromString($this->prefix($path), $mode, $contents);
63+
if ( ! $result = $this->client->uploadFileFromString($this->prefix($path), $mode, $contents)) {
64+
return false;
65+
}
6466

6567
return $this->normalizeObject($result, $path);
6668
}
6769

6870
protected function uploadStream($path, $resource, $mode)
6971
{
70-
$result = $this->client->uploadFile($path, $mode, $resource);
72+
if ( ! $result = $this->client->uploadFile($path, $mode, $resource)) {
73+
return false;
74+
}
7175

7276
return $this->normalizeObject($result, $path);
7377
}
@@ -101,16 +105,14 @@ public function readStream($path)
101105

102106
public function rename($path, $newpath)
103107
{
104-
$options = $this->getOptions($newpath, array(
105-
'Bucket' => $this->bucket,
106-
'CopySource' => $this->bucket.'/'.$this->prefix($path),
107-
));
108+
$path = $this->prefix($path);
109+
$newpath = $this->prefix($newpath);
108110

109-
$result = $this->client->copyObject($options)->getAll();
110-
$result = $this->normalizeObject($result, $newpath);
111-
$this->delete($path);
111+
if ( ! $result = $this->client->move($path, $newpath)) {
112+
return false;
113+
}
112114

113-
return $result;
115+
return $this->normalizeObject($result);
114116
}
115117

116118
public function delete($path)
@@ -164,7 +166,10 @@ public function retrieveListing($dir, $recursive = true)
164166
$listing = array();
165167
$directory = rtrim($dir, '/');
166168
$length = strlen($directory) + 1;
167-
$result = $this->client->getMetadataWithChildren($directory);
169+
170+
if ( ! $result = $this->client->getMetadataWithChildren($directory)) {
171+
return false;
172+
}
168173

169174
foreach ($result['contents'] as $object)
170175
{

tests/DropboxTests.php

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<?php
2+
3+
use Flysystem\Adapter\Dropbox;
4+
5+
class DropboxTests extends PHPUnit_Framework_TestCase
6+
{
7+
public function getClientMock()
8+
{
9+
$mock = Mockery::mock('Dropbox\Client');
10+
$mock->shouldReceive('__toString')->andReturn('Dropbox\Client');
11+
12+
return $mock;
13+
}
14+
15+
public function testInstantiable()
16+
{
17+
$adapter = new Dropbox($this->getClientMock(), 'prefix');
18+
}
19+
20+
public function dropboxProvider()
21+
{
22+
$mock = $this->getClientMock();
23+
24+
return array(
25+
array(new Dropbox($mock, 'prefix'), $mock),
26+
);
27+
}
28+
29+
/**
30+
* @dataProvider dropboxProvider
31+
*/
32+
public function testWrite($adapter, $mock)
33+
{
34+
$mock->shouldReceive('uploadFileFromString')->andReturn(array(
35+
'is_dir' => false,
36+
'modified' => '10 September 2000',
37+
), false);
38+
39+
$result = $adapter->write('something', 'contents');
40+
$this->assertInternalType('array', $result);
41+
$this->assertArrayHasKey('type', $result);
42+
$this->assertEquals('file', $result['type']);
43+
$this->assertFalse($adapter->write('something', 'something'));
44+
}
45+
46+
/**
47+
* @dataProvider dropboxProvider
48+
*/
49+
public function testUpdate($adapter, $mock)
50+
{
51+
$mock->shouldReceive('uploadFileFromString')->andReturn(array(
52+
'is_dir' => false,
53+
'modified' => '10 September 2000',
54+
), false);
55+
56+
$result = $adapter->update('something', 'contents');
57+
$this->assertInternalType('array', $result);
58+
$this->assertArrayHasKey('type', $result);
59+
$this->assertEquals('file', $result['type']);
60+
$this->assertFalse($adapter->update('something', 'something'));
61+
}
62+
63+
/**
64+
* @dataProvider dropboxProvider
65+
*/
66+
public function testWriteStream($adapter, $mock)
67+
{
68+
$mock->shouldReceive('uploadFile')->andReturn(array(
69+
'is_dir' => false,
70+
'modified' => '10 September 2000',
71+
), false);
72+
73+
$result = $adapter->writeStream('something', 'contents');
74+
$this->assertInternalType('array', $result);
75+
$this->assertArrayHasKey('type', $result);
76+
$this->assertEquals('file', $result['type']);
77+
$this->assertFalse($adapter->writeStream('something', 'something'));
78+
}
79+
80+
/**
81+
* @dataProvider dropboxProvider
82+
*/
83+
public function testUpdateStream($adapter, $mock)
84+
{
85+
$mock->shouldReceive('uploadFile')->andReturn(array(
86+
'is_dir' => false,
87+
'modified' => '10 September 2000',
88+
), false);
89+
90+
$result = $adapter->updateStream('something', 'contents');
91+
$this->assertInternalType('array', $result);
92+
$this->assertArrayHasKey('type', $result);
93+
$this->assertEquals('file', $result['type']);
94+
$this->assertFalse($adapter->updateStream('something', 'something'));
95+
}
96+
97+
public function metadataProvider()
98+
{
99+
return array(
100+
array('getMetadata'),
101+
array('getMimetype'),
102+
array('getTimestamp'),
103+
array('getSize'),
104+
array('has'),
105+
);
106+
}
107+
108+
/**
109+
* @dataProvider metadataProvider
110+
*/
111+
public function testMetadataCalls($method)
112+
{
113+
$mock = $this->getClientMock();
114+
$mock->shouldReceive('getMetadata')->twice()->andReturn(array(
115+
'is_dir' => false,
116+
'modified' => '10 September 2000',
117+
), false);
118+
119+
$adapter = new Dropbox($mock);
120+
$this->assertInternalType('array', $adapter->{$method}('one', 'two'));
121+
$this->assertFalse($adapter->{$method}('one', 'two'));
122+
}
123+
124+
/**
125+
* @dataProvider dropboxProvider
126+
*/
127+
public function testRead($adapter, $mock)
128+
{
129+
$stream = tmpfile();
130+
fwrite($stream, 'something');
131+
$mock->shouldReceive('getFile')->andReturn($stream, false);
132+
$this->assertInternalType('array', $adapter->read('something'));
133+
$this->assertFalse($adapter->read('something'));
134+
fclose($stream);
135+
}
136+
137+
/**
138+
* @dataProvider dropboxProvider
139+
*/
140+
public function testReadStream($adapter, $mock)
141+
{
142+
$stream = tmpfile();
143+
fwrite($stream, 'something');
144+
$mock->shouldReceive('getFile')->andReturn($stream, false);
145+
$this->assertInternalType('array', $adapter->readStream('something'));
146+
$this->assertFalse($adapter->readStream('something'));
147+
fclose($stream);
148+
}
149+
150+
/**
151+
* @dataProvider dropboxProvider
152+
*/
153+
public function testDelete($adapter, $mock)
154+
{
155+
$mock->shouldReceive('delete')->andReturn(true);
156+
$this->assertTrue($adapter->delete('something'));
157+
$this->assertTrue($adapter->deleteDir('something'));
158+
}
159+
160+
/**
161+
* @dataProvider dropboxProvider
162+
*/
163+
public function testCreateDir($adapter, $mock)
164+
{
165+
$this->assertInternalType('array', $adapter->createDir('something'));
166+
}
167+
168+
/**
169+
* @dataProvider dropboxProvider
170+
*/
171+
public function testListContents($adapter, $mock)
172+
{
173+
$mock->shouldReceive('getMetadataWithChildren')->andReturn(
174+
array('contents' => array(
175+
array('is_dir' => true, 'path' => 'dirname'),
176+
)),
177+
array('contents' => array(
178+
array('is_dir' => false, 'path' => 'dirname/file'),
179+
)),
180+
false
181+
);
182+
183+
$result = $adapter->listContents('', true);
184+
$this->assertCount(2, $result);
185+
$this->assertFalse($adapter->listContents('', false));
186+
}
187+
188+
/**
189+
* @dataProvider dropboxProvider
190+
*/
191+
public function testRename($adapter, $mock)
192+
{
193+
$mock->shouldReceive('move')->andReturn(array('is_dir' => false, 'path' => 'something'), false);
194+
$this->assertInternalType('array', $adapter->rename('something', 'something'));
195+
$this->assertFalse($adapter->rename('something', 'something'));
196+
}
197+
}

tests/FilesystemTests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Flysystem;
44

5-
class FlilesystemTests extends \PHPUnit_Framework_TestCase
5+
class FilesystemTests extends \PHPUnit_Framework_TestCase
66
{
77
public function setup()
88
{

0 commit comments

Comments
 (0)