AttachmentTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 97
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateFromBinaryData() 0 10 1
A testCreateFromBase64Data() 0 12 1
A testCreateStub() 0 11 1
A testTriggerStubLazyLoad() 0 17 1
A testTriggerLazyLoadOfMissingAttachmentThrowsException() 0 15 1
A testToArray() 0 7 1
A testToArrayStub() 0 8 1
A testCreateFromBinaryFileHandle() 0 7 1
1
<?php
2
3
namespace Doctrine\Tests\ODM\CouchDB;
4
5
use Doctrine\CouchDB\Attachment;
6
7
class AttachmentTest extends CouchDBTestCase
8
{
9
    public function testCreateFromBinaryData()
10
    {
11
        $text = "Hello i am a string";
12
        $attachment = Attachment::createFromBinaryData($text, "text/plain");
13
14
        $this->assertEquals($text, $attachment->getRawData());
15
        $this->assertEquals("text/plain", $attachment->getContentType());
16
        $this->assertEquals("SGVsbG8gaSBhbSBhIHN0cmluZw==", $attachment->getBase64EncodedData());
17
        $this->assertEquals(28, $attachment->getLength());
18
    }
19
20
    public function testCreateFromBase64Data()
21
    {
22
        $data = "SGVsbG8gaSBhbSBhIHN0cmluZw==";
23
24
        $attachment = Attachment::createFromBase64Data($data, "text/plain", 2);
25
26
        $this->assertEquals("Hello i am a string", $attachment->getRawData());
27
        $this->assertEquals($data, $attachment->getBase64EncodedData());
28
        $this->assertEquals("text/plain", $attachment->getContentType());
29
        $this->assertEquals(28, $attachment->getLength());
30
        $this->assertEquals(2, $attachment->getRevPos());
31
    }
32
33
    public function testCreateStub()
34
    {
35
        $httpClient = $this->getMock('Doctrine\CouchDB\HTTP\Client');
36
        $httpClient->expects($this->never())->method('request');
37
        $attachment = Attachment::createStub('plain/text', 28, 2, $httpClient, '/');
38
39
        $this->assertEquals('plain/text', $attachment->getContentType());
40
        $this->assertEquals(28, $attachment->getLength());
41
        $this->assertEquals(2, $attachment->getRevPos());
42
        $this->assertFalse($attachment->isLoaded());
43
    }
44
45
    public function testTriggerStubLazyLoad()
46
    {
47
        $path = '/';
48
49
        $response = new \Doctrine\CouchDB\HTTP\Response(200, array(), 'Hello i am a string', true);
50
        $httpClient = $this->getMock('Doctrine\CouchDB\HTTP\Client');
51
        $httpClient->expects($this->once())
52
                   ->method('request')
53
                   ->with($this->equalTo('GET'), $this->equalTo($path))
54
                   ->will($this->returnValue( $response ));
55
        $attachment = Attachment::createStub('plain/text', 28, 2, $httpClient, $path);
56
57
        $this->assertFalse($attachment->isLoaded());
58
        $this->assertEquals('Hello i am a string', $attachment->getRawData());
59
        $this->assertEquals('SGVsbG8gaSBhbSBhIHN0cmluZw==', $attachment->getBase64EncodedData());
60
        $this->assertTrue($attachment->isLoaded());
61
    }
62
63
    public function testTriggerLazyLoadOfMissingAttachmentThrowsException()
64
    {
65
        $path = '/';
66
67
        $errorResponse = new \Doctrine\CouchDB\HTTP\ErrorResponse(404, array(), '{"error":"not_found","reason":"missing"}');
68
        $httpClient = $this->getMock('Doctrine\CouchDB\HTTP\Client');
69
        $httpClient->expects($this->once())
70
                   ->method('request')
71
                   ->with($this->equalTo('GET'), $this->equalTo($path))
72
                   ->will($this->returnValue( $errorResponse ));
73
        $attachment = Attachment::createStub('plain/text', 28, 2, $httpClient, $path);
74
75
        $this->setExpectedException('Doctrine\CouchDB\HTTP\HTTPException');
76
        $attachment->getRawData();
77
    }
78
79
    public function testToArray()
80
    {
81
        $text = "Hello i am a string";
82
        $attachment = Attachment::createFromBinaryData($text, "text/plain");
83
84
        $this->assertEquals(array("data" => "SGVsbG8gaSBhbSBhIHN0cmluZw==", "content_type" => "text/plain"), $attachment->toArray());
85
    }
86
87
    public function testToArrayStub()
88
    {
89
        $httpClient = $this->getMock('Doctrine\CouchDB\HTTP\Client');
90
        $httpClient->expects($this->never())->method('request');
91
        $attachment = Attachment::createStub('plain/text', 28, 2, $httpClient, '/');
92
93
        $this->assertEquals(array('stub' => true), $attachment->toArray());
94
    }
95
96
    public function testCreateFromBinaryFileHandle()
97
    {
98
        $fh = fopen(__DIR__ . "/_files/foo.txt", "r");
99
100
        $attachment = Attachment::createFromBinaryData($fh);
101
        $this->assertEquals('Hello i am a string!', $attachment->getRawData());
102
    }
103
}