1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gaufrette\Functional\Adapter; |
4
|
|
|
|
5
|
|
|
use Gaufrette\Adapter\Aws\S3; |
6
|
|
|
use Aws\S3\S3Client; |
7
|
|
|
use Guzzle\Plugin\Mock\MockPlugin; |
8
|
|
|
use Guzzle\Http\Message\Response; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @todo move to phpspec |
12
|
|
|
*/ |
13
|
|
|
class S3Test extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
protected function getClient() |
16
|
|
|
{ |
17
|
|
|
return S3Client::factory(array( |
18
|
|
|
'key' => 'foo', |
19
|
|
|
'secret' => 'bar' |
20
|
|
|
)); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testCreatesBucketIfMissing() |
24
|
|
|
{ |
25
|
|
|
$mock = new MockPlugin(array( |
26
|
|
|
new Response(404), // Head bucket response |
27
|
|
|
new Response(200), // Create bucket response |
28
|
|
|
new Response(200, array(), 'foo') // Get object response |
29
|
|
|
)); |
30
|
|
|
$client = $this->getClient(); |
31
|
|
|
$client->addSubscriber($mock); |
32
|
|
|
$adapter = new S3($client, 'bucket', array('create' => true)); |
33
|
|
|
$this->assertEquals('foo', $adapter->read('foo')); |
34
|
|
|
|
35
|
|
|
$requests = $mock->getReceivedRequests(); |
36
|
|
|
$this->assertEquals('HEAD', $requests[0]->getMethod()); |
37
|
|
|
$this->assertEquals('PUT', $requests[1]->getMethod()); |
38
|
|
|
$this->assertEquals('GET', $requests[2]->getMethod()); |
39
|
|
|
$this->assertEquals('bucket.s3.amazonaws.com', $requests[0]->getHost()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @expectedException \RuntimeException |
44
|
|
|
*/ |
45
|
|
|
public function testThrowsExceptionIfBucketMissingAndNotCreating() |
46
|
|
|
{ |
47
|
|
|
$mock = new MockPlugin(array(new Response(404))); |
48
|
|
|
$client = $this->getClient(); |
49
|
|
|
$client->addSubscriber($mock); |
50
|
|
|
$adapter = new S3($client, 'bucket'); |
51
|
|
|
$adapter->read('foo'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testWritesObjects() |
55
|
|
|
{ |
56
|
|
|
$mock = new MockPlugin(array( |
57
|
|
|
new Response(200), // HEAD bucket response |
58
|
|
|
new Response(201) // PUT object response |
59
|
|
|
)); |
60
|
|
|
$client = $this->getClient(); |
61
|
|
|
$client->addSubscriber($mock); |
62
|
|
|
$adapter = new S3($client, 'bucket'); |
63
|
|
|
$this->assertEquals(7, $adapter->write('foo', 'testing')); |
64
|
|
|
$requests = $mock->getReceivedRequests(); |
65
|
|
|
$this->assertEquals('bucket.s3.amazonaws.com', $requests[1]->getHost()); |
66
|
|
|
$this->assertEquals('PUT', $requests[1]->getMethod()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function testChecksForObjectExistence() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$mock = new MockPlugin(array(new Response(200))); |
72
|
|
|
$client = $this->getClient(); |
73
|
|
|
$client->addSubscriber($mock); |
74
|
|
|
$adapter = new S3($client, 'bucket'); |
75
|
|
|
$this->assertTrue($adapter->exists('foo')); |
76
|
|
|
$requests = $mock->getReceivedRequests(); |
77
|
|
|
$this->assertEquals('bucket.s3.amazonaws.com', $requests[0]->getHost()); |
78
|
|
|
$this->assertEquals('HEAD', $requests[0]->getMethod()); |
79
|
|
|
$this->assertEquals('/foo', $requests[0]->getResource()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testGetsObjectUrls() |
83
|
|
|
{ |
84
|
|
|
$client = $this->getClient(); |
85
|
|
|
$adapter = new S3($client, 'bucket'); |
86
|
|
|
$this->assertEquals('https://bucket.s3.amazonaws.com/foo', $adapter->getUrl('foo')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
public function testChecksForObjectExistenceWithDirectory() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$mock = new MockPlugin(array(new Response(200))); |
92
|
|
|
$client = $this->getClient(); |
93
|
|
|
$client->addSubscriber($mock); |
94
|
|
|
$adapter = new S3($client, 'bucket', array('directory' => 'bar')); |
95
|
|
|
$this->assertTrue($adapter->exists('foo')); |
96
|
|
|
$requests = $mock->getReceivedRequests(); |
97
|
|
|
$this->assertEquals('bucket.s3.amazonaws.com', $requests[0]->getHost()); |
98
|
|
|
$this->assertEquals('HEAD', $requests[0]->getMethod()); |
99
|
|
|
$this->assertEquals('/bar/foo', $requests[0]->getResource()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testGetsObjectUrlsWithDirectory() |
103
|
|
|
{ |
104
|
|
|
$client = $this->getClient(); |
105
|
|
|
$adapter = new S3($client, 'bucket', array('directory' => 'bar')); |
106
|
|
|
$this->assertEquals('https://bucket.s3.amazonaws.com/bar/foo', $adapter->getUrl('foo')); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function shouldListKeysWithoutDirectory() |
110
|
|
|
{ |
111
|
|
|
$client = $this->getClient(); |
112
|
|
|
$adapter = new S3($client, 'bucket', array('directory' => 'bar')); |
113
|
|
|
$adapter->write('test.txt', 'some content'); |
114
|
|
|
$keys = $adapter->listKeys(); |
115
|
|
|
$this->assertEquals('test.txt', $keys['key']); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.