CollectionTest.php 9.6 KB
Newer Older
Paul Klimov committed
1 2
<?php

3
namespace yiiunit\extensions\mongodb;
Paul Klimov committed
4 5

/**
6
 * @group mongodb
Paul Klimov committed
7
 */
8
class CollectionTest extends MongoDbTestCase
Paul Klimov committed
9 10 11 12
{
	protected function tearDown()
	{
		$this->dropCollection('customer');
13
		$this->dropCollection('mapReduceOut');
Paul Klimov committed
14 15 16 17 18
		parent::tearDown();
	}

	// Tests :

19 20 21 22 23
	public function testGetName()
	{
		$collectionName = 'customer';
		$collection = $this->getConnection()->getCollection($collectionName);
		$this->assertEquals($collectionName, $collection->getName());
24
		$this->assertEquals($this->mongoDbConfig['defaultDatabaseName'] . '.' . $collectionName, $collection->getFullName());
25 26
	}

27 28 29 30 31 32 33
	public function testFind()
	{
		$collection = $this->getConnection()->getCollection('customer');
		$cursor = $collection->find();
		$this->assertTrue($cursor instanceof \MongoCursor);
	}

Paul Klimov committed
34 35
	public function testInsert()
	{
36
		$collection = $this->getConnection()->getCollection('customer');
Paul Klimov committed
37 38 39 40
		$data = [
			'name' => 'customer 1',
			'address' => 'customer 1 address',
		];
41
		$id = $collection->insert($data);
Paul Klimov committed
42 43 44 45 46 47
		$this->assertTrue($id instanceof \MongoId);
		$this->assertNotEmpty($id->__toString());
	}

	/**
	 * @depends testInsert
48
	 * @depends testFind
Paul Klimov committed
49 50 51
	 */
	public function testFindAll()
	{
52
		$collection = $this->getConnection()->getCollection('customer');
Paul Klimov committed
53 54 55 56
		$data = [
			'name' => 'customer 1',
			'address' => 'customer 1 address',
		];
57
		$id = $collection->insert($data);
Paul Klimov committed
58

59 60 61 62 63
		$cursor = $collection->find();
		$rows = [];
		foreach ($cursor as $row) {
			$rows[] = $row;
		}
Paul Klimov committed
64 65 66 67
		$this->assertEquals(1, count($rows));
		$this->assertEquals($id, $rows[0]['_id']);
	}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	/**
	 * @depends testFind
	 */
	public function testBatchInsert()
	{
		$collection = $this->getConnection()->getCollection('customer');
		$rows = [
			[
				'name' => 'customer 1',
				'address' => 'customer 1 address',
			],
			[
				'name' => 'customer 2',
				'address' => 'customer 2 address',
			],
		];
		$insertedRows = $collection->batchInsert($rows);
		$this->assertTrue($insertedRows[0]['_id'] instanceof \MongoId);
		$this->assertTrue($insertedRows[1]['_id'] instanceof \MongoId);
		$this->assertEquals(count($rows), $collection->find()->count());
	}

Paul Klimov committed
90 91
	public function testSave()
	{
92
		$collection = $this->getConnection()->getCollection('customer');
Paul Klimov committed
93 94 95 96
		$data = [
			'name' => 'customer 1',
			'address' => 'customer 1 address',
		];
97
		$id = $collection->save($data);
Paul Klimov committed
98 99 100 101 102 103 104
		$this->assertTrue($id instanceof \MongoId);
		$this->assertNotEmpty($id->__toString());
	}

	/**
	 * @depends testSave
	 */
105
	public function testUpdateBySave()
Paul Klimov committed
106
	{
107
		$collection = $this->getConnection()->getCollection('customer');
Paul Klimov committed
108 109 110 111
		$data = [
			'name' => 'customer 1',
			'address' => 'customer 1 address',
		];
112
		$newId = $collection->save($data);
Paul Klimov committed
113

114
		$updatedId = $collection->save($data);
Paul Klimov committed
115 116 117
		$this->assertEquals($newId, $updatedId, 'Unable to update data!');

		$data['_id'] = $newId->__toString();
118
		$updatedId = $collection->save($data);
Paul Klimov committed
119 120 121 122 123 124 125 126
		$this->assertEquals($newId, $updatedId, 'Unable to updated data by string id!');
	}

	/**
	 * @depends testFindAll
	 */
	public function testRemove()
	{
127
		$collection = $this->getConnection()->getCollection('customer');
Paul Klimov committed
128 129 130 131
		$data = [
			'name' => 'customer 1',
			'address' => 'customer 1 address',
		];
132
		$id = $collection->insert($data);
Paul Klimov committed
133

134 135
		$count = $collection->remove(['_id' => $id]);
		$this->assertEquals(1, $count);
Paul Klimov committed
136

137
		$rows = $this->findAll($collection);
Paul Klimov committed
138 139
		$this->assertEquals(0, count($rows));
	}
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

	/**
	 * @depends testFindAll
	 */
	public function testUpdate()
	{
		$collection = $this->getConnection()->getCollection('customer');
		$data = [
			'name' => 'customer 1',
			'address' => 'customer 1 address',
		];
		$id = $collection->insert($data);

		$newData = [
			'name' => 'new name'
		];
156 157
		$count = $collection->update(['_id' => $id], $newData);
		$this->assertEquals(1, $count);
158

159
		list($row) = $this->findAll($collection);
160 161
		$this->assertEquals($newData['name'], $row['name']);
	}
162 163 164 165

	/**
	 * @depends testBatchInsert
	 */
166
	public function testGroup()
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	{
		$collection = $this->getConnection()->getCollection('customer');
		$rows = [
			[
				'name' => 'customer 1',
				'address' => 'customer 1 address',
			],
			[
				'name' => 'customer 2',
				'address' => 'customer 2 address',
			],
		];
		$collection->batchInsert($rows);

		$keys = ['address' => 1];
		$initial = ['items' => []];
		$reduce = "function (obj, prev) { prev.items.push(obj.name); }";
184
		$result = $collection->group($keys, $initial, $reduce);
185 186 187 188
		$this->assertEquals(2, count($result));
		$this->assertNotEmpty($result[0]['address']);
		$this->assertNotEmpty($result[0]['items']);
	}
189

Alexander Mohorev committed
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	public function testFindAndModify()
	{
		$collection = $this->getConnection()->getCollection('customer');
		$rows = [
			[
				'name' => 'customer 1',
				'status' => 1,
				'amount' => 100,
			],
			[
				'name' => 'customer 2',
				'status' => 1,
				'amount' => 200,
			],
		];
		$collection->batchInsert($rows);

		// increment field
		$result = $collection->findAndModify(['name' => 'customer 1'], ['$inc' => ['status' => 1]]);
		$this->assertEquals('customer 1', $result['name']);
		$this->assertEquals(1, $result['status']);
		$newResult = $collection->findOne(['name' => 'customer 1']);
		$this->assertEquals(2, $newResult['status']);

		// $set and return modified document
		$result = $collection->findAndModify(
			['name' => 'customer 2'],
			['$set' => ['status' => 2]],
			[],
			['new' => true]
		);
		$this->assertEquals('customer 2', $result['name']);
		$this->assertEquals(2, $result['status']);

		// Full update document
		$data = [
			'name' => 'customer 3',
			'city' => 'Minsk'
		];
		$result = $collection->findAndModify(
			['name' => 'customer 2'],
			$data,
			[],
			['new' => true]
		);
		$this->assertEquals('customer 3', $result['name']);
		$this->assertEquals('Minsk', $result['city']);
		$this->assertTrue(!isset($result['status']));

		// Test exceptions
		$this->setExpectedException('\yii\mongodb\Exception');
		$collection->findAndModify(['name' => 'customer 1'], ['$wrongOperator' => ['status' => 1]]);
	}

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	/**
	 * @depends testBatchInsert
	 */
	public function testMapReduce()
	{
		$collection = $this->getConnection()->getCollection('customer');
		$rows = [
			[
				'name' => 'customer 1',
				'status' => 1,
				'amount' => 100,
			],
			[
				'name' => 'customer 2',
				'status' => 1,
				'amount' => 200,
			],
			[
				'name' => 'customer 2',
				'status' => 2,
				'amount' => 400,
			],
			[
				'name' => 'customer 2',
				'status' => 3,
				'amount' => 500,
			],
		];
		$collection->batchInsert($rows);

		$result = $collection->mapReduce(
			'function () {emit(this.status, this.amount)}',
			'function (key, values) {return Array.sum(values)}',
			'mapReduceOut',
			['status' => ['$lt' => 3]]
		);
		$this->assertEquals('mapReduceOut', $result);

		$outputCollection = $this->getConnection()->getCollection($result);
283
		$rows = $this->findAll($outputCollection);
284 285 286 287 288 289 290 291 292 293 294 295 296
		$expectedRows = [
			[
				'_id' => 1,
				'value' => 300,
			],
			[
				'_id' => 2,
				'value' => 400,
			],
		];
		$this->assertEquals($expectedRows, $rows);
	}

Alexander Mohorev committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	/**
	 * @depends testMapReduce
	 */
	public function testMapReduceInline()
	{
		$collection = $this->getConnection()->getCollection('customer');
		$rows = [
			[
				'name' => 'customer 1',
				'status' => 1,
				'amount' => 100,
			],
			[
				'name' => 'customer 2',
				'status' => 1,
				'amount' => 200,
			],
			[
				'name' => 'customer 2',
				'status' => 2,
				'amount' => 400,
			],
			[
				'name' => 'customer 2',
				'status' => 3,
				'amount' => 500,
			],
		];
		$collection->batchInsert($rows);

		$result = $collection->mapReduce(
			'function () {emit(this.status, this.amount)}',
			'function (key, values) {return Array.sum(values)}',
			['inline' => true],
			['status' => ['$lt' => 3]]
		);
		$expectedRows = [
			[
				'_id' => 1,
				'value' => 300,
			],
			[
				'_id' => 2,
				'value' => 400,
			],
		];
		$this->assertEquals($expectedRows, $result);
	}
345

346 347 348 349 350 351 352 353
	public function testCreateIndex()
	{
		$collection = $this->getConnection()->getCollection('customer');
		$columns = [
			'name',
			'status' => \MongoCollection::DESCENDING,
		];
		$this->assertTrue($collection->createIndex($columns));
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
		$indexInfo = $collection->mongoCollection->getIndexInfo();
		$this->assertEquals(2, count($indexInfo));
	}

	/**
	 * @depends testCreateIndex
	 */
	public function testDropIndex()
	{
		$collection = $this->getConnection()->getCollection('customer');

		$collection->createIndex('name');
		$this->assertTrue($collection->dropIndex('name'));
		$indexInfo = $collection->mongoCollection->getIndexInfo();
		$this->assertEquals(1, count($indexInfo));

370
		$this->setExpectedException('\yii\mongodb\Exception');
371 372 373 374 375 376 377 378 379 380
		$collection->dropIndex('name');
	}

	/**
	 * @depends testCreateIndex
	 */
	public function testDropAllIndexes()
	{
		$collection = $this->getConnection()->getCollection('customer');
		$collection->createIndex('name');
381
		$this->assertEquals(2, $collection->dropAllIndexes());
382 383
		$indexInfo = $collection->mongoCollection->getIndexInfo();
		$this->assertEquals(1, count($indexInfo));
384
	}
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

	/**
	 * @depends testBatchInsert
	 * @depends testCreateIndex
	 */
	public function testFullTextSearch()
	{
		if (version_compare('2.4', $this->getServerVersion(), '>')) {
			$this->markTestSkipped("Mongo Server 2.4 required.");
		}

		$collection = $this->getConnection()->getCollection('customer');

		$rows = [
			[
				'name' => 'customer 1',
				'status' => 1,
				'amount' => 100,
			],
			[
				'name' => 'some customer',
				'status' => 1,
				'amount' => 200,
			],
409 410 411 412 413
			[
				'name' => 'no search keyword',
				'status' => 1,
				'amount' => 200,
			],
414 415 416 417
		];
		$collection->batchInsert($rows);
		$collection->createIndex(['name' => 'text']);

418
		$result = $collection->fullTextSearch('customer');
419
		$this->assertNotEmpty($result);
420
		$this->assertCount(2, $result);
421
	}
Paul Klimov committed
422
}