*/ class MongoCollectionTest extends TestCase { public function testGetNestedCollections() { $collection = $this->getCollection()->foo->bar; $this->assertSame('mongo-php-adapter.test.foo.bar', (string) $collection); } public function testCreateRecord() { $id = '54203e08d51d4a1f868b456e'; $collection = $this->getCollection(); $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']); $newCollection = $this->getCheckDatabase()->selectCollection('test'); $this->assertSame(1, $newCollection->count()); $object = $newCollection->findOne(); $this->assertNotNull($object); $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object); $this->assertSame($id, (string) $object->_id); $this->assertObjectHasAttribute('foo', $object); $this->assertAttributeSame('bar', 'foo', $object); } public function testFindReturnsCursor() { $collection = $this->getCollection(); $collection->insert(['sorter' => 1]); $this->assertInstanceOf('MongoCursor', $collection->find()); } public function testCount() { $collection = $this->getCollection(); $collection->insert(['foo' => 'bar']); $collection->insert(['foo' => 'foo']); $this->assertSame(2, $collection->count()); $this->assertSame(1, $collection->count(['foo' => 'bar'])); } public function testAggregate() { $collection = $this->getCollection(); $collection->insert(['foo' => 'bar']); $collection->insert(['foo' => 'bar']); $collection->insert(['foo' => 'foo']); $pipeline = [ [ '$group' => [ '_id' => '$foo', 'count' => [ '$sum' => 1 ], ], ], [ '$sort' => ['_id' => 1] ] ]; $result = $collection->aggregate($pipeline); $this->assertInternalType('array', $result); $this->assertArrayHasKey('result', $result); $this->assertEquals([ ['_id' => 'bar', 'count' => 2], ['_id' => 'foo', 'count' => 1], ], $result['result']); } public function testAggregateCursor() { $collection = $this->getCollection(); $collection->insert(['foo' => 'bar']); $collection->insert(['foo' => 'bar']); $collection->insert(['foo' => 'foo']); $pipeline = [ [ '$group' => [ '_id' => '$foo', 'count' => [ '$sum' => 1 ], ], ], [ '$sort' => ['_id' => 1] ] ]; $cursor = $collection->aggregateCursor($pipeline); $this->assertInstanceOf('MongoCommandCursor', $cursor); $this->assertEquals([ ['_id' => 'bar', 'count' => 2], ['_id' => 'foo', 'count' => 1], ], iterator_to_array($cursor)); } /** * @return \MongoCollection */ protected function getCollection($name = 'test') { $client = new \MongoClient(); return $client->selectCollection('mongo-php-adapter', $name); } }