MongoCollectionTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. $this->prepareData();
  30. $collection = $this->getCollection();
  31. $this->assertInstanceOf('MongoCursor', $collection->find());
  32. }
  33. public function testCount()
  34. {
  35. $this->prepareData();
  36. $collection = $this->getCollection();
  37. $this->assertSame(3, $collection->count());
  38. $this->assertSame(2, $collection->count(['foo' => 'bar']));
  39. }
  40. public function testFindOne()
  41. {
  42. $this->prepareData();
  43. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  44. $this->assertEquals(['foo' => 'foo'], $document);
  45. }
  46. public function testDistinct()
  47. {
  48. $this->prepareData();
  49. $values = $this->getCollection()->distinct('foo');
  50. $this->assertInternalType('array', $values);
  51. sort($values);
  52. $this->assertEquals(['bar', 'foo'], $values);
  53. }
  54. public function testDistinctWithQuery()
  55. {
  56. $this->prepareData();
  57. $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
  58. $this->assertInternalType('array', $values);
  59. $this->assertEquals(['bar'], $values);
  60. }
  61. public function testAggregate()
  62. {
  63. $collection = $this->getCollection();
  64. $collection->insert(['foo' => 'bar']);
  65. $collection->insert(['foo' => 'bar']);
  66. $collection->insert(['foo' => 'foo']);
  67. $pipeline = [
  68. [
  69. '$group' => [
  70. '_id' => '$foo',
  71. 'count' => [ '$sum' => 1 ],
  72. ],
  73. ],
  74. [
  75. '$sort' => ['_id' => 1]
  76. ]
  77. ];
  78. $result = $collection->aggregate($pipeline);
  79. $this->assertInternalType('array', $result);
  80. $this->assertArrayHasKey('result', $result);
  81. $this->assertEquals([
  82. ['_id' => 'bar', 'count' => 2],
  83. ['_id' => 'foo', 'count' => 1],
  84. ], $result['result']);
  85. }
  86. public function testAggregateCursor()
  87. {
  88. $collection = $this->getCollection();
  89. $collection->insert(['foo' => 'bar']);
  90. $collection->insert(['foo' => 'bar']);
  91. $collection->insert(['foo' => 'foo']);
  92. $pipeline = [
  93. [
  94. '$group' => [
  95. '_id' => '$foo',
  96. 'count' => [ '$sum' => 1 ],
  97. ],
  98. ],
  99. [
  100. '$sort' => ['_id' => 1]
  101. ]
  102. ];
  103. $cursor = $collection->aggregateCursor($pipeline);
  104. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  105. $this->assertEquals([
  106. ['_id' => 'bar', 'count' => 2],
  107. ['_id' => 'foo', 'count' => 1],
  108. ], iterator_to_array($cursor));
  109. }
  110. /**
  111. * @return \MongoCollection
  112. */
  113. protected function getCollection($name = 'test')
  114. {
  115. $client = new \MongoClient();
  116. return $client->selectCollection('mongo-php-adapter', $name);
  117. }
  118. /**
  119. * @return \MongoCollection
  120. */
  121. protected function prepareData()
  122. {
  123. $collection = $this->getCollection();
  124. $collection->insert(['foo' => 'bar']);
  125. $collection->insert(['foo' => 'bar']);
  126. $collection->insert(['foo' => 'foo']);
  127. return $collection;
  128. }
  129. }