This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathGridFSTest.php
164 lines (127 loc) · 4.68 KB
/
GridFSTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Doctrine\MongoDB\Tests;
use Doctrine\MongoDB\GridFSFile;
class GridFSTest extends DatabaseTestCase
{
public function testInsertFindOneAndWrite()
{
$document = [
'foo' => 'bar',
'file' => new GridFSFile(__FILE__),
];
$gridFS = $this->getGridFS();
$gridFS->insert($document);
$document = $gridFS->findOne(['_id' => $document['_id']]);
$this->assertArrayHasKey('_id', $document);
$this->assertEquals('bar', $document['foo']);
$file = $document['file'];
$this->assertInstanceOf('Doctrine\MongoDB\GridFSFile', $file);
$this->assertFalse($file->isDirty());
$this->assertEquals(__FILE__, $file->getFilename());
$this->assertStringEqualsFile(__FILE__, $file->getBytes());
$this->assertEquals(filesize(__FILE__), $file->getSize());
$path = tempnam(sys_get_temp_dir(), 'doctrine_write_test');
$this->assertNotEquals(false, $path);
$file->write($path);
$this->assertFileEquals(__FILE__, $path);
unlink($path);
}
public function testStoreFile()
{
$document = ['foo' => 'bar'];
$gridFS = $this->getGridFS();
$file = $gridFS->storeFile(__FILE__, $document);
$this->assertArrayHasKey('_id', $document);
$this->assertEquals('bar', $document['foo']);
$this->assertInstanceOf('Doctrine\MongoDB\GridFSFile', $file);
$this->assertFalse($file->isDirty());
$this->assertEquals(__FILE__, $file->getFilename());
$this->assertStringEqualsFile(__FILE__, $file->getBytes());
$this->assertEquals(filesize(__FILE__), $file->getSize());
}
public function testUpdate()
{
$gridFS = $this->getGridFS();
$path = __DIR__.'/file.txt';
$file = new GridFSFile($path);
$document = [
'title' => 'Test Title',
'file' => $file
];
$gridFS->insert($document);
$id = $document['_id'];
$document = $gridFS->findOne(['_id' => $id]);
$file = $document['file'];
$gridFS->update(['_id' => $id], ['$pushAll' => ['test' => [1, 2, 3]]]);
$check = $gridFS->findOne(['_id' => $id]);
$this->assertArrayHasKey('test', $check);
$this->assertCount(3, $check['test']);
$this->assertEquals([1, 2, 3], $check['test']);
$gridFS->update(['_id' => $id], ['_id' => $id]);
$gridFS->update(['_id' => $id], ['_id' => $id, 'boom' => true]);
$check = $gridFS->findOne(['_id' => $id]);
$this->assertArrayNotHasKey('test', $check);
$this->assertTrue($check['boom']);
}
public function testUpsertDocumentWithoutFile()
{
$gridFS = $this->getGridFS();
$gridFS->update(
['id' => 123],
['x' => 1],
['upsert' => true, 'multiple' => false]
);
$document = $gridFS->findOne();
$this->assertNotNull($document);
$this->assertNotEquals(123, $document['_id']);
$this->assertEquals(1, $document['x']);
}
public function testUpsertDocumentWithoutFileWithId()
{
$gridFS = $this->getGridFS();
$gridFS->update(
['x' => 1],
['_id' => 123],
['upsert' => true, 'multiple' => false]
);
$document = $gridFS->findOne(['_id' => 123]);
$this->assertNotNull($document);
$this->assertArrayNotHasKey('x', $document);
}
public function testUpsertModifierWithoutFile()
{
$gridFS = $this->getGridFS();
$gridFS->update(
['_id' => 123],
['$set' => ['x' => 1]],
['upsert' => true, 'multiple' => false]
);
$document = $gridFS->findOne(['_id' => 123]);
$this->assertNotNull($document);
$this->assertEquals(1, $document['x']);
}
public function testUpsert()
{
$gridFS = $this->getGridFS();
$id = new \MongoId();
$path = __DIR__.'/file.txt';
$file = new GridFSFile($path);
$newObj = [
'$set' => [
'title' => 'Test Title',
'file' => $file,
],
];
$gridFS->update(['_id' => $id], $newObj, ['upsert' => true, 'multiple' => false]);
$document = $gridFS->findOne(['_id' => $id]);
$file = $document['file'];
$this->assertFalse($file->isDirty());
$this->assertEquals($path, $file->getFilename());
$this->assertEquals(file_get_contents($path), $file->getBytes());
$this->assertEquals(22, $file->getSize());
}
private function getGridFS()
{
return $this->conn->selectDatabase(self::$dbName)->getGridFS();
}
}