Completed
Pull Request — master (#435)
by Albin
09:27
created

S3Test   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 22.86 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 24
loc 105
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getClient() 0 7 1
A testCreatesBucketIfMissing() 0 18 1
A testThrowsExceptionIfBucketMissingAndNotCreating() 0 8 1
A testWritesObjects() 0 14 1
A testChecksForObjectExistence() 12 12 1
A testGetsObjectUrls() 0 6 1
A testChecksForObjectExistenceWithDirectory() 12 12 1
A testGetsObjectUrlsWithDirectory() 0 6 1
A shouldListKeysWithoutDirectory() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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