MongoCollectionTest.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
  3. use MongoDB\Driver\ReadPreference;
  4. use Alcaeus\MongoDbAdapter\Tests\TestCase;
  5. /**
  6. * @author alcaeus <alcaeus@alcaeus.org>
  7. */
  8. class MongoCollectionTest extends TestCase
  9. {
  10. public function testSerialize()
  11. {
  12. $this->assertInternalType('string', serialize($this->getCollection()));
  13. }
  14. public function testGetNestedCollections()
  15. {
  16. $collection = $this->getCollection()->foo->bar;
  17. $this->assertSame('mongo-php-adapter.test.foo.bar', (string) $collection);
  18. }
  19. public function testCreateRecord()
  20. {
  21. $collection = $this->getCollection();
  22. $expected = [
  23. 'ok' => 1.0,
  24. 'n' => 0,
  25. 'err' => null,
  26. 'errmsg' => null,
  27. ];
  28. $document = ['foo' => 'bar'];
  29. $this->assertSame($expected, $collection->insert($document));
  30. $this->assertInstanceOf('MongoId', $document['_id']);
  31. $id = (string) $document['_id'];
  32. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  33. $this->assertSame(1, $newCollection->count());
  34. $object = $newCollection->findOne();
  35. $this->assertNotNull($object);
  36. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  37. $this->assertSame($id, (string) $object->_id);
  38. $this->assertObjectHasAttribute('foo', $object);
  39. $this->assertAttributeSame('bar', 'foo', $object);
  40. }
  41. public function testInsertInvalidData()
  42. {
  43. $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');
  44. $document = 8;
  45. $this->getCollection()->insert($document);
  46. }
  47. public function testInsertEmptyArray()
  48. {
  49. $document = [];
  50. $this->getCollection()->insert($document);
  51. $this->assertSame(1, $this->getCollection()->count());
  52. }
  53. public function testInsertArrayWithNumericKeys()
  54. {
  55. $document = [1 => 'foo'];
  56. $this->getCollection()->insert($document);
  57. $this->assertSame(1, $this->getCollection()->count(['_id' => $document['_id']]));
  58. }
  59. public function testInsertEmptyObject()
  60. {
  61. $document = (object) [];
  62. $this->getCollection()->insert($document);
  63. $this->assertSame(1, $this->getCollection()->count());
  64. }
  65. public function testInsertObjectWithPrivateProperties()
  66. {
  67. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  68. $document = new PrivatePropertiesStub();
  69. $this->getCollection()->insert($document);
  70. }
  71. public function testInsertWithInvalidKey()
  72. {
  73. $document = ['*' => 'foo'];
  74. $this->getCollection()->insert($document);
  75. $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
  76. }
  77. public function testInsertWithEmptyKey()
  78. {
  79. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  80. $document = ['' => 'foo'];
  81. $this->getCollection()->insert($document);
  82. }
  83. public function testInsertWithNumericKey()
  84. {
  85. $document = ['foo'];
  86. $this->getCollection()->insert($document);
  87. $this->assertSame(1, $this->getCollection()->count(['foo']));
  88. }
  89. public function testInsertDuplicate()
  90. {
  91. $collection = $this->getCollection();
  92. $collection->createIndex(['foo' => 1], ['unique' => true]);
  93. $document = ['foo' => 'bar'];
  94. $collection->insert($document);
  95. unset($document['_id']);
  96. $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter\.test/');
  97. $collection->insert($document);
  98. }
  99. public function testUnacknowledgedWrite()
  100. {
  101. $document = ['foo' => 'bar'];
  102. $this->assertTrue($this->getCollection()->insert($document, ['w' => 0]));
  103. }
  104. public function testInsertWriteConcernException()
  105. {
  106. $this->setExpectedException(
  107. 'MongoWriteConcernException',
  108. "cannot use 'w' > 1 when a host is not replicated"
  109. );
  110. $document = ['foo' => 'bar'];
  111. $this->getCollection()->insert($document, ['w' => 2]);
  112. }
  113. public function testInsertMany()
  114. {
  115. $expected = [
  116. 'ok' => 1.0,
  117. 'n' => 0,
  118. 'syncMillis' => 0,
  119. 'writtenTo' => null,
  120. 'err' => null,
  121. ];
  122. $documents = [
  123. ['foo' => 'bar'],
  124. ['bar' => 'foo']
  125. ];
  126. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  127. foreach ($documents as $document) {
  128. $this->assertInstanceOf('MongoId', $document['_id']);
  129. }
  130. }
  131. public function testInsertManyWithNonNumericKeys()
  132. {
  133. $expected = [
  134. 'ok' => 1.0,
  135. 'n' => 0,
  136. 'syncMillis' => 0,
  137. 'writtenTo' => null,
  138. 'err' => null,
  139. ];
  140. $documents = [
  141. 'a' => ['foo' => 'bar'],
  142. 'b' => ['bar' => 'foo']
  143. ];
  144. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  145. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  146. $this->assertSame(2, $newCollection->count());
  147. }
  148. public function testBatchInsertContinuesOnError()
  149. {
  150. $expected = [
  151. 'ok' => 1.0,
  152. 'n' => 0,
  153. 'syncMillis' => 0,
  154. 'writtenTo' => null,
  155. 'err' => null,
  156. ];
  157. $documents = [
  158. 8,
  159. 'b' => ['bar' => 'foo']
  160. ];
  161. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents, ['continueOnError' => true]));
  162. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  163. $this->assertSame(1, $newCollection->count());
  164. }
  165. public function testBatchInsertException()
  166. {
  167. $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter.test.*_id_/');
  168. $id = new \MongoId();
  169. $documents = [['_id' => $id, 'foo' => 'bar'], ['_id' => $id, 'foo' => 'bleh']];
  170. $this->getCollection()->batchInsert($documents);
  171. }
  172. public function testBatchInsertObjectWithPrivateProperties()
  173. {
  174. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  175. $documents = [new PrivatePropertiesStub()];
  176. $this->getCollection()->batchInsert($documents);
  177. }
  178. public function testBatchInsertWithInvalidKey()
  179. {
  180. $documents = [['*' => 'foo']];
  181. $this->getCollection()->batchInsert($documents);
  182. $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
  183. }
  184. public function testBatchInsertWithEmptyKey()
  185. {
  186. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  187. $documents = [['' => 'foo']];
  188. $this->getCollection()->batchInsert($documents);
  189. }
  190. public function testBatchInsertWithNumericKey()
  191. {
  192. $documents = [['foo']];
  193. $this->getCollection()->batchInsert($documents);
  194. $this->assertSame(1, $this->getCollection()->count(['foo']));
  195. }
  196. public function testBatchInsertEmptyBatchException()
  197. {
  198. $this->setExpectedException('MongoException', 'No write ops were included in the batch');
  199. $documents = [];
  200. $this->getCollection()->batchInsert($documents, ['w' => 2]);
  201. }
  202. public function testUpdateWriteConcern()
  203. {
  204. $this->setExpectedException('MongoWriteConcernException', "cannot use 'w' > 1 when a host is not replicated");
  205. $this->getCollection()->update([], ['$set' => ['foo' => 'bar']], ['w' => 2]);
  206. }
  207. public function testUpdateOne()
  208. {
  209. $document = ['foo' => 'bar'];
  210. $this->getCollection()->insert($document);
  211. // Unset ID to re-insert
  212. unset($document['_id']);
  213. $this->getCollection()->insert($document);
  214. $expected = [
  215. 'ok' => 1.0,
  216. 'nModified' => 1,
  217. 'n' => 1,
  218. 'err' => null,
  219. 'errmsg' => null,
  220. 'updatedExisting' => true,
  221. ];
  222. $result = $this->getCollection()->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  223. $this->assertSame($expected, $result);
  224. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  225. }
  226. public function testUpdateReplaceOne()
  227. {
  228. $document = ['foo' => 'bar', 'bar' => 'foo'];
  229. $this->getCollection()->insert($document);
  230. // Unset ID to re-insert
  231. unset($document['_id']);
  232. $this->getCollection()->insert($document);
  233. $expected = [
  234. 'ok' => 1.0,
  235. 'nModified' => 1,
  236. 'n' => 1,
  237. 'err' => null,
  238. 'errmsg' => null,
  239. 'updatedExisting' => true,
  240. ];
  241. $result = $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo']);
  242. $this->assertSame($expected, $result);
  243. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  244. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['bar' => 'foo']));
  245. }
  246. public function testUpdateReplaceMultiple()
  247. {
  248. $this->setExpectedExceptionRegExp('MongoWriteConcernException', '/multi update only works with \$ operators/', 9);
  249. $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo'], ['multiple' => true]);
  250. }
  251. public function testUpdateDuplicate()
  252. {
  253. $collection = $this->getCollection();
  254. $collection->createIndex(['foo' => 1], ['unique' => 1]);
  255. $document = ['foo' => 'bar'];
  256. $collection->insert($document);
  257. $document = ['foo' => 'foo'];
  258. $collection->insert($document);
  259. $this->setExpectedException('MongoDuplicateKeyException');
  260. $collection->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  261. }
  262. public function testUpdateMany()
  263. {
  264. $document = ['change' => true, 'foo' => 'bar'];
  265. $this->getCollection()->insert($document);
  266. unset($document['_id']);
  267. $this->getCollection()->insert($document);
  268. $document = ['change' => true, 'foo' => 'foo'];
  269. $this->getCollection()->insert($document);
  270. $expected = [
  271. 'ok' => 1.0,
  272. 'nModified' => 2,
  273. 'n' => 3,
  274. 'err' => null,
  275. 'errmsg' => null,
  276. 'updatedExisting' => true,
  277. ];
  278. $result = $this->getCollection()->update(['change' => true], ['$set' => ['foo' => 'foo']], ['multiple' => true]);
  279. $this->assertSame($expected, $result);
  280. $this->assertSame(3, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  281. }
  282. public function testUnacknowledgedUpdate()
  283. {
  284. $document = ['foo' => 'bar'];
  285. $this->getCollection()->insert($document);
  286. unset($document['_id']);
  287. $this->getCollection()->insert($document);
  288. $this->assertTrue($this->getCollection()->update($document, ['$set' => ['foo' => 'foo']], ['w' => 0]));
  289. }
  290. public function testRemoveMultiple()
  291. {
  292. $document = ['change' => true, 'foo' => 'bar'];
  293. $this->getCollection()->insert($document);
  294. unset($document['_id']);
  295. $this->getCollection()->insert($document);
  296. $document = ['change' => true, 'foo' => 'foo'];
  297. $this->getCollection()->insert($document);
  298. $expected = [
  299. 'ok' => 1.0,
  300. 'n' => 2,
  301. 'err' => null,
  302. 'errmsg' => null,
  303. ];
  304. $result = $this->getCollection()->remove(['foo' => 'bar']);
  305. $this->assertSame($expected, $result);
  306. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count());
  307. }
  308. public function testRemoveSingle()
  309. {
  310. $document = ['change' => true, 'foo' => 'bar'];
  311. $this->getCollection()->insert($document);
  312. unset($document['_id']);
  313. $this->getCollection()->insert($document);
  314. unset($document['_id']);
  315. $this->getCollection()->insert($document);
  316. $expected = [
  317. 'ok' => 1.0,
  318. 'n' => 1,
  319. 'err' => null,
  320. 'errmsg' => null,
  321. ];
  322. $result = $this->getCollection()->remove(['foo' => 'bar'], ['justOne' => true]);
  323. $this->assertSame($expected, $result);
  324. $this->assertSame(2, $this->getCheckDatabase()->selectCollection('test')->count());
  325. }
  326. public function testRemoveUnacknowledged()
  327. {
  328. $document = ['change' => true, 'foo' => 'bar'];
  329. $this->getCollection()->insert($document);
  330. unset($document['_id']);
  331. $this->getCollection()->insert($document);
  332. unset($document['_id']);
  333. $this->getCollection()->insert($document);
  334. $this->assertTrue($this->getCollection()->remove(['foo' => 'bar'], ['w' => 0]));
  335. }
  336. public function testFindReturnsCursor()
  337. {
  338. $this->prepareData();
  339. $collection = $this->getCollection();
  340. $this->assertInstanceOf('MongoCursor', $collection->find());
  341. }
  342. public function testCount()
  343. {
  344. $this->prepareData();
  345. $collection = $this->getCollection();
  346. $this->assertSame(3, $collection->count());
  347. $this->assertSame(2, $collection->count(['foo' => 'bar']));
  348. }
  349. public function testCountTimeout()
  350. {
  351. $this->failMaxTimeMS();
  352. $this->setExpectedException('MongoExecutionTimeoutException');
  353. $this->getCollection()->count([], ['maxTimeMS' => 1]);
  354. }
  355. public function testFindOne()
  356. {
  357. $this->prepareData();
  358. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  359. $this->assertSame(['foo' => 'foo'], $document);
  360. }
  361. public function testFindOneNotFound()
  362. {
  363. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  364. $this->assertNull($document);
  365. }
  366. public function testFindOneConnectionIssue()
  367. {
  368. $this->setExpectedException('MongoConnectionException');
  369. $client = $this->getClient([], 'mongodb://localhost:28888?connectTimeoutMS=1');
  370. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  371. $collection->findOne();
  372. }
  373. public function testDistinct()
  374. {
  375. $this->prepareData();
  376. $values = $this->getCollection()->distinct('foo');
  377. $this->assertInternalType('array', $values);
  378. sort($values);
  379. $this->assertEquals(['bar', 'foo'], $values);
  380. }
  381. public function testDistinctWithQuery()
  382. {
  383. $this->prepareData();
  384. $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
  385. $this->assertInternalType('array', $values);
  386. $this->assertEquals(['bar'], $values);
  387. }
  388. public function testAggregate()
  389. {
  390. $collection = $this->getCollection();
  391. $this->prepareData();
  392. $pipeline = [
  393. [
  394. '$group' => [
  395. '_id' => '$foo',
  396. 'count' => [ '$sum' => 1 ],
  397. ],
  398. ],
  399. [
  400. '$sort' => ['_id' => 1]
  401. ]
  402. ];
  403. $result = $collection->aggregate($pipeline);
  404. $this->assertInternalType('array', $result);
  405. $this->assertArrayHasKey('result', $result);
  406. $this->assertEquals([
  407. ['_id' => 'bar', 'count' => 2],
  408. ['_id' => 'foo', 'count' => 1],
  409. ], $result['result']);
  410. }
  411. public function testAggregateInvalidPipeline()
  412. {
  413. $collection = $this->getCollection();
  414. $pipeline = [
  415. [
  416. '$invalid' => []
  417. ],
  418. ];
  419. $this->setExpectedException('MongoResultException', 'Unrecognized pipeline stage name');
  420. $collection->aggregate($pipeline);
  421. }
  422. public function testAggregateTimeoutException()
  423. {
  424. $collection = $this->getCollection();
  425. $this->failMaxTimeMS();
  426. $this->setExpectedException('MongoExecutionTimeoutException');
  427. $pipeline = [
  428. [
  429. '$group' => [
  430. '_id' => '$foo',
  431. 'count' => [ '$sum' => 1 ],
  432. ],
  433. ],
  434. [
  435. '$sort' => ['_id' => 1]
  436. ]
  437. ];
  438. $collection->aggregate($pipeline, ['maxTimeMS' => 1]);
  439. }
  440. public function testAggregateCursor()
  441. {
  442. $collection = $this->getCollection();
  443. $this->prepareData();
  444. $pipeline = [
  445. [
  446. '$group' => [
  447. '_id' => '$foo',
  448. 'count' => [ '$sum' => 1 ],
  449. ],
  450. ],
  451. [
  452. '$sort' => ['_id' => 1]
  453. ]
  454. ];
  455. $cursor = $collection->aggregateCursor($pipeline);
  456. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  457. $this->assertEquals([
  458. ['_id' => 'bar', 'count' => 2],
  459. ['_id' => 'foo', 'count' => 1],
  460. ], iterator_to_array($cursor));
  461. }
  462. public function testReadPreference()
  463. {
  464. $collection = $this->getCollection();
  465. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  466. $this->assertFalse($collection->getSlaveOkay());
  467. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  468. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  469. $this->assertTrue($collection->getSlaveOkay());
  470. $this->assertTrue($collection->setSlaveOkay(true));
  471. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  472. $this->assertTrue($collection->setSlaveOkay(false));
  473. $this->assertArraySubset(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  474. }
  475. public function testReadPreferenceIsSetInDriver()
  476. {
  477. $this->skipTestIf(extension_loaded('mongo'));
  478. $collection = $this->getCollection();
  479. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  480. // Only way to check whether options are passed down is through debugInfo
  481. $readPreference = $collection->getCollection()->__debugInfo()['readPreference'];
  482. $this->assertSame(ReadPreference::RP_SECONDARY, $readPreference->getMode());
  483. $this->assertSame([['a' => 'b']], $readPreference->getTagSets());
  484. }
  485. public function testReadPreferenceIsInherited()
  486. {
  487. $database = $this->getDatabase();
  488. $database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]);
  489. $collection = $database->selectCollection('test');
  490. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  491. }
  492. public function testWriteConcern()
  493. {
  494. $collection = $this->getCollection();
  495. $this->assertTrue($collection->setWriteConcern('majority', 100));
  496. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  497. }
  498. public function testWriteConcernIsSetInDriver()
  499. {
  500. $this->skipTestIf(extension_loaded('mongo'));
  501. $collection = $this->getCollection();
  502. $this->assertTrue($collection->setWriteConcern(2, 100));
  503. // Only way to check whether options are passed down is through debugInfo
  504. $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
  505. $this->assertSame(2, $writeConcern->getW());
  506. $this->assertSame(100, $writeConcern->getWtimeout());
  507. }
  508. public function testWriteConcernIsInherited()
  509. {
  510. $database = $this->getDatabase();
  511. $database->setWriteConcern('majority', 100);
  512. $collection = $database->selectCollection('test');
  513. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  514. }
  515. public function testSaveInsert()
  516. {
  517. $id = '54203e08d51d4a1f868b456e';
  518. $collection = $this->getCollection();
  519. $objectId = new \MongoId($id);
  520. $expected = [
  521. 'ok' => 1.0,
  522. 'nModified' => 0,
  523. 'n' => 1,
  524. 'err' => null,
  525. 'errmsg' => null,
  526. 'upserted' => $objectId,
  527. 'updatedExisting' => false,
  528. ];
  529. $document = ['_id' => $objectId, 'foo' => 'bar'];
  530. $this->assertEquals($expected, $collection->save($document));
  531. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  532. $this->assertSame(1, $newCollection->count());
  533. $object = $newCollection->findOne();
  534. $this->assertNotNull($object);
  535. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  536. $this->assertSame($id, (string) $object->_id);
  537. $this->assertObjectHasAttribute('foo', $object);
  538. $this->assertAttributeSame('bar', 'foo', $object);
  539. }
  540. public function testRemoveOne()
  541. {
  542. $id = '54203e08d51d4a1f868b456e';
  543. $collection = $this->getCollection();
  544. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  545. $collection->insert($document);
  546. $collection->remove(['_id' => new \MongoId($id)]);
  547. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  548. $this->assertSame(0, $newCollection->count());
  549. }
  550. public function testSaveUpdate()
  551. {
  552. $expected = [
  553. 'ok' => 1.0,
  554. 'nModified' => 1,
  555. 'n' => 1,
  556. 'err' => null,
  557. 'errmsg' => null,
  558. 'updatedExisting' => true,
  559. ];
  560. $id = '54203e08d51d4a1f868b456e';
  561. $collection = $this->getCollection();
  562. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  563. $saveDocument = ['_id' => new \MongoId($id), 'foo' => 'foo'];
  564. $collection->insert($insertDocument);
  565. $this->assertSame($expected, $collection->save($saveDocument));
  566. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  567. $this->assertSame(1, $newCollection->count());
  568. $object = $newCollection->findOne();
  569. $this->assertNotNull($object);
  570. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  571. $this->assertSame($id, (string) $object->_id);
  572. $this->assertObjectHasAttribute('foo', $object);
  573. $this->assertAttributeSame('foo', 'foo', $object);
  574. }
  575. public function testSavingShouldReplaceTheWholeDocument() {
  576. $id = '54203e08d51d4a1f868b456e';
  577. $collection = $this->getCollection();
  578. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  579. $saveDocument = ['_id' => new \MongoId($id)];
  580. $collection->insert($insertDocument);
  581. $collection->save($saveDocument);
  582. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  583. $this->assertSame(1, $newCollection->count());
  584. $object = $newCollection->findOne();
  585. $this->assertNotNull($object);
  586. $this->assertObjectNotHasAttribute('foo', $object);
  587. }
  588. public function testSaveDuplicate()
  589. {
  590. $collection = $this->getCollection();
  591. $collection->createIndex(['foo' => 1], ['unique' => true]);
  592. $document = ['foo' => 'bar'];
  593. $collection->save($document);
  594. $this->setExpectedException('MongoDuplicateKeyException');
  595. unset($document['_id']);
  596. $collection->save($document);
  597. }
  598. public function testSaveEmptyKeys()
  599. {
  600. $document = [];
  601. $this->getCollection()->save($document);
  602. $this->assertSame(1, $this->getCollection()->count());
  603. }
  604. public function testSaveEmptyObject()
  605. {
  606. $document = (object) [];
  607. $this->getCollection()->save($document);
  608. $this->assertSame(1, $this->getCollection()->count());
  609. }
  610. public function testGetDBRef()
  611. {
  612. $collection = $this->getCollection();
  613. $insertDocument = ['_id' => 1, 'foo' => 'bar'];
  614. $collection->insert($insertDocument);
  615. $document = $collection->getDBRef([
  616. '$ref' => 'test',
  617. '$id' => 1,
  618. ]);
  619. $this->assertEquals($insertDocument, $document);
  620. }
  621. public function testCreateDBRef()
  622. {
  623. $collection = $this->getCollection();
  624. $reference = $collection->createDBRef(['_id' => 'foo']);
  625. $this->assertSame(
  626. [
  627. '$ref' => 'test',
  628. '$id' => 'foo',
  629. ],
  630. $reference
  631. );
  632. }
  633. public function testCreateIndex()
  634. {
  635. $expected = [
  636. 'createdCollectionAutomatically' => true,
  637. 'numIndexesBefore' => 1,
  638. 'numIndexesAfter' => 2,
  639. 'ok' => 1.0,
  640. ];
  641. $collection = $this->getCollection();
  642. $this->assertSame($expected, $collection->createIndex(['foo' => 1]));
  643. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  644. $iterator = $newCollection->listIndexes();
  645. $indexes = iterator_to_array($iterator);
  646. $this->assertCount(2, $indexes);
  647. $index = $indexes[1];
  648. $this->assertSame(['foo' => 1], $index->getKey());
  649. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  650. }
  651. public function testCreateIndexInvalid()
  652. {
  653. $this->setExpectedException('MongoException', 'index specification has no elements');
  654. $this->getCollection()->createIndex([]);
  655. }
  656. public function testCreateIndexTwice()
  657. {
  658. $this->getCollection()->createIndex(['foo' => 1]);
  659. $expected = [
  660. 'createdCollectionAutomatically' => false,
  661. 'numIndexesBefore' => 2,
  662. 'numIndexesAfter' => 2,
  663. 'note' => 'all indexes already exist',
  664. 'ok' => 1.0
  665. ];
  666. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  667. }
  668. public function testCreateIndexWithDeprecatedOptions()
  669. {
  670. $this->getCollection()->createIndex(['foo' => 1], ['w' => 1]);
  671. $expected = [
  672. 'createdCollectionAutomatically' => false,
  673. 'numIndexesBefore' => 2,
  674. 'numIndexesAfter' => 2,
  675. 'note' => 'all indexes already exist',
  676. 'ok' => 1.0
  677. ];
  678. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  679. }
  680. public function testCreateIndexTwiceWithSameName()
  681. {
  682. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']);
  683. $expected = [
  684. 'createdCollectionAutomatically' => false,
  685. 'numIndexesBefore' => 2,
  686. 'numIndexesAfter' => 2,
  687. 'note' => 'all indexes already exist',
  688. 'ok' => 1.0
  689. ];
  690. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']));
  691. }
  692. public function testCreateIndexTwiceWithDifferentName()
  693. {
  694. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']);
  695. $expected = [
  696. 'createdCollectionAutomatically' => false,
  697. 'numIndexesBefore' => 2,
  698. 'numIndexesAfter' => 2,
  699. 'note' => 'all indexes already exist',
  700. 'ok' => 1.0
  701. ];
  702. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], ['name' => 'index_test']));
  703. }
  704. public function testCreateIndexTwiceWithDifferentOrder()
  705. {
  706. $this->getCollection()->createIndex(['foo' => 1, 'bar' => 1]);
  707. $expected = [
  708. 'createdCollectionAutomatically' => false,
  709. 'numIndexesBefore' => 2,
  710. 'numIndexesAfter' => 3,
  711. 'ok' => 1.0
  712. ];
  713. $this->assertSame($expected, $this->getCollection()->createIndex(['bar' => 1, 'foo' => 1]));
  714. }
  715. public function testCreateIndexesWithDifferentOptions()
  716. {
  717. $this->setExpectedException('MongoResultException');
  718. $this->getCollection()->createIndex(['foo' => 1]);
  719. $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
  720. }
  721. /**
  722. * @dataProvider createIndexIgnoredOptions
  723. */
  724. public function testCreateIndexesWithIgnoredOptions($option)
  725. {
  726. $this->getCollection()->createIndex(['foo' => 1]);
  727. $expected = [
  728. 'createdCollectionAutomatically' => false,
  729. 'numIndexesBefore' => 2,
  730. 'numIndexesAfter' => 2,
  731. 'note' => 'all indexes already exist',
  732. 'ok' => 1.0
  733. ];
  734. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], [$option => true]));
  735. }
  736. public static function createIndexIgnoredOptions()
  737. {
  738. return [
  739. 'background' => ['background'],
  740. 'dropDups' => ['dropDups'],
  741. ];
  742. }
  743. public function testCreateIndexWithSameNameAndDifferentOptions()
  744. {
  745. $this->setExpectedException('MongoResultException');
  746. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
  747. $this->getCollection()->createIndex(['bar' => 1], ['name' => 'foo']);
  748. }
  749. public function testEnsureIndex()
  750. {
  751. $expected = [
  752. 'createdCollectionAutomatically' => true,
  753. 'numIndexesBefore' => 1,
  754. 'numIndexesAfter' => 2,
  755. 'ok' => 1.0
  756. ];
  757. $collection = $this->getCollection();
  758. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  759. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  760. $indexes = iterator_to_array($newCollection->listIndexes());
  761. $this->assertCount(2, $indexes);
  762. $index = $indexes[1];
  763. $this->assertSame(['bar' => 1], $index->getKey());
  764. $this->assertTrue($index->isUnique());
  765. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  766. }
  767. public function testEnsureIndexAlreadyExists()
  768. {
  769. $collection = $this->getCollection();
  770. $collection->ensureIndex(['bar' => 1], ['unique' => true]);
  771. $expected = [
  772. 'createdCollectionAutomatically' => false,
  773. 'numIndexesBefore' => 2,
  774. 'numIndexesAfter' => 2,
  775. 'ok' => 1.0,
  776. 'note' => 'all indexes already exist',
  777. ];
  778. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  779. }
  780. public function testEnsureIndexAlreadyExistsWithDifferentOptions()
  781. {
  782. $collection = $this->getCollection();
  783. $collection->ensureIndex(['bar' => 1], ['unique' => true]);
  784. $this->setExpectedException('MongoResultException', 'Index with name: bar_1 already exists with different options');
  785. $collection->ensureIndex(['bar' => 1]);
  786. }
  787. public function testDeleteIndexUsingIndexName()
  788. {
  789. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  790. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  791. $expected = [
  792. 'nIndexesWas' => 2,
  793. 'errmsg' => 'index not found with name [bar_1]',
  794. 'ok' => 0.0,
  795. 'code' => 27,
  796. ];
  797. $this->assertEquals($expected, $this->getCollection()->deleteIndex('bar'));
  798. $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
  799. }
  800. public function testDeleteIndexUsingField()
  801. {
  802. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  803. $newCollection->createIndex(['bar' => 1]);
  804. $expected = [
  805. 'nIndexesWas' => 2,
  806. 'ok' => 1.0,
  807. ];
  808. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  809. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  810. }
  811. public function testDeleteIndexUsingKeys()
  812. {
  813. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  814. $newCollection->createIndex(['bar' => 1]);
  815. $expected = [
  816. 'nIndexesWas' => 2,
  817. 'ok' => 1.0,
  818. ];
  819. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  820. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  821. }
  822. public function testDeleteIndexes()
  823. {
  824. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  825. $newCollection->createIndex(['bar' => 1]);
  826. $expected = [
  827. 'nIndexesWas' => 2,
  828. 'msg' => 'non-_id indexes dropped for collection',
  829. 'ok' => 1.0,
  830. ];
  831. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  832. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  833. }
  834. public function testDeleteIndexesForNonExistingCollection()
  835. {
  836. $expected = [
  837. 'ok' => 0.0,
  838. 'errmsg' => 'ns not found',
  839. 'code' => 26,
  840. ];
  841. $this->assertSame($expected, $this->getcollection('nonExisting')->deleteIndexes());
  842. }
  843. public function testGetIndexInfo()
  844. {
  845. $collection = $this->getCollection();
  846. $collection->createIndex(['foo' => 1]);
  847. $expected = [
  848. [
  849. 'v' => 1,
  850. 'key' => ['_id' => 1],
  851. 'name' => '_id_',
  852. 'ns' => 'mongo-php-adapter.test',
  853. ],
  854. [
  855. 'v' => 1,
  856. 'key' => ['foo' => 1],
  857. 'name' => 'foo_1',
  858. 'ns' => 'mongo-php-adapter.test',
  859. ],
  860. ];
  861. $this->assertSame(
  862. $expected,
  863. $collection->getIndexInfo()
  864. );
  865. }
  866. public function testFindAndModifyUpdate()
  867. {
  868. $id = '54203e08d51d4a1f868b456e';
  869. $collection = $this->getCollection();
  870. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  871. $collection->insert($document);
  872. $document = $collection->findAndModify(
  873. ['_id' => new \MongoId($id)],
  874. ['$set' => ['foo' => 'foo']]
  875. );
  876. $this->assertSame('bar', $document['foo']);
  877. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  878. $this->assertSame(1, $newCollection->count());
  879. $object = $newCollection->findOne();
  880. $this->assertNotNull($object);
  881. $this->assertAttributeSame('foo', 'foo', $object);
  882. }
  883. public function testFindAndModifyUpdateWithUpdateOptions()
  884. {
  885. $id = '54203e08d51d4a1f868b456e';
  886. $collection = $this->getCollection();
  887. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  888. $collection->insert($document);
  889. $document = $collection->findAndModify(
  890. ['_id' => new \MongoId($id)],
  891. [],
  892. [],
  893. [
  894. 'update' => ['bar' => 'foo']
  895. ]
  896. );
  897. $this->assertSame('bar', $document['foo']);
  898. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  899. $this->assertSame(1, $newCollection->count());
  900. $object = $newCollection->findOne();
  901. $this->assertNotNull($object);
  902. $this->assertAttributeSame('foo', 'bar', $object);
  903. $this->assertObjectNotHasAttribute('foo', $object);
  904. }
  905. public function testFindAndModifyUpdateReplace()
  906. {
  907. $id = '54203e08d51d4a1f868b456e';
  908. $collection = $this->getCollection();
  909. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  910. $collection->insert($document);
  911. $document = $collection->findAndModify(
  912. ['_id' => new \MongoId($id)],
  913. ['_id' => new \MongoId($id), 'foo' => 'boo']
  914. );
  915. $this->assertSame('bar', $document['foo']);
  916. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  917. $this->assertSame(1, $newCollection->count());
  918. $object = $newCollection->findOne();
  919. $this->assertNotNull($object);
  920. $this->assertAttributeSame('boo', 'foo', $object);
  921. $this->assertObjectNotHasAttribute('bar', $object);
  922. }
  923. public function testFindAndModifyUpdateReturnNew()
  924. {
  925. $id = '54203e08d51d4a1f868b456e';
  926. $collection = $this->getCollection();
  927. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  928. $collection->insert($document);
  929. $document = $collection->findAndModify(
  930. ['_id' => new \MongoId($id)],
  931. ['$set' => ['foo' => 'foo']],
  932. null,
  933. ['new' => true]
  934. );
  935. $this->assertSame('foo', $document['foo']);
  936. }
  937. public function testFindAndModifyWithFields()
  938. {
  939. $id = '54203e08d51d4a1f868b456e';
  940. $collection = $this->getCollection();
  941. $document = [
  942. '_id' => new \MongoId($id),
  943. 'foo' => 'bar',
  944. 'bar' => 'foo',
  945. ];
  946. $collection->insert($document);
  947. $document = $collection->findAndModify(
  948. ['_id' => new \MongoId($id)],
  949. ['$set' => ['foo' => 'foo']],
  950. ['foo' => true]
  951. );
  952. $this->assertArrayNotHasKey('bar', $document);
  953. $this->assertArrayHasKey('foo', $document);
  954. }
  955. public function testGroup()
  956. {
  957. $collection = $this->getCollection();
  958. $document1 = ['a' => 2];
  959. $collection->insert($document1);
  960. $document2 = ['b' => 5];
  961. $collection->insert($document2);
  962. $document3 = ['a' => 1];
  963. $collection->insert($document3);
  964. $keys = [];
  965. $initial = ["count" => 0];
  966. $reduce = "function (obj, prev) { prev.count++; }";
  967. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  968. $result = $collection->group($keys, $initial, $reduce, $condition);
  969. $this->assertArraySubset(
  970. [
  971. 'retval' => [['count' => 1.0]],
  972. 'count' => 1.0,
  973. 'keys' => 1,
  974. 'ok' => 1.0,
  975. ],
  976. $result
  977. );
  978. }
  979. public function testMapReduce()
  980. {
  981. $data = array(
  982. array(
  983. 'username' => 'jones',
  984. 'likes' => 20.0,
  985. 'text' => 'Hello world!'
  986. ),
  987. array(
  988. 'username' => 'bob',
  989. 'likes' => 100.0,
  990. 'text' => 'Hello world!'
  991. ),
  992. array(
  993. 'username' => 'bob',
  994. 'likes' => 100.0,
  995. 'text' => 'Hello world!'
  996. ),
  997. );
  998. $collection = $this->getCollection();
  999. $collection->batchInsert($data);
  1000. $map = 'function() {
  1001. emit(this.username, { count: 1, likes: this.likes });
  1002. }';
  1003. $reduce = 'function(key, values) {
  1004. var result = {count: 0, likes: 0};
  1005. values.forEach(function(value) {
  1006. result.count += value.count;
  1007. result.likes += value.likes;
  1008. });
  1009. return result;
  1010. }';
  1011. $finalize = 'function (key, value) { value.test = "test"; return value; }';
  1012. $command = [
  1013. 'mapreduce' => $this->getCollection()->getName(),
  1014. 'map' => new \MongoCode($map),
  1015. 'reduce' => new \MongoCode($reduce),
  1016. 'query' => (object) [],
  1017. 'out' => ['inline' => true],
  1018. 'finalize' => new \MongoCode($finalize),
  1019. ];
  1020. $result = $this->getDatabase()->command($command);
  1021. $expected = [
  1022. [
  1023. '_id' => 'bob',
  1024. 'value' => [
  1025. 'count' => 2.0,
  1026. 'likes' => 200.0,
  1027. 'test' => 'test',
  1028. ],
  1029. ],
  1030. [
  1031. '_id' => 'jones',
  1032. 'value' => [
  1033. 'count' => 1.0,
  1034. 'likes' => 20.0,
  1035. 'test' => 'test',
  1036. ],
  1037. ],
  1038. ];
  1039. $this->assertSame(1.0, $result['ok']);
  1040. $this->assertSame($expected, $result['results']);
  1041. }
  1042. public function testFindAndModifyResultException()
  1043. {
  1044. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  1045. $collection = $this->getCollection();
  1046. $this->setExpectedException('MongoResultException');
  1047. $collection->findAndModify(
  1048. array("inprogress" => false, "name" => "Next promo"),
  1049. array('$unsupportedOperator' => array("tasks" => -1)),
  1050. array("tasks" => true),
  1051. array("new" => true)
  1052. );
  1053. }
  1054. public function testFindAndModifyExceptionTimeout()
  1055. {
  1056. $this->failMaxTimeMS();
  1057. $id = '54203e08d51d4a1f868b456e';
  1058. $collection = $this->getCollection();
  1059. $this->setExpectedException('MongoExecutionTimeoutException');
  1060. $document = $collection->findAndModify(
  1061. ['_id' => new \MongoId($id)],
  1062. null,
  1063. null,
  1064. ['maxTimeMS' => 1, 'remove' => true]
  1065. );
  1066. }
  1067. public function testFindAndModifyRemove()
  1068. {
  1069. $id = '54203e08d51d4a1f868b456e';
  1070. $collection = $this->getCollection();
  1071. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1072. $collection->insert($document);
  1073. $document = $collection->findAndModify(
  1074. ['_id' => new \MongoId($id)],
  1075. null,
  1076. null,
  1077. ['remove' => true]
  1078. );
  1079. $this->assertEquals('bar', $document['foo']);
  1080. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1081. $this->assertSame(0, $newCollection->count());
  1082. }
  1083. public function testValidate()
  1084. {
  1085. $collection = $this->getCollection();
  1086. $document = ['foo' => 'bar'];
  1087. $collection->insert($document);
  1088. $result = $collection->validate();
  1089. $this->assertArraySubset(
  1090. [
  1091. 'ns' => 'mongo-php-adapter.test',
  1092. 'nrecords' => 1,
  1093. 'nIndexes' => 1,
  1094. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  1095. 'valid' => true,
  1096. 'errors' => [],
  1097. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  1098. 'ok' => 1.0
  1099. ],
  1100. $result
  1101. );
  1102. }
  1103. public function testDrop()
  1104. {
  1105. $document = ['foo' => 'bar'];
  1106. $this->getCollection()->insert($document);
  1107. $expected = [
  1108. 'ns' => (string) $this->getCollection(),
  1109. 'nIndexesWas' => 1,
  1110. 'ok' => 1.0
  1111. ];
  1112. $this->assertSame($expected, $this->getCollection()->drop());
  1113. }
  1114. public function testEmptyCollectionName()
  1115. {
  1116. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  1117. new \MongoCollection($this->getDatabase(), '');
  1118. }
  1119. public function testSelectCollectionWithNullBytes()
  1120. {
  1121. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  1122. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  1123. }
  1124. public function testSubCollectionWithNullBytes()
  1125. {
  1126. $collection = $this->getCollection();
  1127. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  1128. $this->assertSame('test', $collection->getName());
  1129. }
  1130. }
  1131. class PrivatePropertiesStub
  1132. {
  1133. private $foo = 'bar';
  1134. }