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 pathGridFSFileTest.php
164 lines (139 loc) · 4.61 KB
/
GridFSFileTest.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 GridFSFileTest extends DatabaseTestCase
{
public function testIsDirty()
{
$file = new GridFSFile();
$this->assertFalse($file->isDirty());
$file->isDirty(true);
$this->assertTrue($file->isDirty());
$file->isDirty(false);
$this->assertFalse($file->isDirty());
}
public function testSetAndGetBytes()
{
$file = new GridFSFile();
$file->setBytes('bytes');
$this->assertTrue($file->isDirty());
$this->assertTrue($file->hasUnpersistedBytes());
$this->assertFalse($file->hasUnpersistedFile());
$this->assertEquals('bytes', $file->getBytes());
}
public function testSetAndGetFilename()
{
$file = new GridFSFile();
$this->assertFalse($file->isDirty());
$file->setFilename(__FILE__);
$this->assertTrue($file->isDirty());
$this->assertFalse($file->hasUnpersistedBytes());
$this->assertTrue($file->hasUnpersistedFile());
$this->assertEquals(__FILE__, $file->getFilename());
}
public function testSetAndGetMongoGridFSFile()
{
$mongoGridFSFile = $this->getMockMongoGridFSFile();
$file = new GridFSFile();
$file->setMongoGridFSFile($mongoGridFSFile);
$this->assertSame($mongoGridFSFile, $file->getMongoGridFSFile());
}
public function testGetBytesWithSetBytes()
{
$file = new GridFSFile();
$file->setBytes('bytes');
$this->assertEquals('bytes', $file->getBytes());
}
public function testGetBytesWithSetFilename()
{
$file = new GridFSFile();
$file->setFilename(__FILE__);
$this->assertStringEqualsFile(__FILE__, $file->getBytes());
}
public function testGetBytesWithSetMongoGridFSFile()
{
$mongoGridFSFile = $this->getMockMongoGridFSFile();
$mongoGridFSFile->expects($this->once())
->method('getBytes')
->will($this->returnValue('bytes'));
$file = new GridFSFile();
$file->setMongoGridFSFile($mongoGridFSFile);
$this->assertEquals('bytes', $file->getBytes());
}
public function testGetBytesWithEmptyState()
{
$file = new GridFSFile();
$this->assertNull($file->getBytes());
}
public function testWriteWithSetBytes()
{
$path = tempnam(sys_get_temp_dir(), 'doctrine_write_test');
$this->assertNotEquals(false, $path);
$file = new GridFSFile();
$file->setBytes('bytes');
$file->write($path);
$this->assertStringEqualsFile($path, 'bytes');
unlink($path);
}
public function testWriteWithSetFilename()
{
$path = tempnam(sys_get_temp_dir(), 'doctrine_write_test');
$this->assertNotEquals(false, $path);
$file = new GridFSFile();
$file->setFilename(__FILE__);
$file->write($path);
$this->assertFileEquals(__FILE__, $path);
unlink($path);
}
public function testWriteWithSetMongoGridFSFile()
{
$mongoGridFSFile = $this->getMockMongoGridFSFile();
$mongoGridFSFile->expects($this->once())
->method('write')
->with('filename');
$file = new GridFSFile();
$file->setMongoGridFSFile($mongoGridFSFile);
$file->write('filename');
}
/**
* @expectedException BadMethodCallException
*/
public function testWriteWithEmptyState()
{
$file = new GridFSFile();
$file->write('filename');
}
public function testGetSizeWithSetBytes()
{
$file = new GridFSFile();
$file->setBytes('bytes');
$this->assertEquals(5, $file->getSize());
}
public function testGetSizeWithSetFilename()
{
$file = new GridFSFile();
$file->setFilename(__FILE__);
$this->assertEquals(filesize(__FILE__), $file->getSize());
}
public function testGetSizeWithSetMongoGridFSFile()
{
$mongoGridFSFile = $this->getMockMongoGridFSFile();
$mongoGridFSFile->expects($this->once())
->method('getSize')
->will($this->returnValue(200));
$file = new GridFSFile();
$file->setMongoGridFSFile($mongoGridFSFile);
$this->assertEquals(200, $file->getSize());
}
public function testGetSizeWithEmptyState()
{
$file = new GridFSFile();
$this->assertEquals(0, $file->getSize());
}
private function getMockMongoGridFSFile()
{
return $this->getMockBuilder('MongoGridFSFile')
->disableOriginalConstructor()
->getMock();
}
}