CollectionTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. namespace yiiunit\extensions\mongodb;
  3. use MongoDB\BSON\ObjectID;
  4. use MongoDB\Driver\Cursor;
  5. class CollectionTest extends TestCase
  6. {
  7. protected function tearDown()
  8. {
  9. $this->dropCollection('customer');
  10. $this->dropCollection('mapReduceOut');
  11. parent::tearDown();
  12. }
  13. // Tests :
  14. public function testGetName()
  15. {
  16. $collectionName = 'customer';
  17. $collection = $this->getConnection()->getCollection($collectionName);
  18. $this->assertEquals($collectionName, $collection->name);
  19. $this->assertEquals($this->getConnection()->getDefaultDatabaseName() . '.' . $collectionName, $collection->getFullName());
  20. }
  21. public function testFind()
  22. {
  23. $collection = $this->getConnection()->getCollection('customer');
  24. $cursor = $collection->find();
  25. $this->assertTrue($cursor instanceof Cursor);
  26. }
  27. public function testInsert()
  28. {
  29. $collection = $this->getConnection()->getCollection('customer');
  30. $data = [
  31. 'name' => 'customer 1',
  32. 'address' => 'customer 1 address',
  33. ];
  34. $id = $collection->insert($data);
  35. $this->assertTrue($id instanceof ObjectID);
  36. $this->assertNotEmpty($id->__toString());
  37. }
  38. /**
  39. * @depends testInsert
  40. * @depends testFind
  41. */
  42. public function testFindOne()
  43. {
  44. $collection = $this->getConnection()->getCollection('customer');
  45. $data = [
  46. 'name' => 'customer 1',
  47. 'address' => 'customer 1 address',
  48. ];
  49. $id = $collection->insert($data);
  50. $row = $collection->findOne(['_id' => $id]);
  51. $this->assertEquals($data['name'], $row['name']);
  52. $row = $collection->findOne(['_id' => 'unexisting-id']);
  53. $this->assertNull($row);
  54. }
  55. /**
  56. * @depends testInsert
  57. * @depends testFind
  58. */
  59. public function testFindAll()
  60. {
  61. $collection = $this->getConnection()->getCollection('customer');
  62. $data = [
  63. 'name' => 'customer 1',
  64. 'address' => 'customer 1 address',
  65. ];
  66. $id = $collection->insert($data);
  67. $cursor = $collection->find();
  68. $rows = [];
  69. foreach ($cursor as $row) {
  70. $rows[] = $row;
  71. }
  72. $this->assertEquals(1, count($rows));
  73. $this->assertEquals($id, $rows[0]['_id']);
  74. }
  75. /**
  76. * @depends testFind
  77. */
  78. public function testBatchInsert()
  79. {
  80. $collection = $this->getConnection()->getCollection('customer');
  81. $rows = [
  82. [
  83. 'name' => 'customer 1',
  84. 'address' => 'customer 1 address',
  85. ],
  86. [
  87. 'name' => 'customer 2',
  88. 'address' => 'customer 2 address',
  89. ],
  90. ];
  91. $insertedRows = $collection->batchInsert($rows);
  92. $this->assertTrue($insertedRows[0]['_id'] instanceof ObjectID);
  93. $this->assertTrue($insertedRows[1]['_id'] instanceof ObjectID);
  94. $this->assertCount(count($rows), $collection->find()->toArray());
  95. }
  96. public function testSave()
  97. {
  98. $collection = $this->getConnection()->getCollection('customer');
  99. $data = [
  100. 'name' => 'customer 1',
  101. 'address' => 'customer 1 address',
  102. ];
  103. $id = $collection->save($data);
  104. $this->assertTrue($id instanceof ObjectID);
  105. $this->assertNotEmpty($id->__toString());
  106. }
  107. /**
  108. * @depends testSave
  109. */
  110. public function testUpdateBySave()
  111. {
  112. $collection = $this->getConnection()->getCollection('customer');
  113. $data = [
  114. 'name' => 'customer 1',
  115. 'address' => 'customer 1 address',
  116. ];
  117. $newId = $collection->save($data);
  118. $data['_id'] = $newId;
  119. $updatedId = $collection->save($data);
  120. $this->assertEquals($newId, $updatedId, 'Unable to update data!');
  121. $data['_id'] = $newId->__toString();
  122. $updatedId = $collection->save($data);
  123. $this->assertEquals($newId, $updatedId, 'Unable to updated data by string id!');
  124. }
  125. /**
  126. * @depends testFindAll
  127. */
  128. public function testRemove()
  129. {
  130. $collection = $this->getConnection()->getCollection('customer');
  131. $data = [
  132. 'name' => 'customer 1',
  133. 'address' => 'customer 1 address',
  134. ];
  135. $id = $collection->insert($data);
  136. $count = $collection->remove(['_id' => $id]);
  137. $this->assertEquals(1, $count);
  138. $rows = $this->findAll($collection);
  139. $this->assertEquals(0, count($rows));
  140. }
  141. /**
  142. * @depends testBatchInsert
  143. * @depends testRemove
  144. */
  145. public function testRemoveComplexCondition()
  146. {
  147. $collection = $this->getConnection()->getCollection('customer');
  148. $collection->batchInsert([
  149. [
  150. 'name' => 'customer 1',
  151. 'status' => 1,
  152. ],
  153. [
  154. 'name' => 'customer 2',
  155. 'status' => 2,
  156. ],
  157. [
  158. 'name' => 'customer 3',
  159. 'status' => 3,
  160. ],
  161. ]);
  162. $count = $collection->remove(['status' => [1, 3]]);
  163. $this->assertEquals(2, $count);
  164. $rows = $this->findAll($collection);
  165. $this->assertEquals(1, count($rows));
  166. }
  167. /**
  168. * @depends testFindAll
  169. */
  170. public function testUpdate()
  171. {
  172. $collection = $this->getConnection()->getCollection('customer');
  173. $data = [
  174. 'name' => 'customer 1',
  175. 'address' => 'customer 1 address',
  176. ];
  177. $id = $collection->insert($data);
  178. $newData = [
  179. 'name' => 'new name'
  180. ];
  181. $count = $collection->update(['_id' => $id], $newData);
  182. $this->assertEquals(1, $count);
  183. list($row) = $this->findAll($collection);
  184. $this->assertEquals($newData['name'], $row['name']);
  185. }
  186. /**
  187. * @depends testBatchInsert
  188. */
  189. public function testGroup()
  190. {
  191. $collection = $this->getConnection()->getCollection('customer');
  192. $rows = [
  193. [
  194. 'name' => 'customer 1',
  195. 'address' => 'customer 1 address',
  196. ],
  197. [
  198. 'name' => 'customer 2',
  199. 'address' => 'customer 2 address',
  200. ],
  201. ];
  202. $collection->batchInsert($rows);
  203. $keys = ['address' => 1];
  204. $initial = ['items' => []];
  205. $reduce = "function (obj, prev) { prev.items.push(obj.name); }";
  206. $result = $collection->group($keys, $initial, $reduce);
  207. $this->assertEquals(2, count($result));
  208. $this->assertNotEmpty($result[0]['address']);
  209. $this->assertNotEmpty($result[0]['items']);
  210. }
  211. public function testFindAndModify()
  212. {
  213. $collection = $this->getConnection()->getCollection('customer');
  214. $rows = [
  215. [
  216. 'name' => 'customer 1',
  217. 'status' => 1,
  218. 'amount' => 100,
  219. ],
  220. [
  221. 'name' => 'customer 2',
  222. 'status' => 1,
  223. 'amount' => 200,
  224. ],
  225. ];
  226. $collection->batchInsert($rows);
  227. // increment field
  228. $result = $collection->findAndModify(['name' => 'customer 1'], ['$inc' => ['status' => 1]]);
  229. $this->assertEquals('customer 1', $result['name']);
  230. $this->assertEquals(1, $result['status']);
  231. $newResult = $collection->findOne(['name' => 'customer 1']);
  232. $this->assertEquals(2, $newResult['status']);
  233. // $set and return modified document
  234. $result = $collection->findAndModify(
  235. ['name' => 'customer 2'],
  236. ['$set' => ['status' => 2]],
  237. ['new' => true]
  238. );
  239. $this->assertEquals('customer 2', $result['name']);
  240. $this->assertEquals(2, $result['status']);
  241. // Full update document
  242. $data = [
  243. 'name' => 'customer 3',
  244. 'city' => 'Minsk'
  245. ];
  246. $result = $collection->findAndModify(
  247. ['name' => 'customer 2'],
  248. $data,
  249. ['new' => true]
  250. );
  251. $this->assertEquals('customer 3', $result['name']);
  252. $this->assertEquals('Minsk', $result['city']);
  253. $this->assertTrue(!isset($result['status']));
  254. // Test exceptions
  255. $this->expectException('\yii\mongodb\Exception');
  256. $collection->findAndModify(['name' => 'customer 1'], ['$wrongOperator' => ['status' => 1]]);
  257. }
  258. /**
  259. * @depends testBatchInsert
  260. */
  261. public function testMapReduce()
  262. {
  263. $collection = $this->getConnection()->getCollection('customer');
  264. $rows = [
  265. [
  266. 'name' => 'customer 1',
  267. 'status' => 1,
  268. 'amount' => 100,
  269. ],
  270. [
  271. 'name' => 'customer 2',
  272. 'status' => 1,
  273. 'amount' => 200,
  274. ],
  275. [
  276. 'name' => 'customer 2',
  277. 'status' => 2,
  278. 'amount' => 400,
  279. ],
  280. [
  281. 'name' => 'customer 2',
  282. 'status' => 3,
  283. 'amount' => 500,
  284. ],
  285. ];
  286. $collection->batchInsert($rows);
  287. $result = $collection->mapReduce(
  288. 'function () {emit(this.status, this.amount)}',
  289. 'function (key, values) {return Array.sum(values)}',
  290. 'mapReduceOut',
  291. ['status' => ['$lt' => 3]]
  292. );
  293. $this->assertEquals('mapReduceOut', $result);
  294. $outputCollection = $this->getConnection()->getCollection($result);
  295. $rows = $this->findAll($outputCollection);
  296. $expectedRows = [
  297. [
  298. '_id' => 1,
  299. 'value' => 300,
  300. ],
  301. [
  302. '_id' => 2,
  303. 'value' => 400,
  304. ],
  305. ];
  306. $this->assertEquals($expectedRows, $rows);
  307. }
  308. /**
  309. * @depends testMapReduce
  310. */
  311. public function testMapReduceInline()
  312. {
  313. $collection = $this->getConnection()->getCollection('customer');
  314. $rows = [
  315. [
  316. 'name' => 'customer 1',
  317. 'status' => 1,
  318. 'amount' => 100,
  319. ],
  320. [
  321. 'name' => 'customer 2',
  322. 'status' => 1,
  323. 'amount' => 200,
  324. ],
  325. [
  326. 'name' => 'customer 2',
  327. 'status' => 2,
  328. 'amount' => 400,
  329. ],
  330. [
  331. 'name' => 'customer 2',
  332. 'status' => 3,
  333. 'amount' => 500,
  334. ],
  335. ];
  336. $collection->batchInsert($rows);
  337. $result = $collection->mapReduce(
  338. 'function () {emit(this.status, this.amount)}',
  339. 'function (key, values) {return Array.sum(values)}',
  340. ['inline' => true],
  341. ['status' => ['$lt' => 3]]
  342. );
  343. $expectedRows = [
  344. [
  345. '_id' => 1,
  346. 'value' => 300,
  347. ],
  348. [
  349. '_id' => 2,
  350. 'value' => 400,
  351. ],
  352. ];
  353. $this->assertEquals($expectedRows, $result);
  354. }
  355. public function testCreateIndex()
  356. {
  357. $collection = $this->getConnection()->getCollection('customer');
  358. $columns = [
  359. 'name',
  360. 'status' => SORT_DESC
  361. ];
  362. $this->assertTrue($collection->createIndex($columns));
  363. $indexInfo = $collection->listIndexes();
  364. $this->assertEquals(2, count($indexInfo));
  365. }
  366. /**
  367. * @depends testCreateIndex
  368. */
  369. public function testDropIndex()
  370. {
  371. $collection = $this->getConnection()->getCollection('customer');
  372. $collection->createIndex('name');
  373. $this->assertTrue($collection->dropIndex('name'));
  374. $indexInfo = $collection->listIndexes();
  375. $this->assertEquals(1, count($indexInfo));
  376. $this->expectException('\yii\mongodb\Exception');
  377. $collection->dropIndex('name');
  378. }
  379. /**
  380. * @depends testDropIndex
  381. * @see https://github.com/yiisoft/yii2-mongodb/issues/247
  382. */
  383. public function testDropIndexWithPlugin()
  384. {
  385. $collection = $this->getConnection()->getCollection('customer');
  386. $columns = [
  387. 'name' => 'text'
  388. ];
  389. $collection->createIndex($columns);
  390. $this->assertTrue($collection->dropIndex($columns));
  391. $indexInfo = $collection->listIndexes();
  392. $this->assertEquals(1, count($indexInfo));
  393. }
  394. /**
  395. * @depends testCreateIndex
  396. */
  397. public function testDropAllIndexes()
  398. {
  399. $collection = $this->getConnection()->getCollection('customer');
  400. $collection->createIndex('name');
  401. $this->assertEquals(2, $collection->dropAllIndexes());
  402. $indexInfo = $collection->listIndexes();
  403. $this->assertEquals(1, count($indexInfo));
  404. }
  405. public function testCreateIndexes()
  406. {
  407. $collection = $this->getConnection()->getCollection('customer');
  408. $columns = [
  409. ['key' => ['name']],
  410. ['key' => ['status' => SORT_DESC]]
  411. ];
  412. $this->assertTrue($collection->createIndexes($columns));
  413. $indexInfo = $collection->listIndexes();
  414. $this->assertEquals(3, count($indexInfo));
  415. }
  416. /**
  417. * @depends testCreateIndexes
  418. */
  419. public function testDropIndexes()
  420. {
  421. $collection = $this->getConnection()->getCollection('customer');
  422. $columns = [
  423. [
  424. 'key' => ['name'],
  425. 'name' => 'test_index'
  426. ],
  427. [
  428. 'key' => ['status'],
  429. 'name' => 'to_be_dropped'
  430. ],
  431. ];
  432. $collection->createIndexes($columns);
  433. $collection->dropIndexes('to_be_dropped');
  434. $indexInfo = $collection->listIndexes();
  435. $this->assertEquals(2, count($indexInfo));
  436. }
  437. /**
  438. * @depends testInsert
  439. * @depends testFind
  440. */
  441. public function testFindByNotObjectId()
  442. {
  443. $collection = $this->getConnection()->getCollection('customer');
  444. $data = [
  445. 'name' => 'customer 1',
  446. 'address' => 'customer 1 address',
  447. ];
  448. $id = $collection->insert($data);
  449. $cursor = $collection->find(['_id' => (string) $id]);
  450. $this->assertTrue($cursor instanceof Cursor);
  451. $row = current($cursor->toArray());
  452. $this->assertEquals($id, $row['_id']);
  453. $cursor = $collection->find(['_id' => 'fake']);
  454. $this->assertTrue($cursor instanceof Cursor);
  455. $this->assertCount(0, $cursor->toArray());
  456. }
  457. /**
  458. * @depends testInsert
  459. *
  460. * @see https://github.com/yiisoft/yii2/issues/2548
  461. */
  462. public function testInsertMongoBin()
  463. {
  464. $collection = $this->getConnection()->getCollection('customer');
  465. $fileName = __FILE__;
  466. $data = [
  467. 'name' => 'customer 1',
  468. 'address' => 'customer 1 address',
  469. 'binData' => new \MongoDB\BSON\Binary(file_get_contents($fileName), 2),
  470. ];
  471. $id = $collection->insert($data);
  472. $this->assertTrue($id instanceof ObjectID);
  473. $this->assertNotEmpty($id->__toString());
  474. }
  475. /**
  476. * @depends testBatchInsert
  477. */
  478. public function testDistinct()
  479. {
  480. $collection = $this->getConnection()->getCollection('customer');
  481. $rows = [
  482. [
  483. 'name' => 'customer 1',
  484. 'status' => 1,
  485. ],
  486. [
  487. 'name' => 'customer 1',
  488. 'status' => 1,
  489. ],
  490. [
  491. 'name' => 'customer 3',
  492. 'status' => 2,
  493. ],
  494. ];
  495. $collection->batchInsert($rows);
  496. $rows = $collection->distinct('status');
  497. $this->assertFalse($rows === false);
  498. $this->assertCount(2, $rows);
  499. $rows = $collection->distinct('status', ['status' => 1]);
  500. $this->assertFalse($rows === false);
  501. $this->assertCount(1, $rows);
  502. }
  503. }