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(); |
|
|
|
|
17
|
|
|
$client = $this->dm->getHttpClient(); |
18
|
|
|
$response = $client->request('PUT', '/' . $this->getTestDatabase() . '/user_with_attachment', \file_get_contents(__DIR__ . "/_files/user_with_attachment.json")); |
|
|
|
|
19
|
|
|
$response = $client->request('PUT', '/' . $this->getTestDatabase() . '/attachment_with_spaces_in_name', \file_get_contents(__DIR__ . "/_files/attachment_with_spaces_in_name.json")); |
|
|
|
|
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
|
|
|
} |
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..