MongoCollectionTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. /**
  4. * @author alcaeus <alcaeus@alcaeus.org>
  5. */
  6. class MongoCollectionTest extends TestCase
  7. {
  8. public function testGetNestedCollections()
  9. {
  10. $collection = $this->getCollection()->foo->bar;
  11. $this->assertSame('mongo-php-adapter.test.foo.bar', (string) $collection);
  12. }
  13. public function testCreateRecord()
  14. {
  15. $id = '54203e08d51d4a1f868b456e';
  16. $collection = $this->getCollection();
  17. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  18. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  19. $this->assertSame(1, $newCollection->count());
  20. $object = $newCollection->findOne();
  21. $this->assertNotNull($object);
  22. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  23. $this->assertSame($id, (string) $object->_id);
  24. $this->assertObjectHasAttribute('foo', $object);
  25. $this->assertAttributeSame('bar', 'foo', $object);
  26. }
  27. public function testFindReturnsCursor()
  28. {
  29. $collection = $this->getCollection();
  30. $collection->insert(['sorter' => 1]);
  31. $this->assertInstanceOf('MongoCursor', $collection->find());
  32. }
  33. public function testCount()
  34. {
  35. $collection = $this->getCollection();
  36. $collection->insert(['foo' => 'bar']);
  37. $collection->insert(['foo' => 'foo']);
  38. $this->assertSame(2, $collection->count());
  39. $this->assertSame(1, $collection->count(['foo' => 'bar']));
  40. }
  41. public function testAggregate()
  42. {
  43. $collection = $this->getCollection();
  44. $collection->insert(['foo' => 'bar']);
  45. $collection->insert(['foo' => 'bar']);
  46. $collection->insert(['foo' => 'foo']);
  47. $pipeline = [
  48. [
  49. '$group' => [
  50. '_id' => '$foo',
  51. 'count' => [ '$sum' => 1 ],
  52. ],
  53. ],
  54. [
  55. '$sort' => ['_id' => 1]
  56. ]
  57. ];
  58. $result = $collection->aggregate($pipeline);
  59. $this->assertInternalType('array', $result);
  60. $this->assertArrayHasKey('result', $result);
  61. $this->assertEquals([
  62. ['_id' => 'bar', 'count' => 2],
  63. ['_id' => 'foo', 'count' => 1],
  64. ], $result['result']);
  65. }
  66. public function testAggregateCursor()
  67. {
  68. $collection = $this->getCollection();
  69. $collection->insert(['foo' => 'bar']);
  70. $collection->insert(['foo' => 'bar']);
  71. $collection->insert(['foo' => 'foo']);
  72. $pipeline = [
  73. [
  74. '$group' => [
  75. '_id' => '$foo',
  76. 'count' => [ '$sum' => 1 ],
  77. ],
  78. ],
  79. [
  80. '$sort' => ['_id' => 1]
  81. ]
  82. ];
  83. $cursor = $collection->aggregateCursor($pipeline);
  84. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  85. $this->assertEquals([
  86. ['_id' => 'bar', 'count' => 2],
  87. ['_id' => 'foo', 'count' => 1],
  88. ], iterator_to_array($cursor));
  89. }
  90. /**
  91. * @return \MongoCollection
  92. */
  93. protected function getCollection($name = 'test')
  94. {
  95. $client = new \MongoClient();
  96. return $client->selectCollection('mongo-php-adapter', $name);
  97. }
  98. }