123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- <?php
- namespace yiiunit\extensions\mongodb;
- use MongoDB\BSON\ObjectID;
- use MongoDB\Driver\Cursor;
- class CollectionTest extends TestCase
- {
- protected function tearDown()
- {
- $this->dropCollection('customer');
- $this->dropCollection('mapReduceOut');
- parent::tearDown();
- }
- // Tests :
- public function testGetName()
- {
- $collectionName = 'customer';
- $collection = $this->getConnection()->getCollection($collectionName);
- $this->assertEquals($collectionName, $collection->name);
- $this->assertEquals($this->getConnection()->getDefaultDatabaseName() . '.' . $collectionName, $collection->getFullName());
- }
- public function testFind()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $cursor = $collection->find();
- $this->assertTrue($cursor instanceof Cursor);
- }
- public function testInsert()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $data = [
- 'name' => 'customer 1',
- 'address' => 'customer 1 address',
- ];
- $id = $collection->insert($data);
- $this->assertTrue($id instanceof ObjectID);
- $this->assertNotEmpty($id->__toString());
- }
- /**
- * @depends testInsert
- * @depends testFind
- */
- public function testFindOne()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $data = [
- 'name' => 'customer 1',
- 'address' => 'customer 1 address',
- ];
- $id = $collection->insert($data);
- $row = $collection->findOne(['_id' => $id]);
- $this->assertEquals($data['name'], $row['name']);
- $row = $collection->findOne(['_id' => 'unexisting-id']);
- $this->assertNull($row);
- }
- /**
- * @depends testInsert
- * @depends testFind
- */
- public function testFindAll()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $data = [
- 'name' => 'customer 1',
- 'address' => 'customer 1 address',
- ];
- $id = $collection->insert($data);
- $cursor = $collection->find();
- $rows = [];
- foreach ($cursor as $row) {
- $rows[] = $row;
- }
- $this->assertEquals(1, count($rows));
- $this->assertEquals($id, $rows[0]['_id']);
- }
- /**
- * @depends testFind
- */
- public function testBatchInsert()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $rows = [
- [
- 'name' => 'customer 1',
- 'address' => 'customer 1 address',
- ],
- [
- 'name' => 'customer 2',
- 'address' => 'customer 2 address',
- ],
- ];
- $insertedRows = $collection->batchInsert($rows);
- $this->assertTrue($insertedRows[0]['_id'] instanceof ObjectID);
- $this->assertTrue($insertedRows[1]['_id'] instanceof ObjectID);
- $this->assertCount(count($rows), $collection->find()->toArray());
- }
- public function testSave()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $data = [
- 'name' => 'customer 1',
- 'address' => 'customer 1 address',
- ];
- $id = $collection->save($data);
- $this->assertTrue($id instanceof ObjectID);
- $this->assertNotEmpty($id->__toString());
- }
- /**
- * @depends testSave
- */
- public function testUpdateBySave()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $data = [
- 'name' => 'customer 1',
- 'address' => 'customer 1 address',
- ];
- $newId = $collection->save($data);
- $data['_id'] = $newId;
- $updatedId = $collection->save($data);
- $this->assertEquals($newId, $updatedId, 'Unable to update data!');
- $data['_id'] = $newId->__toString();
- $updatedId = $collection->save($data);
- $this->assertEquals($newId, $updatedId, 'Unable to updated data by string id!');
- }
- /**
- * @depends testFindAll
- */
- public function testRemove()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $data = [
- 'name' => 'customer 1',
- 'address' => 'customer 1 address',
- ];
- $id = $collection->insert($data);
- $count = $collection->remove(['_id' => $id]);
- $this->assertEquals(1, $count);
- $rows = $this->findAll($collection);
- $this->assertEquals(0, count($rows));
- }
- /**
- * @depends testBatchInsert
- * @depends testRemove
- */
- public function testRemoveComplexCondition()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $collection->batchInsert([
- [
- 'name' => 'customer 1',
- 'status' => 1,
- ],
- [
- 'name' => 'customer 2',
- 'status' => 2,
- ],
- [
- 'name' => 'customer 3',
- 'status' => 3,
- ],
- ]);
- $count = $collection->remove(['status' => [1, 3]]);
- $this->assertEquals(2, $count);
- $rows = $this->findAll($collection);
- $this->assertEquals(1, count($rows));
- }
- /**
- * @depends testFindAll
- */
- public function testUpdate()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $data = [
- 'name' => 'customer 1',
- 'address' => 'customer 1 address',
- ];
- $id = $collection->insert($data);
- $newData = [
- 'name' => 'new name'
- ];
- $count = $collection->update(['_id' => $id], $newData);
- $this->assertEquals(1, $count);
- list($row) = $this->findAll($collection);
- $this->assertEquals($newData['name'], $row['name']);
- }
- /**
- * @depends testBatchInsert
- */
- public function testGroup()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $rows = [
- [
- 'name' => 'customer 1',
- 'address' => 'customer 1 address',
- ],
- [
- 'name' => 'customer 2',
- 'address' => 'customer 2 address',
- ],
- ];
- $collection->batchInsert($rows);
- $keys = ['address' => 1];
- $initial = ['items' => []];
- $reduce = "function (obj, prev) { prev.items.push(obj.name); }";
- $result = $collection->group($keys, $initial, $reduce);
- $this->assertEquals(2, count($result));
- $this->assertNotEmpty($result[0]['address']);
- $this->assertNotEmpty($result[0]['items']);
- }
- public function testFindAndModify()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $rows = [
- [
- 'name' => 'customer 1',
- 'status' => 1,
- 'amount' => 100,
- ],
- [
- 'name' => 'customer 2',
- 'status' => 1,
- 'amount' => 200,
- ],
- ];
- $collection->batchInsert($rows);
- // increment field
- $result = $collection->findAndModify(['name' => 'customer 1'], ['$inc' => ['status' => 1]]);
- $this->assertEquals('customer 1', $result['name']);
- $this->assertEquals(1, $result['status']);
- $newResult = $collection->findOne(['name' => 'customer 1']);
- $this->assertEquals(2, $newResult['status']);
- // $set and return modified document
- $result = $collection->findAndModify(
- ['name' => 'customer 2'],
- ['$set' => ['status' => 2]],
- ['new' => true]
- );
- $this->assertEquals('customer 2', $result['name']);
- $this->assertEquals(2, $result['status']);
- // Full update document
- $data = [
- 'name' => 'customer 3',
- 'city' => 'Minsk'
- ];
- $result = $collection->findAndModify(
- ['name' => 'customer 2'],
- $data,
- ['new' => true]
- );
- $this->assertEquals('customer 3', $result['name']);
- $this->assertEquals('Minsk', $result['city']);
- $this->assertTrue(!isset($result['status']));
- // Test exceptions
- $this->expectException('\yii\mongodb\Exception');
- $collection->findAndModify(['name' => 'customer 1'], ['$wrongOperator' => ['status' => 1]]);
- }
- /**
- * @depends testBatchInsert
- */
- public function testMapReduce()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $rows = [
- [
- 'name' => 'customer 1',
- 'status' => 1,
- 'amount' => 100,
- ],
- [
- 'name' => 'customer 2',
- 'status' => 1,
- 'amount' => 200,
- ],
- [
- 'name' => 'customer 2',
- 'status' => 2,
- 'amount' => 400,
- ],
- [
- 'name' => 'customer 2',
- 'status' => 3,
- 'amount' => 500,
- ],
- ];
- $collection->batchInsert($rows);
- $result = $collection->mapReduce(
- 'function () {emit(this.status, this.amount)}',
- 'function (key, values) {return Array.sum(values)}',
- 'mapReduceOut',
- ['status' => ['$lt' => 3]]
- );
- $this->assertEquals('mapReduceOut', $result);
- $outputCollection = $this->getConnection()->getCollection($result);
- $rows = $this->findAll($outputCollection);
- $expectedRows = [
- [
- '_id' => 1,
- 'value' => 300,
- ],
- [
- '_id' => 2,
- 'value' => 400,
- ],
- ];
- $this->assertEquals($expectedRows, $rows);
- }
- /**
- * @depends testMapReduce
- */
- public function testMapReduceInline()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $rows = [
- [
- 'name' => 'customer 1',
- 'status' => 1,
- 'amount' => 100,
- ],
- [
- 'name' => 'customer 2',
- 'status' => 1,
- 'amount' => 200,
- ],
- [
- 'name' => 'customer 2',
- 'status' => 2,
- 'amount' => 400,
- ],
- [
- 'name' => 'customer 2',
- 'status' => 3,
- 'amount' => 500,
- ],
- ];
- $collection->batchInsert($rows);
- $result = $collection->mapReduce(
- 'function () {emit(this.status, this.amount)}',
- 'function (key, values) {return Array.sum(values)}',
- ['inline' => true],
- ['status' => ['$lt' => 3]]
- );
- $expectedRows = [
- [
- '_id' => 1,
- 'value' => 300,
- ],
- [
- '_id' => 2,
- 'value' => 400,
- ],
- ];
- $this->assertEquals($expectedRows, $result);
- }
- public function testCreateIndex()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $columns = [
- 'name',
- 'status' => SORT_DESC
- ];
- $this->assertTrue($collection->createIndex($columns));
- $indexInfo = $collection->listIndexes();
- $this->assertEquals(2, count($indexInfo));
- }
- /**
- * @depends testCreateIndex
- */
- public function testDropIndex()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $collection->createIndex('name');
- $this->assertTrue($collection->dropIndex('name'));
- $indexInfo = $collection->listIndexes();
- $this->assertEquals(1, count($indexInfo));
- $this->expectException('\yii\mongodb\Exception');
- $collection->dropIndex('name');
- }
- /**
- * @depends testDropIndex
- * @see https://github.com/yiisoft/yii2-mongodb/issues/247
- */
- public function testDropIndexWithPlugin()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $columns = [
- 'name' => 'text'
- ];
- $collection->createIndex($columns);
- $this->assertTrue($collection->dropIndex($columns));
- $indexInfo = $collection->listIndexes();
- $this->assertEquals(1, count($indexInfo));
- }
- /**
- * @depends testCreateIndex
- */
- public function testDropAllIndexes()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $collection->createIndex('name');
- $this->assertEquals(2, $collection->dropAllIndexes());
- $indexInfo = $collection->listIndexes();
- $this->assertEquals(1, count($indexInfo));
- }
- public function testCreateIndexes()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $columns = [
- ['key' => ['name']],
- ['key' => ['status' => SORT_DESC]]
- ];
- $this->assertTrue($collection->createIndexes($columns));
- $indexInfo = $collection->listIndexes();
- $this->assertEquals(3, count($indexInfo));
- }
- /**
- * @depends testCreateIndexes
- */
- public function testDropIndexes()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $columns = [
- [
- 'key' => ['name'],
- 'name' => 'test_index'
- ],
- [
- 'key' => ['status'],
- 'name' => 'to_be_dropped'
- ],
- ];
- $collection->createIndexes($columns);
- $collection->dropIndexes('to_be_dropped');
- $indexInfo = $collection->listIndexes();
- $this->assertEquals(2, count($indexInfo));
- }
- /**
- * @depends testInsert
- * @depends testFind
- */
- public function testFindByNotObjectId()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $data = [
- 'name' => 'customer 1',
- 'address' => 'customer 1 address',
- ];
- $id = $collection->insert($data);
- $cursor = $collection->find(['_id' => (string) $id]);
- $this->assertTrue($cursor instanceof Cursor);
- $row = current($cursor->toArray());
- $this->assertEquals($id, $row['_id']);
- $cursor = $collection->find(['_id' => 'fake']);
- $this->assertTrue($cursor instanceof Cursor);
- $this->assertCount(0, $cursor->toArray());
- }
- /**
- * @depends testInsert
- *
- * @see https://github.com/yiisoft/yii2/issues/2548
- */
- public function testInsertMongoBin()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $fileName = __FILE__;
- $data = [
- 'name' => 'customer 1',
- 'address' => 'customer 1 address',
- 'binData' => new \MongoDB\BSON\Binary(file_get_contents($fileName), 2),
- ];
- $id = $collection->insert($data);
- $this->assertTrue($id instanceof ObjectID);
- $this->assertNotEmpty($id->__toString());
- }
- /**
- * @depends testBatchInsert
- */
- public function testDistinct()
- {
- $collection = $this->getConnection()->getCollection('customer');
- $rows = [
- [
- 'name' => 'customer 1',
- 'status' => 1,
- ],
- [
- 'name' => 'customer 1',
- 'status' => 1,
- ],
- [
- 'name' => 'customer 3',
- 'status' => 2,
- ],
- ];
- $collection->batchInsert($rows);
- $rows = $collection->distinct('status');
- $this->assertFalse($rows === false);
- $this->assertCount(2, $rows);
- $rows = $collection->distinct('status', ['status' => 1]);
- $this->assertFalse($rows === false);
- $this->assertCount(1, $rows);
- }
- }
|