CollectionTest.php 7.33 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

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
	/**
	 * @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);
229
		$rows = $this->findAll($outputCollection);
230 231 232 233 234 235 236 237 238 239 240 241 242
		$expectedRows = [
			[
				'_id' => 1,
				'value' => 300,
			],
			[
				'_id' => 2,
				'value' => 400,
			],
		];
		$this->assertEquals($expectedRows, $rows);
	}

243 244 245 246 247 248 249 250
	public function testCreateIndex()
	{
		$collection = $this->getConnection()->getCollection('customer');
		$columns = [
			'name',
			'status' => \MongoCollection::DESCENDING,
		];
		$this->assertTrue($collection->createIndex($columns));
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
		$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));

267
		$this->setExpectedException('\yii\mongodb\Exception');
268 269 270 271 272 273 274 275 276 277
		$collection->dropIndex('name');
	}

	/**
	 * @depends testCreateIndex
	 */
	public function testDropAllIndexes()
	{
		$collection = $this->getConnection()->getCollection('customer');
		$collection->createIndex('name');
278
		$this->assertEquals(2, $collection->dropAllIndexes());
279 280
		$indexInfo = $collection->mongoCollection->getIndexInfo();
		$this->assertEquals(1, count($indexInfo));
281
	}
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

	/**
	 * @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,
			],
306 307 308 309 310
			[
				'name' => 'no search keyword',
				'status' => 1,
				'amount' => 200,
			],
311 312 313 314
		];
		$collection->batchInsert($rows);
		$collection->createIndex(['name' => 'text']);

315
		$result = $collection->fullTextSearch('customer');
316
		$this->assertNotEmpty($result);
317
		$this->assertCount(2, $result);
318
	}
Paul Klimov committed
319
}