123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- <?php
- namespace yiiunit\extensions\mongodb;
- use MongoDB\BSON\ObjectID;
- use MongoDB\Driver\Cursor;
- use yii\helpers\ArrayHelper;
- class CommandTest extends TestCase
- {
- protected function tearDown()
- {
- $this->dropCollection('customer');
- parent::tearDown();
- }
- public function testCreateCollection()
- {
- $command = $this->getConnection()->createCommand();
- $this->assertTrue($command->createCollection('customer'));
- }
- /**
- * @depends testCreateCollection
- */
- public function testDropCollection()
- {
- $command = $this->getConnection()->createCommand();
- $command->createCollection('customer');
- $this->assertTrue($command->dropCollection('customer'));
- }
- public function testCount()
- {
- $command = $this->getConnection()->createCommand();
- $this->assertEquals(0, $command->count('customer'));
- }
- public function testCreateIndexes()
- {
- $command = $this->getConnection()->createCommand();
- $this->assertTrue($command->createIndexes('customer', [
- [
- 'key' => ['name' => +1],
- ],
- [
- 'key' => ['email'],
- ],
- [
- 'key' => 'address',
- ],
- ]));
- }
- /**
- * @depends testCreateIndexes
- */
- public function testListIndexes()
- {
- $command = $this->getConnection()->createCommand();
- $command->createIndexes('customer', [
- [
- 'key' => ['name' => +1],
- 'name' => 'asc_index'
- ],
- ]);
- $result = $command->listIndexes('customer');
- $this->assertEquals('_id_', $result[0]['name']);
- $this->assertEquals('asc_index', $result[1]['name']);
- }
- /**
- * @depends testCreateIndexes
- */
- public function testDropIndexes()
- {
- $command = $this->getConnection()->createCommand();
- $command->createIndexes('customer', [
- [
- 'key' => ['name' => +1],
- 'name' => 'asc_index'
- ],
- [
- 'key' => ['name' => -1],
- 'name' => 'desc_index'
- ],
- ]);
- $result = $command->dropIndexes('customer', 'asc_index');
- $this->assertEquals(3, $result['nIndexesWas']);
- $result = $command->dropIndexes('customer', '*');
- $this->assertEquals(2, $result['nIndexesWas']);
- $this->expectException('yii\mongodb\Exception');
- $this->expectExceptionMessage('index not found with name');
- $command->dropIndexes('customer', 'desc_index');
- }
- public function testInsert()
- {
- $command = $this->getConnection()->createCommand();
- $insertedId = $command->insert('customer', ['name' => 'John']);
- $this->assertTrue($insertedId instanceof ObjectID);
- }
- /**
- * @depends testInsert
- */
- public function testBatchInsert()
- {
- $command = $this->getConnection()->createCommand();
- $insertedIds = $command->batchInsert('customer', [
- ['name' => 'John'],
- ['name' => 'Sara'],
- ]);
- $this->assertTrue($insertedIds[0] instanceof ObjectID);
- $this->assertTrue($insertedIds[1] instanceof ObjectID);
- }
- /**
- * @depends testInsert
- */
- public function testUpdate()
- {
- $connection = $this->getConnection();
- $newRecordId = $connection->createCommand()->insert('customer', ['name' => 'John']);
- $result = $connection->createCommand()->update('customer', ['_id' => $newRecordId], ['name' => 'Mike']);
- $this->assertEquals(1, $result->getModifiedCount());
- }
- /**
- * @depends testInsert
- */
- public function testDelete()
- {
- $connection = $this->getConnection();
- $newRecordId = $connection->createCommand()->insert('customer', ['name' => 'John']);
- $result = $connection->createCommand()->delete('customer', ['_id' => $newRecordId]);
- $this->assertEquals(1, $result->getDeletedCount());
- }
- /**
- * @depends testInsert
- */
- public function testFind()
- {
- $connection = $this->getConnection();
- $connection->createCommand()->insert('customer', ['name' => 'John']);
- $cursor = $connection->createCommand()->find('customer', []);
- $rows = $cursor->toArray();
- $this->assertCount(1, $rows);
- $this->assertEquals('John', $rows[0]['name']);
- }
- /**
- * @depends testBatchInsert
- */
- public function testFindAndModify()
- {
- $connection = $this->getConnection();
- $rows = [
- [
- 'name' => 'customer 1',
- 'status' => 1,
- 'amount' => 100,
- ],
- [
- 'name' => 'customer 2',
- 'status' => 1,
- 'amount' => 200,
- ],
- ];
- $command = $connection->createCommand();
- $command->batchInsert('customer', $rows);
- // increment field
- $result = $connection->createCommand()->findAndModify('customer', ['name' => 'customer 1'], ['$inc' => ['status' => 1]]);
- $this->assertEquals('customer 1', $result['name']);
- $this->assertEquals(1, $result['status']);
- $cursor = $connection->createCommand()->find('customer', ['name' => 'customer 1']);
- $newResult = current($cursor->toArray());
- $this->assertEquals(2, $newResult['status']);
- // $set and return modified document
- $result = $connection->createCommand()->findAndModify(
- 'customer',
- ['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 = $connection->createCommand()->findAndModify(
- 'customer',
- ['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');
- $connection->createCommand()->findAndModify('customer',['name' => 'customer 1'], ['$wrongOperator' => ['status' => 1]]);
- }
- /**
- * @depends testBatchInsert
- */
- public function testAggregate()
- {
- $connection = $this->getConnection();
- $rows = [
- [
- 'name' => 'customer 1',
- 'status' => 1,
- 'amount' => 100,
- ],
- [
- 'name' => 'customer 2',
- 'status' => 1,
- 'amount' => 200,
- ],
- ];
- $command = $connection->createCommand();
- $command->batchInsert('customer', $rows);
- $pipelines = [
- [
- '$match' => ['status' => 1]
- ],
- [
- '$group' => [
- '_id' => '1',
- 'total' => [
- '$sum' => '$amount'
- ],
- ]
- ]
- ];
- $result = $connection->createCommand()->aggregate('customer', $pipelines);
- $this->assertEquals(['_id' => '1', 'total' => 300], $result[0]);
- }
- /**
- * @depends testAggregate
- *
- * @see https://github.com/yiisoft/yii2-mongodb/issues/228
- */
- public function testAggregateCursor()
- {
- $connection = $this->getConnection();
- $rows = [
- [
- 'name' => 'customer 1',
- 'status' => 1,
- 'amount' => 100,
- ],
- [
- 'name' => 'customer 2',
- 'status' => 1,
- 'amount' => 200,
- ],
- [
- 'name' => 'customer 3',
- 'status' => 1,
- 'amount' => 100,
- ],
- [
- 'name' => 'customer 4',
- 'status' => 1,
- 'amount' => 200,
- ],
- ];
- $command = $connection->createCommand();
- $command->batchInsert('customer', $rows);
- $pipelines = [
- [
- '$match' => ['status' => 1]
- ],
- [
- '$group' => [
- '_id' => '1',
- 'total' => [
- '$sum' => '$amount'
- ],
- ]
- ]
- ];
- $result = $connection->createCommand()->aggregate('customer', $pipelines, ['cursor' => ['batchSize' => 2]]);
- $this->assertTrue($result instanceof Cursor);
- $this->assertEquals(['_id' => '1', 'total' => 600], $result->toArray()[0]);
- }
- /**
- * @depends testFind
- */
- public function testExplain()
- {
- $connection = $this->getConnection();
- $connection->createCommand()->insert('customer', ['name' => 'John']);
- $result = $connection->createCommand()->explain('customer', [
- 'filter' => [
- 'name' => 'John'
- ],
- ]);
- $this->assertArrayHasKey('queryPlanner', $result);
- $this->assertArrayHasKey('executionStats', $result);
- }
- /**
- * @depends testCreateCollection
- */
- public function testListCollections()
- {
- $connection = $this->getConnection();
- $connection->createCommand()->createCollection('customer');
- $collections = $connection->createCommand()->listCollections();
- $collectionNames = ArrayHelper::getColumn($collections, 'name');
- $this->assertContains('customer', $collectionNames);
- }
- /**
- * @depends testUpdate
- * @depends testCount
- *
- * @see https://github.com/yiisoft/yii2-mongodb/issues/168
- */
- public function testUpdateUpsert()
- {
- $connection = $this->getConnection();
- $connection->createCommand()->insert('customer', ['name' => 'John']);
- $result = $connection->createCommand()
- ->update('customer', ['name' => 'Mike'], ['name' => 'Jack']);
- $this->assertEquals(0, $result->getModifiedCount());
- $this->assertEquals(0, $result->getUpsertedCount());
- $this->assertEquals(1, $connection->createCommand()->count('customer'));
- $result = $connection->createCommand()
- ->update('customer', ['name' => 'Mike'], ['name' => 'Jack'], ['upsert' => true]);
- $this->assertEquals(0, $result->getModifiedCount());
- $this->assertEquals(1, $result->getUpsertedCount());
- $this->assertEquals(2, $connection->createCommand()->count('customer'));
- }
- }
|