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
<?php
namespace yiiunit\extensions\mongodb;
use yii\helpers\FileHelper;
use yii\mongodb\Connection;
use Yii;
use yii\mongodb\Exception;
use yiiunit\TestCase;
class MongoDbTestCase extends TestCase
{
/**
* @var array Mongo connection configuration.
*/
protected $mongoDbConfig = [
'dsn' => 'mongodb://localhost:27017',
'defaultDatabaseName' => 'yii2test',
'options' => [],
];
/**
* @var Connection Mongo connection instance.
*/
protected $mongodb;
public static function setUpBeforeClass()
{
static::loadClassMap();
}
protected function setUp()
{
parent::setUp();
if (!extension_loaded('mongo')) {
$this->markTestSkipped('mongo extension required.');
}
$config = $this->getParam('mongodb');
if (!empty($config)) {
$this->mongoDbConfig = $config;
}
$this->mockApplication();
static::loadClassMap();
}
protected function tearDown()
{
if ($this->mongodb) {
$this->mongodb->close();
}
$this->destroyApplication();
}
/**
* Adds sphinx extension files to [[Yii::$classPath]],
* avoiding the necessity of usage Composer autoloader.
*/
protected static function loadClassMap()
{
$baseNameSpace = 'yii/mongodb';
$basePath = realpath(__DIR__. '/../../../../extensions/yii/mongodb');
$files = FileHelper::findFiles($basePath);
foreach ($files as $file) {
$classRelativePath = str_replace($basePath, '', $file);
$classFullName = str_replace(['/', '.php'], ['\\', ''], $baseNameSpace . $classRelativePath);
Yii::$classMap[$classFullName] = $file;
}
}
/**
* @param boolean $reset whether to clean up the test database
* @param boolean $open whether to open test database
* @return \yii\mongodb\Connection
*/
public function getConnection($reset = false, $open = true)
{
if (!$reset && $this->mongodb) {
return $this->mongodb;
}
$db = new Connection;
$db->dsn = $this->mongoDbConfig['dsn'];
$db->defaultDatabaseName = $this->mongoDbConfig['defaultDatabaseName'];
if (isset($this->mongoDbConfig['options'])) {
$db->options = $this->mongoDbConfig['options'];
}
if ($open) {
$db->open();
}
$this->mongodb = $db;
return $db;
}
/**
* Drops the specified collection.
* @param string $name collection name.
*/
protected function dropCollection($name)
{
if ($this->mongodb) {
try {
$this->mongodb->getCollection($name)->drop();
} catch (Exception $e) {
// shut down exception
}
}
}
/**
* Drops the specified file collection.
* @param string $name file collection name.
*/
protected function dropFileCollection($name = 'fs')
{
if ($this->mongodb) {
try {
$this->mongodb->getFileCollection($name)->drop();
} catch (Exception $e) {
// shut down exception
}
}
}
/**
* Finds all records in collection.
* @param \yii\mongodb\Collection $collection
* @param array $condition
* @param array $fields
* @return array rows
*/
protected function findAll($collection, $condition = [], $fields = [])
{
$cursor = $collection->find($condition, $fields);
$result = [];
foreach ($cursor as $data) {
$result[] = $data;
}
return $result;
}
/**
* Returns the Mongo server version.
* @return string Mongo server version.
*/
protected function getServerVersion()
{
$connection = $this->getConnection();
$buildInfo = $connection->getDatabase()->executeCommand(['buildinfo' => true]);
return $buildInfo['version'];
}
}