Completed
Pull Request — master (#130)
by
unknown
02:35
created

AttachmentTest::testAddAttachment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ODM\CouchDB\Functional;
4
5
use Doctrine\CouchDB\Attachment;
6
7
class AttachmentTest extends \Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCase
8
{
9
    /**
10
     * @var DocumentManager
11
     */
12
    private $dm;
13
14
    public function setUp()
15
    {
16
        $this->dm = $this->createDocumentManager();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createDocumentManager() of type object<Doctrine\ODM\CouchDB\DocumentManager> is incompatible with the declared type object<Doctrine\Tests\OD...tional\DocumentManager> of property $dm.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
17
        $client = $this->dm->getHttpClient();
18
        $response = $client->request('PUT', '/' . $this->getTestDatabase() . '/user_with_attachment', \file_get_contents(__DIR__ . "/_files/user_with_attachment.json"));
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
19
        $response = $client->request('PUT', '/' . $this->getTestDatabase() . '/attachment_with_spaces_in_name', \file_get_contents(__DIR__ . "/_files/attachment_with_spaces_in_name.json"));
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
20
    }
21
22
    public function testGetAttachmentWithSpacesInName()
23
    {
24
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', 'attachment_with_spaces_in_name');
25
        $this->assertArrayHasKey('attachment with spaces.txt', $user->attachments);
26
        $this->assertInstanceOf('Doctrine\CouchDB\Attachment', $user->attachments['attachment with spaces.txt']);
27
        $this->assertEquals('This is a base64 encoded text', $user->attachments['attachment with spaces.txt']->getRawData());
28
    }
29
30
    public function testHydrateAttachments()
31
    {
32
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', 'user_with_attachment');
33
34
        $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $user, 'User not hydrated correctly!');
35
        $this->assertInternalType('array', $user->attachments, "Attachments are always an array.");
36
        $this->assertArrayHasKey('foo.txt', $user->attachments);
37
        $this->assertInstanceOf('Doctrine\CouchDB\Attachment', $user->attachments['foo.txt']);
38
        $this->assertFalse($user->attachments['foo.txt']->isLoaded());
39
        $this->assertEquals('This is a base64 encoded text', $user->attachments['foo.txt']->getRawData());
40
    }
41
42
    public function testPersistUnchangedAttachmentCorrectly()
43
    {
44
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', 'user_with_attachment');
45
        $user->username = "newusername!";
46
47
        $this->dm->flush();
48
        $this->dm->clear(); // dont re-use identity map
49
50
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', 'user_with_attachment');
51
        $this->assertArrayHasKey('foo.txt', $user->attachments);
52
        $this->assertInstanceOf('Doctrine\CouchDB\Attachment', $user->attachments['foo.txt']);
53
    }
54
55
    public function testRemoveAttachment()
56
    {
57
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', 'user_with_attachment');
58
        unset($user->attachments['foo.txt']);
59
60
        $this->dm->flush();
61
        $this->dm->clear(); // dont re-use identity map
62
63
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', 'user_with_attachment');
64
        $this->assertEquals(0, count($user->attachments));
65
    }
66
67
    public function testAddAttachment()
68
    {
69
        $fh = fopen(__DIR__ . '/_files/logo.jpg', 'r');
70
71
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', 'user_with_attachment');
72
        $user->attachments['logo.jpg'] = Attachment::createFromBinaryData($fh, 'image/jpeg');
73
74
        $this->dm->flush();
75
        $this->dm->clear(); // dont re-use identity map
76
77
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', 'user_with_attachment');
78
79
        $this->assertEquals(2, count($user->attachments));
80
        $this->assertArrayHasKey('logo.jpg', $user->attachments);
81
        $this->assertInstanceOf('Doctrine\CouchDB\Attachment', $user->attachments['foo.txt']);
82
    }
83
84
    public function testUpdateAttachment()
85
    {
86
        $fh = fopen(__DIR__ . '/_files/foo.txt', 'r');
87
88
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', 'user_with_attachment');
89
        $user->attachments['foo.txt'] = Attachment::createFromBinaryData($fh, 'text/plain');
90
91
        $this->dm->flush();
92
        $this->dm->clear(); // dont re-use identity map
93
94
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', 'user_with_attachment');
95
        $this->assertEquals('Hello i am a string!', $user->attachments['foo.txt']->getRawData());
96
    }
97
98
    public function testAddRemoveAttachment()
99
    {
100
        $fh = fopen(__DIR__ . '/_files/logo.jpg', 'r');
101
102
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', 'user_with_attachment');
103
        $user->attachments['logo.jpg'] = Attachment::createFromBinaryData($fh, 'image/jpeg');
104
105
        $this->dm->flush();
106
        $this->dm->clear(); // dont re-use identity map
107
108
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', 'user_with_attachment');
109
        unset($user->attachments['foo.txt']);
110
111
        $this->dm->flush();
112
        $this->dm->clear(); // dont re-use identity map
113
114
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', 'user_with_attachment');
115
        $this->assertArrayHasKey('logo.jpg', $user->attachments);
116
        $this->assertArrayNotHasKey('foo.txt', $user->attachments);
117
    }
118
}