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
<?php
namespace yiiunit\extensions\mongodb\file;
use yiiunit\extensions\mongodb\MongoDbTestCase;
/**
* @group mongodb
*/
class CollectionTest extends MongoDbTestCase
{
protected function tearDown()
{
$this->dropFileCollection('fs');
parent::tearDown();
}
// Tests :
public function testGetChunkCollection()
{
$collection = $this->getConnection()->getFileCollection();
$chunkCollection = $collection->getChunkCollection();
$this->assertTrue($chunkCollection instanceof \yii\mongodb\Collection);
$this->assertTrue($chunkCollection->mongoCollection instanceof \MongoCollection);
}
public function testFind()
{
$collection = $this->getConnection()->getFileCollection();
$cursor = $collection->find();
$this->assertTrue($cursor instanceof \MongoGridFSCursor);
}
public function testInsertFile()
{
$collection = $this->getConnection()->getFileCollection();
$filename = __FILE__;
$id = $collection->insertFile($filename);
$this->assertTrue($id instanceof \MongoId);
$files = $this->findAll($collection);
$this->assertEquals(1, count($files));
/** @var $file \MongoGridFSFile */
$file = $files[0];
$this->assertEquals($filename, $file->getFilename());
$this->assertEquals(file_get_contents($filename), $file->getBytes());
}
public function testInsertFileContent()
{
$collection = $this->getConnection()->getFileCollection();
$bytes = 'Test file content';
$id = $collection->insertFileContent($bytes);
$this->assertTrue($id instanceof \MongoId);
$files = $this->findAll($collection);
$this->assertEquals(1, count($files));
/** @var $file \MongoGridFSFile */
$file = $files[0];
$this->assertEquals($bytes, $file->getBytes());
}
/**
* @depends testInsertFileContent
*/
public function testGet()
{
$collection = $this->getConnection()->getFileCollection();
$bytes = 'Test file content';
$id = $collection->insertFileContent($bytes);
$file = $collection->get($id);
$this->assertTrue($file instanceof \MongoGridFSFile);
$this->assertEquals($bytes, $file->getBytes());
}
/**
* @depends testGet
*/
public function testDelete()
{
$collection = $this->getConnection()->getFileCollection();
$bytes = 'Test file content';
$id = $collection->insertFileContent($bytes);
$this->assertTrue($collection->delete($id));
$file = $collection->get($id);
$this->assertNull($file);
}
}