Repository navigation
Expand file tree
/
Copy pathMongoDb.php
More file actions
214 lines (190 loc) · 6.46 KB
/
Copy pathMongoDb.php
File metadata and controls
214 lines (190 loc) · 6.46 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace Codeception\Module;
/**
* Works with MongoDb database.
*
* The most important function of this module is cleaning database before each test.
* To have your database properly cleaned you should configure it to access the database.
*
* In order to have your database populated with data you need a valid js file with data (of the same style which can be fed up to mongo binary)
* File can be generated by RockMongo export command
* Just put it in ``` tests/_data ``` dir (by default) and specify path to it in config.
* Next time after database is cleared all your data will be restored from dump.
* The DB preparation should as following:
* - clean database
* - system collection system.users should contain the user which will be authenticated while script performs DB operations
*
* Connection is done by MongoDb driver, which is stored in Codeception\Util\Driver namespace.
* Check out the driver if you get problems loading dumps and cleaning databases.
*
* ## Status
*
* * Maintainer: **judgedim**, **davert**
* * Stability: **beta**
* * Contact: codecept@davert.mail.ua
*
* *Please review the code of non-stable modules and provide patches if you have issues.*
*
* ## Config
*
* * dsn *required* - MongoDb DSN with the db name specified at the end of the host after slash
* * user *required* - user to access database
* * password *required* - password
* * dump - path to database dump
* * populate: true - should the dump be loaded before test suite is started.
* * cleanup: true - should the dump be reloaded after each test
*
*/
use \Codeception\Util\Driver\MongoDb as MongoDbDriver;
class MongoDb extends \Codeception\Module
{
/**
* @api
* @var
*/
public $dbh;
/**
* @var
*/
protected $dumpFile;
protected $isDumpFileEmpty = true;
protected $config = array(
'populate' => true,
'cleanup' => true,
'dump' => null);
protected $populated = false;
/**
* @var \Codeception\Util\Driver\MongoDb
*/
public $driver;
protected $requiredFields = array('dsn', 'user', 'password');
public function _initialize()
{
if ($this->config['dump'] && ($this->config['cleanup'] or ($this->config['populate']))) {
if (!file_exists(getcwd() . DIRECTORY_SEPARATOR . $this->config['dump'])) {
throw new \Codeception\Exception\ModuleConfig(__CLASS__, "
File with dump doesn't exist.\n
Please, check path for dump file: " . $this->config['dump']);
}
$this->dumpFile = getcwd() . DIRECTORY_SEPARATOR . $this->config['dump'];
$this->isDumpFileEmpty = false;
$content = file_get_contents($this->dumpFile);
$content = trim(preg_replace('%/\*(?:(?!\*/).)*\*/%s', "", $content));
if (!sizeof(explode("\n", $content))) {
$this->isDumpFileEmpty = true;
}
}
try {
$this->driver = MongoDbDriver::create($this->config['dsn'], $this->config['user'], $this->config['password']);
} catch (\MongoConnectionException $e) {
throw new \Codeception\Exception\Module(__CLASS__, $e->getMessage() . ' while creating Mongo connection');
}
// starting with loading dump
if ($this->config['populate']) {
$this->cleanup();
$this->loadDump();
$this->populated = true;
}
}
public function _before(\Codeception\TestCase $test)
{
if ($this->config['cleanup'] && !$this->populated) {
$this->cleanup();
$this->loadDump();
}
}
public function _after(\Codeception\TestCase $test)
{
$this->populated = false;
}
protected function cleanup()
{
$dbh = $this->driver->getDbh();
if (!$dbh) {
throw new \Codeception\Exception\ModuleConfig(__CLASS__, "No connection to database. Remove this module from config if you don't need database repopulation");
}
try {
$this->driver->cleanup();
} catch (\Exception $e) {
throw new \Codeception\Exception\Module(__CLASS__, $e->getMessage());
}
}
protected function loadDump()
{
if ($this->isDumpFileEmpty) {
return;
}
try {
$this->driver->load($this->dumpFile);
} catch (\Exception $e) {
throw new \Codeception\Exception\Module(__CLASS__, $e->getMessage());
}
}
/**
* Inserts data into collection
*
* ``` php
* $I->haveInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com'));
* $user_id = $I->haveInCollection('users', array('email' => 'john@coltrane.com'));
* ```
*
* @param $collection
* @param array $data
*/
public function haveInCollection($collection, array $data)
{
$collection = $this->driver->getDbh()->selectCollection($collection);
$collection->insert($data);
return $data['_id'];
}
/**
* Checks if collection contains an item.
*
* ``` php
* <?php
* $I->seeInCollection('users', array('name' => 'miles'));
* ```
*
* @param $collection
* @param array $criteria
*/
public function seeInCollection($collection, $criteria = array())
{
$collection = $this->driver->getDbh()->selectCollection($collection);
$res = $collection->count($criteria);
\PHPUnit_Framework_Assert::assertGreaterThan(0, $res);
}
/**
* Checks if collection doesn't contain an item.
*
* ``` php
* <?php
* $I->dontSeeInCollection('users', array('name' => 'miles'));
* ```
*
* @param $collection
* @param array $criteria
*/
public function dontSeeInCollection($collection, $criteria = array())
{
$collection = $this->driver->getDbh()->selectCollection($collection);
$res = $collection->count($criteria);
\PHPUnit_Framework_Assert::assertLessThan(1, $res);
}
/**
* Grabs a data from collection
*
* ``` php
* <?php
* $cursor = $I->grabFromCollection('users', array('name' => 'miles'));
* ```
*
* @param $collection
* @param array $criteria
* @return \MongoCursor
*/
public function grabFromCollection($collection, $criteria = array()) {
$collection = $this->driver->getDbh()->selectCollection($collection);
return $collection->findOne($criteria);
}
}