diff --git a/tests/ActiveDataProviderTest.php b/tests/ActiveDataProviderTest.php index 4d4579641..44efed6e7 100644 --- a/tests/ActiveDataProviderTest.php +++ b/tests/ActiveDataProviderTest.php @@ -3,9 +3,9 @@ namespace yiiunit\extensions\mongodb; use MongoDB\BSON\ObjectID; +use yii; use yii\data\ActiveDataProvider; use yii\mongodb\Query; -use yiiunit\extensions\mongodb\data\ar\ActiveRecord; use yiiunit\extensions\mongodb\data\ar\Customer; class ActiveDataProviderTest extends TestCase @@ -13,8 +13,6 @@ class ActiveDataProviderTest extends TestCase protected function setUp() { parent::setUp(); - $this->mockApplication(); - ActiveRecord::$db = $this->getConnection(); $this->setUpTestRows(); } @@ -29,7 +27,7 @@ protected function tearDown() */ protected function setUpTestRows() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = Yii::$app->mongodb->getCollection('customer'); $rows = []; for ($i = 1; $i <= 10; $i++) { $rows[] = [ @@ -51,14 +49,12 @@ public function testQuery() $provider = new ActiveDataProvider([ 'query' => $query, - 'db' => $this->getConnection(), ]); $models = $provider->getModels(); $this->assertEquals(10, count($models)); $provider = new ActiveDataProvider([ 'query' => $query, - 'db' => $this->getConnection(), 'pagination' => [ 'pageSize' => 5, ] diff --git a/tests/ActiveFixtureTest.php b/tests/ActiveFixtureTest.php index b7b58bd74..19d9a0dee 100644 --- a/tests/ActiveFixtureTest.php +++ b/tests/ActiveFixtureTest.php @@ -10,11 +10,6 @@ class ActiveFixtureTest extends TestCase { - protected function setUp() - { - parent::setUp(); - $this->mockApplication(); - } protected function tearDown() { @@ -32,7 +27,7 @@ public function testLoadCollection() $fixture = $this->getMockBuilder(ActiveFixture::className()) ->setConstructorArgs([ [ - 'db' => $this->getConnection(), + 'db' => Yii::$app->mongodb, 'collectionName' => Customer::collectionName() ] ]) @@ -45,7 +40,7 @@ public function testLoadCollection() $fixture->load(); - $rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName())); + $rows = $this->findAll(Yii::$app->mongodb->getCollection(Customer::collectionName())); $this->assertCount(2, $rows); } @@ -55,7 +50,7 @@ public function testLoadClass() $fixture = $this->getMockBuilder(ActiveFixture::className()) ->setConstructorArgs([ [ - 'db' => $this->getConnection(), + 'db' => yii::$app->mongodb, 'collectionName' => Customer::collectionName() ] ]) @@ -68,7 +63,7 @@ public function testLoadClass() $fixture->load(); - $rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName())); + $rows = $this->findAll(yii::$app->mongodb->getCollection(Customer::collectionName())); $this->assertCount(2, $rows); } @@ -83,7 +78,7 @@ public function testLoadEmptyData() $fixture = $this->getMockBuilder(ActiveFixture::className()) ->setConstructorArgs([ [ - 'db' => $this->getConnection(), + 'db' => Yii::$app->mongodb, 'collectionName' => Customer::collectionName() ] ]) @@ -95,7 +90,7 @@ public function testLoadEmptyData() $fixture->load(); // should be no error - $rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName())); + $rows = $this->findAll(Yii::$app->mongodb->getCollection(Customer::collectionName())); $this->assertEmpty($rows); } @@ -106,7 +101,6 @@ public function testLoadEmptyData() */ public function testDefaultDataFile() { - $db = $this->getConnection(); $fixturePath = Yii::getAlias('@runtime/fixtures'); $fixtureDataPath = $fixturePath . DIRECTORY_SEPARATOR . 'data'; @@ -138,29 +132,27 @@ class {$className} extends \yii\mongodb\ActiveFixture ['name' => 'name2'], ['name' => 'name3'], ]; - $fixtureDataFile = $fixtureDataPath . DIRECTORY_SEPARATOR . $db->getDefaultDatabaseName() . '.' . Customer::collectionName() . '.php'; + $fixtureDataFile = $fixtureDataPath . DIRECTORY_SEPARATOR . Yii::$app->mongodb->getDefaultDatabaseName() . '.' . Customer::collectionName() . '.php'; $fixtureDataContent = ' $db, 'collectionName' => Customer::collectionName(), ]); $fixture->load(); - $rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName())); + $rows = $this->findAll(Yii::$app->mongodb->getCollection(Customer::collectionName())); $this->assertCount(2, $rows); $fixture = new $className([ - 'db' => $db, 'collectionName' => [ - $db->getDefaultDatabaseName(), + Yii::$app->mongodb->getDefaultDatabaseName(), Customer::collectionName() ], ]); $fixture->load(); - $rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName())); + $rows = $this->findAll(yii::$app->mongodb->getCollection(Customer::collectionName())); $this->assertCount(3, $rows); } -} \ No newline at end of file +} diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index 7c259d6ed..d99d551cb 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -5,8 +5,8 @@ use MongoDB\BSON\Binary; use MongoDB\BSON\ObjectID; use MongoDB\BSON\Regex; +use yii; use yii\mongodb\ActiveQuery; -use yiiunit\extensions\mongodb\data\ar\ActiveRecord; use yiiunit\extensions\mongodb\data\ar\Customer; use yiiunit\extensions\mongodb\data\ar\Animal; use yiiunit\extensions\mongodb\data\ar\Dog; @@ -22,7 +22,6 @@ class ActiveRecordTest extends TestCase protected function setUp() { parent::setUp(); - ActiveRecord::$db = $this->getConnection(); $this->setUpTestRows(); } @@ -37,7 +36,7 @@ protected function tearDown() */ protected function setUpTestRows() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $rows = []; for ($i = 1; $i <= 10; $i++) { $rows[] = [ @@ -282,44 +281,42 @@ public function testExists() public function testScalar() { - $connection = $this->getConnection(); $result = Customer::find() ->select(['name' => true, '_id' => false]) ->orderBy(['name' => SORT_ASC]) ->limit(1) - ->scalar($connection); + ->scalar(); $this->assertSame('name1', $result); $result = Customer::find() ->select(['name' => true, '_id' => false]) ->andWhere(['status' => -1]) - ->scalar($connection); + ->scalar(); $this->assertSame(false, $result); $result = Customer::find() ->select(['name']) ->orderBy(['name' => SORT_ASC]) ->limit(1) - ->scalar($connection); + ->scalar(); $this->assertSame('name1', $result); $result = Customer::find() ->select(['_id']) ->limit(1) - ->scalar($connection); + ->scalar(); $this->assertTrue($result instanceof ObjectID); } public function testColumn() { - $connection = $this->getConnection(); $result = Customer::find() ->select(['name' => true, '_id' => false]) ->orderBy(['name' => SORT_ASC]) ->limit(2) - ->column($connection); + ->column(); $this->assertEquals(['name1', 'name10'], $result); $result = Customer::find() @@ -327,21 +324,21 @@ public function testColumn() ->andWhere(['status' => -1]) ->orderBy(['name' => SORT_ASC]) ->limit(2) - ->column($connection); + ->column(); $this->assertEquals([], $result); $result = Customer::find() ->select(['name']) ->orderBy(['name' => SORT_ASC]) ->limit(2) - ->column($connection); + ->column(); $this->assertEquals(['name1', 'name10'], $result); $result = Customer::find() ->select(['_id']) ->orderBy(['name' => SORT_ASC]) ->limit(2) - ->column($connection); + ->column(); $this->assertTrue($result[0] instanceof ObjectID); $this->assertTrue($result[1] instanceof ObjectID); } diff --git a/tests/ActiveRelationTest.php b/tests/ActiveRelationTest.php index 0f2cd9748..b0ebe9c5e 100644 --- a/tests/ActiveRelationTest.php +++ b/tests/ActiveRelationTest.php @@ -2,7 +2,7 @@ namespace yiiunit\extensions\mongodb; -use yiiunit\extensions\mongodb\data\ar\ActiveRecord; +use yii; use yiiunit\extensions\mongodb\data\ar\Customer; use yiiunit\extensions\mongodb\data\ar\CustomerOrder; use yiiunit\extensions\mongodb\data\ar\Item; @@ -12,7 +12,6 @@ class ActiveRelationTest extends TestCase protected function setUp() { parent::setUp(); - ActiveRecord::$db = $this->getConnection(); $this->setUpTestRows(); } @@ -38,7 +37,7 @@ protected function setUpTestRows() 'status' => $i, ]; } - $customerCollection = $this->getConnection()->getCollection('customer'); + $customerCollection = yii::$app->mongodb->getCollection('customer'); $customers = $customerCollection->batchInsert($customers); $items = []; @@ -48,7 +47,7 @@ protected function setUpTestRows() 'price' => $i, ]; } - $itemCollection = $this->getConnection()->getCollection('item'); + $itemCollection = yii::$app->mongodb->getCollection('item'); $items = $itemCollection->batchInsert($items); $customerOrders = []; @@ -70,7 +69,7 @@ protected function setUpTestRows() ], ]; } - $customerOrderCollection = $this->getConnection()->getCollection('customer_order'); + $customerOrderCollection = yii::$app->mongodb->getCollection('customer_order'); $customerOrderCollection->batchInsert($customerOrders); } diff --git a/tests/BatchQueryResultTest.php b/tests/BatchQueryResultTest.php index af04a3813..d47134eba 100644 --- a/tests/BatchQueryResultTest.php +++ b/tests/BatchQueryResultTest.php @@ -2,9 +2,9 @@ namespace yiiunit\extensions\mongodb; +use yii; use yii\mongodb\BatchQueryResult; use yii\mongodb\Query; -use yiiunit\extensions\mongodb\data\ar\ActiveRecord; use yiiunit\extensions\mongodb\data\ar\Customer; use yiiunit\extensions\mongodb\data\ar\CustomerOrder; @@ -13,7 +13,6 @@ class BatchQueryResultTest extends TestCase protected function setUp() { parent::setUp(); - ActiveRecord::$db = $this->getConnection(); $this->setUpTestRows(); } @@ -38,7 +37,7 @@ protected function setUpTestRows() 'status' => $i, ]; } - $customerCollection = $this->getConnection()->getCollection('customer'); + $customerCollection = yii::$app->mongodb->getCollection('customer'); $customers = $customerCollection->batchInsert($customers); $customerOrders = []; @@ -52,7 +51,7 @@ protected function setUpTestRows() 'number' => $customer['status'] + 100, ]; } - $customerOrderCollection = $this->getConnection()->getCollection('customer_order'); + $customerOrderCollection = yii::$app->mongodb->getCollection('customer_order'); $customerOrderCollection->batchInsert($customerOrders); } @@ -60,12 +59,11 @@ protected function setUpTestRows() public function testQuery() { - $db = $this->getConnection(); // initialize property test $query = new Query(); $query->from('customer')->orderBy('id'); - $result = $query->batch(2, $db); + $result = $query->batch(2); $this->assertTrue($result instanceof BatchQueryResult); $this->assertEquals(2, $result->batchSize); $this->assertTrue($result->query === $query); @@ -74,7 +72,7 @@ public function testQuery() $query = new Query(); $query->from('customer'); $allRows = []; - $batch = $query->batch(2, $db); + $batch = $query->batch(2); foreach ($batch as $rows) { $allRows = array_merge($allRows, $rows); } @@ -84,7 +82,7 @@ public function testQuery() $query = new Query(); $query->from('customer')->orderBy('name'); $allRows = []; - $batch = $query->batch(2, $db); + $batch = $query->batch(2); foreach ($batch as $rows) { $allRows = array_merge($allRows, $rows); } @@ -106,7 +104,7 @@ public function testQuery() $query = new Query(); $query->from('customer')->where(['name' => 'unexistingName']); $allRows = []; - $batch = $query->batch(2, $db); + $batch = $query->batch(2); foreach ($batch as $rows) { $allRows = array_merge($allRows, $rows); } @@ -116,7 +114,7 @@ public function testQuery() $query = new Query(); $query->from('customer')->indexBy('name'); $allRows = []; - foreach ($query->batch(2, $db) as $rows) { + foreach ($query->batch(2) as $rows) { $allRows = array_merge($allRows, $rows); } $this->assertEquals(9, count($allRows)); @@ -128,7 +126,7 @@ public function testQuery() $query = new Query(); $query->from('customer')->orderBy('name'); $allRows = []; - foreach ($query->each(100, $db) as $rows) { + foreach ($query->each(100) as $rows) { $allRows[] = $rows; } $this->assertEquals(9, count($allRows)); @@ -140,7 +138,7 @@ public function testQuery() $query = new Query(); $query->from('customer')->orderBy('name')->indexBy('name'); $allRows = []; - foreach ($query->each(100, $db) as $key => $row) { + foreach ($query->each(100) as $key => $row) { $allRows[$key] = $row; } $this->assertEquals(9, count($allRows)); @@ -151,11 +149,10 @@ public function testQuery() public function testActiveQuery() { - $db = $this->getConnection(); $query = Customer::find()->orderBy('id'); $customers = []; - foreach ($query->batch(2, $db) as $models) { + foreach ($query->batch(2) as $models) { $customers = array_merge($customers, $models); } $this->assertEquals(9, count($customers)); @@ -166,7 +163,7 @@ public function testActiveQuery() // batch with eager loading $query = Customer::find()->with('orders')->orderBy('id'); $customers = []; - foreach ($query->batch(2, $db) as $models) { + foreach ($query->batch(2) as $models) { $customers = array_merge($customers, $models); foreach ($models as $model) { $this->assertTrue($model->isRelationPopulated('orders')); diff --git a/tests/CacheTest.php b/tests/CacheTest.php index 16050d511..4f3596172 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -26,7 +26,6 @@ protected function createCache() { return Yii::createObject([ 'class' => Cache::className(), - 'db' => $this->getConnection(), 'cacheCollection' => static::$cacheCollection, 'gcProbability' => 0, ]); diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index a18c5330d..7a9c3e25c 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -4,6 +4,7 @@ use MongoDB\BSON\ObjectID; use MongoDB\Driver\Cursor; +use yii; class CollectionTest extends TestCase { @@ -19,21 +20,21 @@ protected function tearDown() public function testGetName() { $collectionName = 'customer'; - $collection = $this->getConnection()->getCollection($collectionName); + $collection = yii::$app->mongodb->getCollection($collectionName); $this->assertEquals($collectionName, $collection->name); - $this->assertEquals($this->getConnection()->getDefaultDatabaseName() . '.' . $collectionName, $collection->getFullName()); + $this->assertEquals(yii::$app->mongodb->getDefaultDatabaseName() . '.' . $collectionName, $collection->getFullName()); } public function testFind() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $cursor = $collection->find(); $this->assertTrue($cursor instanceof Cursor); } public function testInsert() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $data = [ 'name' => 'customer 1', 'address' => 'customer 1 address', @@ -49,7 +50,7 @@ public function testInsert() */ public function testFindOne() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $data = [ 'name' => 'customer 1', 'address' => 'customer 1 address', @@ -69,7 +70,7 @@ public function testFindOne() */ public function testFindAll() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $data = [ 'name' => 'customer 1', 'address' => 'customer 1 address', @@ -90,7 +91,7 @@ public function testFindAll() */ public function testBatchInsert() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $rows = [ [ 'name' => 'customer 1', @@ -109,7 +110,7 @@ public function testBatchInsert() public function testSave() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $data = [ 'name' => 'customer 1', 'address' => 'customer 1 address', @@ -124,7 +125,7 @@ public function testSave() */ public function testUpdateBySave() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $data = [ 'name' => 'customer 1', 'address' => 'customer 1 address', @@ -145,7 +146,7 @@ public function testUpdateBySave() */ public function testRemove() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $data = [ 'name' => 'customer 1', 'address' => 'customer 1 address', @@ -165,7 +166,7 @@ public function testRemove() */ public function testRemoveComplexCondition() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $collection->batchInsert([ [ 'name' => 'customer 1', @@ -193,7 +194,7 @@ public function testRemoveComplexCondition() */ public function testUpdate() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $data = [ 'name' => 'customer 1', 'address' => 'customer 1 address', @@ -215,7 +216,7 @@ public function testUpdate() */ public function testGroup() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $rows = [ [ 'name' => 'customer 1', @@ -239,7 +240,7 @@ public function testGroup() public function testFindAndModify() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $rows = [ [ 'name' => 'customer 1', @@ -294,7 +295,7 @@ public function testFindAndModify() */ public function testMapReduce() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $rows = [ [ 'name' => 'customer 1', @@ -327,7 +328,7 @@ public function testMapReduce() ); $this->assertEquals('mapReduceOut', $result); - $outputCollection = $this->getConnection()->getCollection($result); + $outputCollection = yii::$app->mongodb->getCollection($result); $rows = $this->findAll($outputCollection); $expectedRows = [ [ @@ -347,7 +348,7 @@ public function testMapReduce() */ public function testMapReduceInline() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $rows = [ [ 'name' => 'customer 1', @@ -393,7 +394,7 @@ public function testMapReduceInline() public function testCreateIndex() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $columns = [ 'name', 'status' => SORT_DESC @@ -408,7 +409,7 @@ public function testCreateIndex() */ public function testDropIndex() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $collection->createIndex('name'); $this->assertTrue($collection->dropIndex('name')); @@ -425,7 +426,7 @@ public function testDropIndex() */ public function testDropIndexWithPlugin() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $columns = [ 'name' => 'text' @@ -442,7 +443,7 @@ public function testDropIndexWithPlugin() */ public function testDropAllIndexes() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $collection->createIndex('name'); $this->assertEquals(2, $collection->dropAllIndexes()); $indexInfo = $collection->listIndexes(); @@ -451,7 +452,7 @@ public function testDropAllIndexes() public function testCreateIndexes() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $columns = [ ['key' => ['name']], ['key' => ['status' => SORT_DESC]] @@ -466,7 +467,7 @@ public function testCreateIndexes() */ public function testDropIndexes() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $columns = [ [ 'key' => ['name'], @@ -491,7 +492,7 @@ public function testDropIndexes() */ public function testFindByNotObjectId() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $data = [ 'name' => 'customer 1', @@ -516,7 +517,7 @@ public function testFindByNotObjectId() */ public function testInsertMongoBin() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $fileName = __FILE__; $data = [ @@ -534,7 +535,7 @@ public function testInsertMongoBin() */ public function testDistinct() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $rows = [ [ diff --git a/tests/CommandTest.php b/tests/CommandTest.php index d7660d155..2b3a41c77 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -4,6 +4,7 @@ use MongoDB\BSON\ObjectID; use MongoDB\Driver\Cursor; +use yii; use yii\helpers\ArrayHelper; class CommandTest extends TestCase @@ -16,7 +17,7 @@ protected function tearDown() public function testCreateCollection() { - $command = $this->getConnection()->createCommand(); + $command = yii::$app->mongodb->createCommand(); $this->assertTrue($command->createCollection('customer')); } @@ -25,20 +26,20 @@ public function testCreateCollection() */ public function testDropCollection() { - $command = $this->getConnection()->createCommand(); + $command = yii::$app->mongodb->createCommand(); $command->createCollection('customer'); $this->assertTrue($command->dropCollection('customer')); } public function testCount() { - $command = $this->getConnection()->createCommand(); + $command = yii::$app->mongodb->createCommand(); $this->assertEquals(0, $command->count('customer')); } public function testCreateIndexes() { - $command = $this->getConnection()->createCommand(); + $command = yii::$app->mongodb->createCommand(); $this->assertTrue($command->createIndexes('customer', [ [ 'key' => ['name' => +1], @@ -57,7 +58,7 @@ public function testCreateIndexes() */ public function testListIndexes() { - $command = $this->getConnection()->createCommand(); + $command = yii::$app->mongodb->createCommand(); $command->createIndexes('customer', [ [ 'key' => ['name' => +1], @@ -75,7 +76,7 @@ public function testListIndexes() */ public function testDropIndexes() { - $command = $this->getConnection()->createCommand(); + $command = yii::$app->mongodb->createCommand(); $command->createIndexes('customer', [ [ 'key' => ['name' => +1], @@ -100,7 +101,7 @@ public function testDropIndexes() public function testInsert() { - $command = $this->getConnection()->createCommand(); + $command = yii::$app->mongodb->createCommand(); $insertedId = $command->insert('customer', ['name' => 'John']); $this->assertTrue($insertedId instanceof ObjectID); } @@ -110,7 +111,7 @@ public function testInsert() */ public function testBatchInsert() { - $command = $this->getConnection()->createCommand(); + $command = yii::$app->mongodb->createCommand(); $insertedIds = $command->batchInsert('customer', [ ['name' => 'John'], ['name' => 'Sara'], @@ -124,11 +125,10 @@ public function testBatchInsert() */ public function testUpdate() { - $connection = $this->getConnection(); - $newRecordId = $connection->createCommand()->insert('customer', ['name' => 'John']); + $newRecordId = yii::$app->mongodb->createCommand()->insert('customer', ['name' => 'John']); - $result = $connection->createCommand()->update('customer', ['_id' => $newRecordId], ['name' => 'Mike']); + $result = yii::$app->mongodb->createCommand()->update('customer', ['_id' => $newRecordId], ['name' => 'Mike']); $this->assertEquals(1, $result->getModifiedCount()); } @@ -138,11 +138,10 @@ public function testUpdate() */ public function testDelete() { - $connection = $this->getConnection(); - $newRecordId = $connection->createCommand()->insert('customer', ['name' => 'John']); + $newRecordId = yii::$app->mongodb->createCommand()->insert('customer', ['name' => 'John']); - $result = $connection->createCommand()->delete('customer', ['_id' => $newRecordId]); + $result = yii::$app->mongodb->createCommand()->delete('customer', ['_id' => $newRecordId]); $this->assertEquals(1, $result->getDeletedCount()); } @@ -152,11 +151,10 @@ public function testDelete() */ public function testFind() { - $connection = $this->getConnection(); - $connection->createCommand()->insert('customer', ['name' => 'John']); + yii::$app->mongodb->createCommand()->insert('customer', ['name' => 'John']); - $cursor = $connection->createCommand()->find('customer', []); + $cursor = yii::$app->mongodb->createCommand()->find('customer', []); $rows = $cursor->toArray(); $this->assertCount(1, $rows); $this->assertEquals('John', $rows[0]['name']); @@ -167,7 +165,6 @@ public function testFind() */ public function testFindAndModify() { - $connection = $this->getConnection(); $rows = [ [ 'name' => 'customer 1', @@ -180,20 +177,20 @@ public function testFindAndModify() 'amount' => 200, ], ]; - $command = $connection->createCommand(); + $command = yii::$app->mongodb->createCommand(); $command->batchInsert('customer', $rows); // increment field - $result = $connection->createCommand()->findAndModify('customer', ['name' => 'customer 1'], ['$inc' => ['status' => 1]]); + $result = yii::$app->mongodb->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']); + $cursor = yii::$app->mongodb->createCommand()->find('customer', ['name' => 'customer 1']); $newResult = current($cursor->toArray()); $this->assertEquals(2, $newResult['status']); // $set and return modified document - $result = $connection->createCommand()->findAndModify( + $result = yii::$app->mongodb->createCommand()->findAndModify( 'customer', ['name' => 'customer 2'], ['$set' => ['status' => 2]], @@ -207,7 +204,7 @@ public function testFindAndModify() 'name' => 'customer 3', 'city' => 'Minsk' ]; - $result = $connection->createCommand()->findAndModify( + $result = yii::$app->mongodb->createCommand()->findAndModify( 'customer', ['name' => 'customer 2'], $data, @@ -219,7 +216,7 @@ public function testFindAndModify() // Test exceptions $this->expectException('\yii\mongodb\Exception'); - $connection->createCommand()->findAndModify('customer',['name' => 'customer 1'], ['$wrongOperator' => ['status' => 1]]); + yii::$app->mongodb->createCommand()->findAndModify('customer',['name' => 'customer 1'], ['$wrongOperator' => ['status' => 1]]); } /** @@ -227,7 +224,6 @@ public function testFindAndModify() */ public function testAggregate() { - $connection = $this->getConnection(); $rows = [ [ 'name' => 'customer 1', @@ -240,7 +236,7 @@ public function testAggregate() 'amount' => 200, ], ]; - $command = $connection->createCommand(); + $command = yii::$app->mongodb->createCommand(); $command->batchInsert('customer', $rows); $pipelines = [ @@ -256,7 +252,7 @@ public function testAggregate() ] ] ]; - $result = $connection->createCommand()->aggregate('customer', $pipelines); + $result = yii::$app->mongodb->createCommand()->aggregate('customer', $pipelines); $this->assertEquals(['_id' => '1', 'total' => 300], $result[0]); } @@ -268,7 +264,6 @@ public function testAggregate() */ public function testAggregateCursor() { - $connection = $this->getConnection(); $rows = [ [ 'name' => 'customer 1', @@ -291,7 +286,7 @@ public function testAggregateCursor() 'amount' => 200, ], ]; - $command = $connection->createCommand(); + $command = yii::$app->mongodb->createCommand(); $command->batchInsert('customer', $rows); $pipelines = [ @@ -307,7 +302,7 @@ public function testAggregateCursor() ] ] ]; - $result = $connection->createCommand()->aggregate('customer', $pipelines, ['cursor' => ['batchSize' => 2]]); + $result = yii::$app->mongodb->createCommand()->aggregate('customer', $pipelines, ['cursor' => ['batchSize' => 2]]); $this->assertTrue($result instanceof Cursor); $this->assertEquals(['_id' => '1', 'total' => 600], $result->toArray()[0]); @@ -318,11 +313,10 @@ public function testAggregateCursor() */ public function testExplain() { - $connection = $this->getConnection(); - $connection->createCommand()->insert('customer', ['name' => 'John']); + yii::$app->mongodb->createCommand()->insert('customer', ['name' => 'John']); - $result = $connection->createCommand()->explain('customer', [ + $result = yii::$app->mongodb->createCommand()->explain('customer', [ 'filter' => [ 'name' => 'John' ], @@ -337,11 +331,10 @@ public function testExplain() */ public function testListCollections() { - $connection = $this->getConnection(); - $connection->createCommand()->createCollection('customer'); + yii::$app->mongodb->createCommand()->createCollection('customer'); - $collections = $connection->createCommand()->listCollections(); + $collections = yii::$app->mongodb->createCommand()->listCollections(); $collectionNames = ArrayHelper::getColumn($collections, 'name'); $this->assertContains('customer', $collectionNames); } @@ -354,22 +347,21 @@ public function testListCollections() */ public function testUpdateUpsert() { - $connection = $this->getConnection(); - $connection->createCommand()->insert('customer', ['name' => 'John']); + yii::$app->mongodb->createCommand()->insert('customer', ['name' => 'John']); - $result = $connection->createCommand() + $result = yii::$app->mongodb->createCommand() ->update('customer', ['name' => 'Mike'], ['name' => 'Jack']); $this->assertEquals(0, $result->getModifiedCount()); $this->assertEquals(0, $result->getUpsertedCount()); - $this->assertEquals(1, $connection->createCommand()->count('customer')); + $this->assertEquals(1, yii::$app->mongodb->createCommand()->count('customer')); - $result = $connection->createCommand() + $result = yii::$app->mongodb->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')); + $this->assertEquals(2, yii::$app->mongodb->createCommand()->count('customer')); } } \ No newline at end of file diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index a3665a662..123ff4a53 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -2,6 +2,7 @@ namespace yiiunit\extensions\mongodb; +use yii; use yii\mongodb\Collection; use yii\mongodb\Command; use yii\mongodb\file\Collection as FileCollection; @@ -13,7 +14,7 @@ class ConnectionTest extends TestCase { public function testConstruct() { - $connection = $this->getConnection(false); + $connection = $this->getConnection(true,false); $params = $this->mongoDbConfig; $connection->open(); @@ -25,7 +26,7 @@ public function testConstruct() public function testOpenClose() { - $connection = $this->getConnection(false, false); + $connection = $this->getConnection(true, false); $this->assertFalse($connection->isActive); $this->assertEquals(null, $connection->manager); @@ -46,17 +47,16 @@ public function testOpenClose() public function testGetDatabase() { - $connection = $this->getConnection(); - $database = $connection->getDatabase($connection->defaultDatabaseName); + $database = yii::$app->mongodb->getDatabase(yii::$app->mongodb->defaultDatabaseName); $this->assertTrue($database instanceof Database); - $this->assertSame($connection, $database->connection); - $this->assertSame($connection->defaultDatabaseName, $database->name); + $this->assertSame(yii::$app->mongodb, $database->connection); + $this->assertSame(yii::$app->mongodb->defaultDatabaseName, $database->name); - $database2 = $connection->getDatabase($connection->defaultDatabaseName); + $database2 = yii::$app->mongodb->getDatabase(yii::$app->mongodb->defaultDatabaseName); $this->assertTrue($database === $database2); - $databaseRefreshed = $connection->getDatabase($connection->defaultDatabaseName, true); + $databaseRefreshed = yii::$app->mongodb->getDatabase(yii::$app->mongodb->defaultDatabaseName, true); $this->assertFalse($database === $databaseRefreshed); } @@ -110,15 +110,14 @@ public function testGetDefaultDatabase() */ public function testGetCollection() { - $connection = $this->getConnection(); - $collection = $connection->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $this->assertTrue($collection instanceof Collection); - $collection2 = $connection->getCollection('customer'); + $collection2 = yii::$app->mongodb->getCollection('customer'); $this->assertTrue($collection === $collection2); - $collection2 = $connection->getCollection('customer', true); + $collection2 = yii::$app->mongodb->getCollection('customer', true); $this->assertFalse($collection === $collection2); } @@ -127,31 +126,28 @@ public function testGetCollection() */ public function testGetFileCollection() { - $connection = $this->getConnection(); - $collection = $connection->getFileCollection('testfs'); + $collection = yii::$app->mongodb->getFileCollection('testfs'); $this->assertTrue($collection instanceof FileCollection); - $collection2 = $connection->getFileCollection('testfs'); + $collection2 = yii::$app->mongodb->getFileCollection('testfs'); $this->assertTrue($collection === $collection2); - $collection2 = $connection->getFileCollection('testfs', true); + $collection2 = yii::$app->mongodb->getFileCollection('testfs', true); $this->assertFalse($collection === $collection2); } public function testGetQueryBuilder() { - $connection = $this->getConnection(); - $this->assertTrue($connection->getQueryBuilder() instanceof QueryBuilder); + $this->assertTrue(yii::$app->mongodb->getQueryBuilder() instanceof QueryBuilder); } public function testCreateCommand() { - $connection = $this->getConnection(); - $command = $connection->createCommand(); + $command = yii::$app->mongodb->createCommand(); $this->assertTrue($command instanceof Command); - $this->assertSame($connection, $command->db); + $this->assertSame(yii::$app->mongodb, $command->db); } } diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index 35700bb29..5999d8378 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -2,6 +2,7 @@ namespace yiiunit\extensions\mongodb; +use yii; use yii\mongodb\Collection; use yii\mongodb\Command; use yii\mongodb\file\Collection as FileCollection; @@ -19,7 +20,7 @@ protected function tearDown() public function testGetCollection() { - $database = $this->getConnection()->getDatabase(); + $database = yii::$app->mongodb->getDatabase(); $collection = $database->getCollection('customer'); $this->assertTrue($collection instanceof Collection); @@ -34,7 +35,7 @@ public function testGetCollection() public function testGetFileCollection() { - $database = $this->getConnection()->getDatabase(); + $database = yii::$app->mongodb->getDatabase(); $collection = $database->getFileCollection('testfs'); $this->assertTrue($collection instanceof FileCollection); @@ -49,7 +50,7 @@ public function testGetFileCollection() public function testCreateCommand() { - $database = $this->getConnection()->getDatabase(); + $database = yii::$app->mongodb->getDatabase(); $command = $database->createCommand(); $this->assertTrue($command instanceof Command); @@ -58,7 +59,7 @@ public function testCreateCommand() public function testCreateCollection() { - $database = $this->getConnection()->getDatabase(); + $database = yii::$app->mongodb->getDatabase(); $this->assertTrue($database->createCollection('customer')); } } diff --git a/tests/LogBuilderTest.php b/tests/LogBuilderTest.php index 08b9c9b29..83bc5c4c0 100644 --- a/tests/LogBuilderTest.php +++ b/tests/LogBuilderTest.php @@ -4,6 +4,7 @@ use MongoDB\BSON\Javascript; use MongoDB\BSON\ObjectID; +use yii; class LogBuilderTest extends TestCase { @@ -37,7 +38,7 @@ public function dataProviderEncodeData() */ public function testEncodeData($data, $expectedResult) { - $logBuilder = $this->getConnection()->getLogBuilder(); + $logBuilder = yii::$app->mongodb->getLogBuilder(); $this->assertTrue(strcasecmp($expectedResult, $logBuilder->encodeData($data)) === 0); } } \ No newline at end of file diff --git a/tests/MigrationTest.php b/tests/MigrationTest.php index a14a8fb51..f7438bb34 100644 --- a/tests/MigrationTest.php +++ b/tests/MigrationTest.php @@ -12,19 +12,11 @@ protected function tearDown() parent::tearDown(); } - /** - * @return Migration migration instance. - */ - protected function createMigration() - { - return new Migration(['db' => $this->getConnection()]); - } - // Tests : public function testCollectionOperations() { - $migration = $this->createMigration(); + $migration = new Migration; $migration->createCollection('customer'); $this->assertNotEmpty($migration->db->getDatabase()->listCollections(['name' => 'customer'])); @@ -35,7 +27,7 @@ public function testCollectionOperations() public function testIndexOperations() { - $migration = $this->createMigration(); + $migration = new Migration; $migration->createIndexes('customer', [ ['key' => 'name'] @@ -58,7 +50,7 @@ public function testIndexOperations() public function testDataOperations() { - $migration = $this->createMigration(); + $migration = new Migration; $id = $migration->insert('customer', ['name' => 'John Doe']); $this->assertTrue($id instanceof ObjectID); @@ -85,7 +77,7 @@ public function testDataOperations() */ public function testCommandOutput() { - $migration = $this->createMigration(); + $migration = new Migration; $migration->compact = false; $migration->createCollection('customer'); diff --git a/tests/QueryRunTest.php b/tests/QueryRunTest.php index c3a97616f..318da4080 100644 --- a/tests/QueryRunTest.php +++ b/tests/QueryRunTest.php @@ -3,6 +3,7 @@ namespace yiiunit\extensions\mongodb; use MongoDB\BSON\ObjectID; +use yii; use yii\mongodb\Query; class QueryRunTest extends TestCase @@ -24,7 +25,7 @@ protected function tearDown() */ protected function setUpTestRows() { - $collection = $this->getConnection()->getCollection('customer'); + $collection = yii::$app->mongodb->getCollection('customer'); $rows = []; for ($i = 1; $i <= 10; $i++) { $rows[] = [ @@ -46,43 +47,39 @@ protected function setUpTestRows() public function testAll() { - $connection = $this->getConnection(); $query = new Query(); - $rows = $query->from('customer')->all($connection); + $rows = $query->from('customer')->all(); $this->assertEquals(10, count($rows)); } public function testDirectMatch() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->where(['name' => 'name1']) - ->all($connection); + ->all(); $this->assertEquals(1, count($rows)); $this->assertEquals('name1', $rows[0]['name']); } public function testIndexBy() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->indexBy('name') - ->all($connection); + ->all(); $this->assertEquals(10, count($rows)); $this->assertNotEmpty($rows['name1']); } public function testInCondition() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->where([ 'name' => ['name1', 'name5'] ]) - ->all($connection); + ->all(); $this->assertEquals(2, count($rows)); $this->assertEquals('name1', $rows[0]['name']); $this->assertEquals('name5', $rows[1]['name']); @@ -90,18 +87,17 @@ public function testInCondition() public function testNotInCondition() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->where(['not in', 'name', ['name1', 'name5']]) - ->all($connection); + ->all(); $this->assertEquals(8, count($rows)); $query = new Query(); $rows = $query->from('customer') ->where(['not in', 'name', ['name1']]) - ->all($connection); + ->all(); $this->assertEquals(9, count($rows)); } @@ -110,7 +106,6 @@ public function testNotInCondition() */ public function testCompositeInCondition() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->where([ @@ -122,7 +117,7 @@ public function testCompositeInCondition() ['status' => 5, 'name' => 'name7'], ] ]) - ->all($connection); + ->all(); $this->assertEquals(2, count($rows)); $this->assertEquals('name1', $rows[0]['name']); $this->assertEquals('name3', $rows[1]['name']); @@ -130,12 +125,11 @@ public function testCompositeInCondition() public function testOrCondition() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->where(['name' => 'name1']) ->orWhere(['address' => 'address5']) - ->all($connection); + ->all(); $this->assertEquals(2, count($rows)); $this->assertEquals('name1', $rows[0]['name']); $this->assertEquals('address5', $rows[1]['address']); @@ -143,21 +137,19 @@ public function testOrCondition() public function testCombinedInAndCondition() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->where([ 'name' => ['name1', 'name5'] ]) ->andWhere(['name' => 'name1']) - ->all($connection); + ->all(); $this->assertEquals(1, count($rows)); $this->assertEquals('name1', $rows[0]['name']); } public function testCombinedInLikeAndCondition() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->where([ @@ -165,14 +157,13 @@ public function testCombinedInLikeAndCondition() ]) ->andWhere(['LIKE', 'name', 'me1']) ->andWhere(['name' => 'name10']) - ->all($connection); + ->all(); $this->assertEquals(1, count($rows)); $this->assertEquals('name10', $rows[0]['name']); } public function testNestedCombinedInAndCondition() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->where([ @@ -185,7 +176,7 @@ public function testNestedCombinedInAndCondition() ['name' => ['name4', 'name5', 'name6']], ['name' => 'name6'] ]) - ->all($connection); + ->all(); $this->assertEquals(2, count($rows)); $this->assertEquals('name1', $rows[0]['name']); $this->assertEquals('name6', $rows[1]['name']); @@ -193,40 +184,37 @@ public function testNestedCombinedInAndCondition() public function testOrder() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->orderBy(['name' => SORT_DESC]) - ->all($connection); + ->all(); $this->assertEquals('name9', $rows[0]['name']); $query = new Query(); $rows = $query->from('customer') ->orderBy(['avatar.height' => SORT_DESC]) - ->all($connection); + ->all(); $this->assertEquals('name10', $rows[0]['name']); } public function testMatchPlainId() { - $connection = $this->getConnection(); $query = new Query(); - $row = $query->from('customer')->one($connection); + $row = $query->from('customer')->one(); $query = new Query(); $rows = $query->from('customer') ->where(['_id' => $row['_id']->__toString()]) - ->all($connection); + ->all(); $this->assertEquals(1, count($rows)); } public function testRegex() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->where(['REGEX', 'name', '/me1/']) - ->all($connection); + ->all(); $this->assertEquals(2, count($rows)); $this->assertEquals('name1', $rows[0]['name']); $this->assertEquals('name10', $rows[1]['name']); @@ -234,12 +222,11 @@ public function testRegex() public function testLike() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->where(['LIKE', 'name', 'me1']) - ->all($connection); + ->all(); $this->assertEquals(2, count($rows)); $this->assertEquals('name1', $rows[0]['name']); $this->assertEquals('name10', $rows[1]['name']); @@ -247,76 +234,72 @@ public function testLike() $query = new Query(); $rowsUppercase = $query->from('customer') ->where(['LIKE', 'name', 'ME1']) - ->all($connection); + ->all(); $this->assertEquals($rows, $rowsUppercase); } public function testCompare() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->where(['$gt', 'status', 8]) - ->all($connection); + ->all(); $this->assertEquals(2, count($rows)); $query = new Query(); $rows = $query->from('customer') ->where(['>', 'status', 8]) - ->all($connection); + ->all(); $this->assertEquals(2, count($rows)); $query = new Query(); $rows = $query->from('customer') ->where(['<=', 'status', 3]) - ->all($connection); + ->all(); $this->assertEquals(3, count($rows)); } public function testNot() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->where(['not', 'status', ['$gte' => 10]]) - ->all($connection); + ->all(); $this->assertEquals(9, count($rows)); $query = new Query(); $rows = $query->from('customer') ->where(['not', 'name', 'name1']) - ->all($connection); + ->all(); $this->assertEquals(9, count($rows)); $query = new Query(); $rows = $query->from('customer') ->where(['not', 'name', null]) - ->all($connection); + ->all(); $this->assertEquals(10, count($rows)); } public function testExists() { - $connection = $this->getConnection(); $query = new Query(); $exists = $query->from('customer') ->where(['name' => 'name1']) - ->exists($connection); + ->exists(); $this->assertTrue($exists); $query = new Query(); $exists = $query->from('customer') ->where(['name' => 'un-existing-name']) - ->exists($connection); + ->exists(); $this->assertFalse($exists); } public function testModify() { - $connection = $this->getConnection(); $query = new Query(); @@ -324,19 +307,19 @@ public function testModify() $newName = 'new name'; $row = $query->from('customer') ->where(['name' => $searchName]) - ->modify(['$set' => ['name' => $newName]], ['new' => false], $connection); + ->modify(['$set' => ['name' => $newName]], ['new' => false]); $this->assertEquals($searchName, $row['name']); $searchName = 'name7'; $newName = 'new name'; $row = $query->from('customer') ->where(['name' => $searchName]) - ->modify(['$set' => ['name' => $newName]], ['new' => true], $connection); + ->modify(['$set' => ['name' => $newName]], ['new' => true]); $this->assertEquals($newName, $row['name']); $row = $query->from('customer') ->where(['name' => 'not existing name']) - ->modify(['$set' => ['name' => 'new name']], ['new' => false], $connection); + ->modify(['$set' => ['name' => 'new name']], ['new' => false]); $this->assertNull($row); } @@ -348,7 +331,6 @@ public function testModify() */ public function testInConditionIgnoreKeys() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') /*->where([ @@ -361,7 +343,7 @@ public function testInConditionIgnoreKeys() 10 => 'name1', 15 => 'name5' ]]) - ->all($connection); + ->all(); $this->assertEquals(2, count($rows)); $this->assertEquals('name1', $rows[0]['name']); $this->assertEquals('name5', $rows[1]['name']); @@ -373,7 +355,7 @@ public function testInConditionIgnoreKeys() 10 => $rows[0]['_id'], 15 => $rows[1]['_id'] ]]) - ->all($connection); + ->all(); $this->assertEquals(2, count($rows)); } @@ -382,12 +364,11 @@ public function testInConditionIgnoreKeys() */ public function testSelect() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('customer') ->select(['name' => true, '_id' => false]) ->limit(1) - ->all($connection); + ->all(); $row = array_pop($rows); $this->assertArrayHasKey('name', $row); $this->assertArrayNotHasKey('address', $row); @@ -396,21 +377,20 @@ public function testSelect() public function testScalar() { - $connection = $this->getConnection(); $result = (new Query()) ->select(['name' => true, '_id' => false]) ->from('customer') ->orderBy(['name' => SORT_ASC]) ->limit(1) - ->scalar($connection); + ->scalar(); $this->assertSame('name1', $result); $result = (new Query()) ->select(['name' => true, '_id' => false]) ->from('customer') ->andWhere(['status' => -1]) - ->scalar($connection); + ->scalar(); $this->assertSame(false, $result); $result = (new Query()) @@ -418,26 +398,25 @@ public function testScalar() ->from('customer') ->orderBy(['name' => SORT_ASC]) ->limit(1) - ->scalar($connection); + ->scalar(); $this->assertSame('name1', $result); $result = (new Query()) ->select(['_id']) ->from('customer') ->limit(1) - ->scalar($connection); + ->scalar(); $this->assertTrue($result instanceof ObjectID); } public function testColumn() { - $connection = $this->getConnection(); $result = (new Query())->from('customer') ->select(['name' => true, '_id' => false]) ->orderBy(['name' => SORT_ASC]) ->limit(2) - ->column($connection); + ->column(); $this->assertEquals(['name1', 'name10'], $result); $result = (new Query())->from('customer') @@ -445,21 +424,21 @@ public function testColumn() ->andWhere(['status' => -1]) ->orderBy(['name' => SORT_ASC]) ->limit(2) - ->column($connection); + ->column(); $this->assertEquals([], $result); $result = (new Query())->from('customer') ->select(['name']) ->orderBy(['name' => SORT_ASC]) ->limit(2) - ->column($connection); + ->column(); $this->assertEquals(['name1', 'name10'], $result); $result = (new Query())->from('customer') ->select(['_id']) ->orderBy(['name' => SORT_ASC]) ->limit(2) - ->column($connection); + ->column(); $this->assertTrue($result[0] instanceof ObjectID); $this->assertTrue($result[1] instanceof ObjectID); } @@ -469,14 +448,13 @@ public function testColumn() */ public function testColumnIndexBy() { - $connection = $this->getConnection(); $result = (new Query())->from('customer') ->select(['name']) ->orderBy(['name' => SORT_ASC]) ->limit(2) ->indexBy('status') - ->column($connection); + ->column(); $this->assertEquals([1 => 'name1', 10 => 'name10'], $result); $result = (new Query())->from('customer') @@ -486,7 +464,7 @@ public function testColumnIndexBy() ->indexBy(function ($row) { return $row['status'] * 2; }) - ->column($connection); + ->column(); $this->assertEquals([2 => 'name1', 20 => 'name10'], $result); $result = (new Query())->from('customer') @@ -494,7 +472,7 @@ public function testColumnIndexBy() ->orderBy(['name' => SORT_ASC]) ->limit(2) ->indexBy('name') - ->column($connection); + ->column(); $this->assertEquals(['name1' => 'name1', 'name10' => 'name10'], $result); } @@ -505,84 +483,82 @@ public function testEmulateExecution() $this->markTestSkipped('"yii2" version 2.0.11 or higher required'); } - $db = $this->getConnection(); - - $this->assertGreaterThan(0, $query->from('customer')->count('*', $db)); + $this->assertGreaterThan(0, $query->from('customer')->count()); $rows = (new Query()) ->from('customer') ->emulateExecution() - ->all($db); + ->all(); $this->assertSame([], $rows); $row = (new Query()) ->from('customer') ->emulateExecution() - ->one($db); + ->one(); $this->assertSame(false, $row); $exists = (new Query()) ->from('customer') ->emulateExecution() - ->exists($db); + ->exists(); $this->assertSame(false, $exists); $count = (new Query()) ->from('customer') ->emulateExecution() - ->count('*', $db); + ->count(); $this->assertSame(0, $count); $sum = (new Query()) ->from('customer') ->emulateExecution() - ->sum('id', $db); + ->sum('id'); $this->assertSame(0, $sum); $sum = (new Query()) ->from('customer') ->emulateExecution() - ->average('id', $db); + ->average('id'); $this->assertSame(0, $sum); $max = (new Query()) ->from('customer') ->emulateExecution() - ->max('id', $db); + ->max('id'); $this->assertSame(null, $max); $min = (new Query()) ->from('customer') ->emulateExecution() - ->min('id', $db); + ->min('id'); $this->assertSame(null, $min); $scalar = (new Query()) ->select(['id']) ->from('customer') ->emulateExecution() - ->scalar($db); + ->scalar(); $this->assertSame(null, $scalar); $column = (new Query()) ->select(['id']) ->from('customer') ->emulateExecution() - ->column($db); + ->column(); $this->assertSame([], $column); $row = (new Query()) ->select(['id']) ->from('customer') ->emulateExecution() - ->modify(['name' => 'new name'], [], $db); + ->modify(['name' => 'new name']); $this->assertSame(null, $row); $values = (new Query()) ->select(['id']) ->from('customer') ->emulateExecution() - ->distinct('name', $db); + ->distinct('name'); $this->assertSame([], $values); } @@ -593,18 +569,17 @@ public function testEmulateExecution() */ public function testOffsetLimit() { - $db = $this->getConnection(); $rows = (new Query()) ->from('customer') ->limit(2) - ->all($db); + ->all(); $this->assertCount(2, $rows); $rows = (new Query()) ->from('customer') ->limit(-1) - ->all($db); + ->all(); $this->assertCount(10, $rows); $rows = (new Query()) @@ -612,7 +587,7 @@ public function testOffsetLimit() ->orderBy(['name' => SORT_ASC]) ->offset(2) ->limit(1) - ->all($db); + ->all(); $this->assertCount(1, $rows); $this->assertEquals('name2', $rows[0]['name']); @@ -621,54 +596,55 @@ public function testOffsetLimit() ->orderBy(['name' => SORT_ASC]) ->offset(-1) ->limit(1) - ->all($db); + ->all(); $this->assertCount(1, $rows); $this->assertEquals('name1', $rows[0]['name']); } public function testDistinct() { - $db = $this->getConnection(); $rows = (new Query()) ->from('customer') - ->distinct('group', $db); + ->distinct('group'); + + #difference order after php 7 + sort($rows); - $this->assertSame(['odd', 'even'], $rows); + $this->assertSame(['even', 'odd'], $rows); } public function testAggregationShortcuts() { - $db = $this->getConnection(); $max = (new Query()) ->from('customer') ->where(['group' => 'odd']) - ->count('*', $db); + ->count(); $this->assertSame(5, $max); $max = (new Query()) ->from('customer') ->where(['group' => 'even']) - ->max('status', $db); + ->max('status'); $this->assertSame(10, $max); $max = (new Query()) ->from('customer') ->where(['group' => 'even']) - ->min('status', $db); + ->min('status'); $this->assertSame(2, $max); $max = (new Query()) ->from('customer') ->where(['group' => 'even']) - ->sum('status', $db); + ->sum('status'); $this->assertSame(30, $max); $max = (new Query()) ->from('customer') ->where(['group' => 'even']) - ->average('status', $db); + ->average('status'); $this->assertEquals(6, $max); } } \ No newline at end of file diff --git a/tests/SessionTest.php b/tests/SessionTest.php index b1f8e5173..77c4c68e5 100755 --- a/tests/SessionTest.php +++ b/tests/SessionTest.php @@ -26,7 +26,6 @@ protected function createSession() { return Yii::createObject([ 'class' => Session::className(), - 'db' => $this->getConnection(), 'sessionCollection' => static::$sessionCollection, ]); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 7c876e453..df91a00a6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -34,7 +34,7 @@ protected function setUp() if (!empty($config)) { $this->mongoDbConfig = $config; } - //$this->mockApplication(); + $this->mockApplication(); } protected function tearDown() @@ -74,6 +74,9 @@ protected function mockApplication($config = [], $appClass = '\yii\console\Appli 'basePath' => __DIR__, 'vendorPath' => $this->getVendorPath(), 'runtimePath' => dirname(__DIR__) . '/runtime', + 'components' => [ + 'mongodb' => $this->getConnection(), + ], ], $config)); } diff --git a/tests/console/controllers/MigrateControllerTest.php b/tests/console/controllers/MigrateControllerTest.php index 44b821c2e..593860532 100644 --- a/tests/console/controllers/MigrateControllerTest.php +++ b/tests/console/controllers/MigrateControllerTest.php @@ -47,22 +47,17 @@ public function setUp() $this->migrationNamespace = 'yiiunit\extensions\mongodb\runtime\test_migrations'; $this->setUpMigrationPath(); - - $this->mockApplication(); - Yii::$app->setComponents(['mongodb' => $this->getConnection()]); } public function tearDown() { - parent::tearDown(); - if (extension_loaded('mongodb')) { - try { - $this->getConnection()->getCollection('migration')->drop(); - } catch (Exception $e) { - // shutdown exception - } + try { + yii::$app->mongodb->getCollection('migration')->drop(); + } catch (Exception $e) { + // shutdown exception } $this->tearDownMigrationPath(); + parent::tearDown(); } public function setUpMigrationPath() @@ -501,7 +496,6 @@ public function testNamespaceTo() */ public function testGetMigrationHistory() { - $connection = $this->getConnection(); $controllerConfig = [ 'migrationPath' => null, @@ -509,9 +503,9 @@ public function testGetMigrationHistory() ]; $controller = $this->createMigrateController($controllerConfig); - $controller->db = $this->getConnection(); + $controller->db = yii::$app->mongodb; - $connection->createCommand()->batchInsert('migration', [ + yii::$app->mongodb->createCommand()->batchInsert('migration', [ [ 'version' => 'app\migrations\M140506102106One', 'apply_time' => 10, @@ -574,9 +568,7 @@ public function testRefreshMigration() $this->markTestSkipped('Method "yii\console\controllers\BaseMigrateController::actionFresh()" does not exist in this Yii framework version.'); } - $connection = $this->getConnection(); - - $collection = $connection->getCollection('hall_of_fame'); + $collection = yii::$app->mongodb->getCollection('hall_of_fame'); $collection->insert(['name' => 'Qiang Xue']); $collection->insert(['name' => 'Alexander Makarov']); @@ -585,6 +577,6 @@ public function testRefreshMigration() $this->assertContains('Collection hall_of_fame dropped.', $result); $this->assertContains('No new migrations found. Your system is up-to-date.', $result); - $this->assertEmpty($connection->getDatabase()->listCollections(['name' => $collection->name])); + $this->assertEmpty(yii::$app->mongodb->getDatabase()->listCollections(['name' => $collection->name])); } } \ No newline at end of file diff --git a/tests/data/ar/ActiveRecord.php b/tests/data/ar/ActiveRecord.php deleted file mode 100644 index 7229ea957..000000000 --- a/tests/data/ar/ActiveRecord.php +++ /dev/null @@ -1,20 +0,0 @@ -getConnection(); $this->dropFileCollection(CustomerFile::collectionName()); $this->setUpTestRows(); $filePath = $this->getTestFilePath(); @@ -55,7 +54,7 @@ protected function getTestFilePath() */ protected function setUpTestRows() { - $collection = $this->getConnection()->getFileCollection(CustomerFile::collectionName()); + $collection = yii::$app->mongodb->getFileCollection(CustomerFile::collectionName()); $rows = []; for ($i = 1; $i <= 10; $i++) { $record = [ diff --git a/tests/file/ActiveRelationTest.php b/tests/file/ActiveRelationTest.php index 9c5eb47ed..4a2a279ca 100644 --- a/tests/file/ActiveRelationTest.php +++ b/tests/file/ActiveRelationTest.php @@ -5,6 +5,7 @@ use yiiunit\extensions\mongodb\data\ar\Customer; use yiiunit\extensions\mongodb\data\ar\file\CustomerFile; use yiiunit\extensions\mongodb\TestCase; +use yii; /** * @group file @@ -14,8 +15,6 @@ class ActiveRelationTest extends TestCase protected function setUp() { parent::setUp(); - \yiiunit\extensions\mongodb\data\ar\ActiveRecord::$db = $this->getConnection(); - \yiiunit\extensions\mongodb\data\ar\file\ActiveRecord::$db = $this->getConnection(); $this->setUpTestRows(); } @@ -31,7 +30,7 @@ protected function tearDown() */ protected function setUpTestRows() { - $fileCollection = $this->getConnection()->getFileCollection(CustomerFile::collectionName()); + $fileCollection = yii::$app->mongodb->getFileCollection(CustomerFile::collectionName()); $customers = []; $files = []; for ($i = 1; $i <= 5; $i++) { @@ -52,7 +51,7 @@ protected function setUpTestRows() 'file_id' => $file['_id'], ]; } - $customerCollection = $this->getConnection()->getCollection(Customer::collectionName()); + $customerCollection = yii::$app->mongodb->getCollection(Customer::collectionName()); $customers = $customerCollection->batchInsert($customers); } diff --git a/tests/file/CollectionTest.php b/tests/file/CollectionTest.php index 07c4350d2..3fb263cff 100644 --- a/tests/file/CollectionTest.php +++ b/tests/file/CollectionTest.php @@ -3,6 +3,7 @@ namespace yiiunit\extensions\mongodb\file; use MongoDB\BSON\ObjectID; +use yii; use yii\mongodb\file\Cursor; use yii\mongodb\file\Download; use yiiunit\extensions\mongodb\TestCase; @@ -22,7 +23,7 @@ protected function tearDown() public function testGetChunkCollection() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $chunkCollection = $collection->getChunkCollection(); $this->assertTrue($chunkCollection instanceof \yii\mongodb\Collection); $this->assertTrue($chunkCollection->database instanceof \yii\mongodb\Database); @@ -30,7 +31,7 @@ public function testGetChunkCollection() public function testGetFileCollection() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $fileCollection = $collection->getFileCollection(); $this->assertTrue($fileCollection instanceof \yii\mongodb\Collection); $this->assertTrue($fileCollection->database instanceof \yii\mongodb\Database); @@ -38,7 +39,7 @@ public function testGetFileCollection() public function testEnsureIndexes() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $collection->ensureIndexes(); $this->assertCount(2, $collection->listIndexes()); @@ -54,14 +55,14 @@ public function testEnsureIndexes() public function testFind() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $cursor = $collection->find(); $this->assertTrue($cursor instanceof Cursor); } public function testInsertFile() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $filename = __FILE__; $id = $collection->insertFile($filename); @@ -77,7 +78,7 @@ public function testInsertFile() public function testInsertFileContent() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $bytes = 'Test file content'; $id = $collection->insertFileContent($bytes); @@ -97,7 +98,7 @@ public function testInsertFileContent() */ public function testGet() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $bytes = 'Test file content'; $id = $collection->insertFileContent($bytes); @@ -112,7 +113,7 @@ public function testGet() */ public function testDeleteFile() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $bytes = 'Test file content'; $id = $collection->insertFileContent($bytes); @@ -128,7 +129,7 @@ public function testDeleteFile() */ public function testRemove() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); for ($i = 1; $i <=10; $i++) { $bytes = 'Test file content ' . $i; diff --git a/tests/file/DownloadTest.php b/tests/file/DownloadTest.php index 0fb3dd037..512743e02 100644 --- a/tests/file/DownloadTest.php +++ b/tests/file/DownloadTest.php @@ -3,7 +3,7 @@ namespace yiiunit\extensions\mongodb\file; use yiiunit\extensions\mongodb\TestCase; - +use yii; /** * @group file */ @@ -19,7 +19,7 @@ protected function tearDown() public function testToStream() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $upload = $collection->createUpload(); $document = $upload->addContent('test content')->complete(); @@ -34,7 +34,7 @@ public function testToStream() public function testToFile() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $upload = $collection->createUpload(); $document = $upload->addContent('test content')->complete(); @@ -49,7 +49,7 @@ public function testToFile() public function testSubstr() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $upload = $collection->createUpload(); $upload->chunkSize = 10; diff --git a/tests/file/QueryTest.php b/tests/file/QueryTest.php index 46b2a564c..b622cde4a 100644 --- a/tests/file/QueryTest.php +++ b/tests/file/QueryTest.php @@ -2,6 +2,7 @@ namespace yiiunit\extensions\mongodb\file; +use yii; use yii\mongodb\file\Download; use yii\mongodb\file\Query; use yiiunit\extensions\mongodb\TestCase; @@ -28,7 +29,7 @@ protected function tearDown() */ protected function setUpTestRows() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); for ($i = 1; $i <= 10; $i++) { $collection->insertFileContent('content' . $i, [ 'filename' => 'name' . $i, @@ -41,28 +42,25 @@ protected function setUpTestRows() public function testAll() { - $connection = $this->getConnection(); $query = new Query(); - $rows = $query->from('fs')->all($connection); + $rows = $query->from('fs')->all(); $this->assertEquals(10, count($rows)); } public function testOne() { - $connection = $this->getConnection(); $query = new Query(); - $row = $query->from('fs')->one($connection); + $row = $query->from('fs')->one(); $this->assertTrue(is_array($row)); $this->assertTrue($row['file'] instanceof Download); } public function testDirectMatch() { - $connection = $this->getConnection(); $query = new Query(); $rows = $query->from('fs') ->where(['file_index' => 5]) - ->all($connection); + ->all(); $this->assertEquals(1, count($rows)); $file = $rows[0]; diff --git a/tests/file/StreamWrapperTest.php b/tests/file/StreamWrapperTest.php index ac82c14dc..8269079cf 100644 --- a/tests/file/StreamWrapperTest.php +++ b/tests/file/StreamWrapperTest.php @@ -2,15 +2,15 @@ namespace yiiunit\extensions\mongodb\file; +use yii; use yiiunit\extensions\mongodb\TestCase; class StreamWrapperTest extends TestCase { protected function tearDown() { - $connection = $this->getConnection(); - if (in_array($connection->fileStreamProtocol, stream_get_wrappers())) { - stream_wrapper_unregister($connection->fileStreamProtocol); + if (in_array(yii::$app->mongodb->fileStreamProtocol, stream_get_wrappers())) { + stream_wrapper_unregister(yii::$app->mongodb->fileStreamProtocol); } $this->dropFileCollection('fs'); @@ -22,7 +22,7 @@ protected function tearDown() public function testCreateFromDownload() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $upload = $collection->createUpload(); $document = $upload->addContent('test content')->complete(); @@ -36,15 +36,8 @@ public function testCreateFromDownload() public function testWriteResource() { - $connection = $this->getConnection(); - $this->mockApplication([ - 'components' => [ - 'mongodb' => $connection - ], - ]); - - $connection->registerFileStreamWrapper(true); - $databaseName = $connection->getDefaultDatabaseName(); + yii::$app->mongodb->registerFileStreamWrapper(true); + $databaseName = yii::$app->mongodb->getDefaultDatabaseName(); $url = "gridfs://{$databaseName}.fs?filename=test.txt"; $resource = fopen($url, 'w'); @@ -52,7 +45,7 @@ public function testWriteResource() fwrite($resource, 'end'); fclose($resource); - $collection = $connection->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $document = $collection->findOne(['filename' => 'test.txt']); $this->assertNotEmpty($document); @@ -61,19 +54,12 @@ public function testWriteResource() public function testReadResource() { - $connection = $this->getConnection(); - $this->mockApplication([ - 'components' => [ - 'mongodb' => $connection - ], - ]); - - $collection = $connection->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $upload = $collection->createUpload(); $document = $upload->addContent('test content')->complete(); - $connection->registerFileStreamWrapper(true); - $databaseName = $connection->getDefaultDatabaseName(); + yii::$app->mongodb->registerFileStreamWrapper(true); + $databaseName = yii::$app->mongodb->getDefaultDatabaseName(); $url = "gridfs://{$databaseName}.fs?_id=" . $document['_id']; $resource = fopen($url, 'r'); @@ -83,15 +69,8 @@ public function testReadResource() public function testSeek() { - $connection = $this->getConnection(); - $this->mockApplication([ - 'components' => [ - 'mongodb' => $connection - ], - ]); - - $connection->registerFileStreamWrapper(true); - $databaseName = $connection->getDefaultDatabaseName(); + yii::$app->mongodb->registerFileStreamWrapper(true); + $databaseName = yii::$app->mongodb->getDefaultDatabaseName(); $url = "gridfs://{$databaseName}.fs?filename=test.txt"; $resource = fopen($url, 'w'); diff --git a/tests/file/UploadTest.php b/tests/file/UploadTest.php index 95983d4e8..56c76bad9 100644 --- a/tests/file/UploadTest.php +++ b/tests/file/UploadTest.php @@ -3,6 +3,7 @@ namespace yiiunit\extensions\mongodb\file; use MongoDB\BSON\ObjectID; +use yii; use yiiunit\extensions\mongodb\TestCase; /** @@ -20,7 +21,7 @@ protected function tearDown() public function testAddContent() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $upload = $collection->createUpload(); $document = $upload->addContent('content line 1') @@ -37,7 +38,7 @@ public function testAddContent() */ public function testAddContentChunk() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $upload = $collection->createUpload(); $upload->chunkSize = 10; @@ -50,7 +51,7 @@ public function testAddContentChunk() public function testAddStream() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $upload = $collection->createUpload(); @@ -68,7 +69,7 @@ public function testAddStream() */ public function testCancel() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $upload = $collection->createUpload(); $document = $upload->addContent('content line 1'); @@ -85,7 +86,7 @@ public function testCancel() */ public function testCustomId() { - $collection = $this->getConnection()->getFileCollection(); + $collection = yii::$app->mongodb->getFileCollection(); $id = new ObjectID(); $upload = $collection->createUpload([ diff --git a/tests/i18n/MongoDbMessageSourceTest.php b/tests/i18n/MongoDbMessageSourceTest.php index d2f7273a9..33667745b 100644 --- a/tests/i18n/MongoDbMessageSourceTest.php +++ b/tests/i18n/MongoDbMessageSourceTest.php @@ -2,6 +2,7 @@ namespace yiiunit\extensions\mongodb\i18n; +use yii; use yii\i18n\I18N; use yii\mongodb\i18n\MongoDbMessageSource; use yiiunit\extensions\mongodb\TestCase; @@ -33,8 +34,7 @@ protected function tearDown() */ protected function setupTestRows() { - $db = $this->getConnection(); - $collection = $db->getCollection('message'); + $collection = yii::$app->mongodb->getCollection('message'); $collection->batchInsert([ [ 'language' => 'de', @@ -104,7 +104,6 @@ protected function setupI18N() $this->i18n = new I18N([ 'translations' => [ '*' => new MongoDbMessageSource([ - 'db' => $this->getConnection(), 'sourceLanguage' => 'en-US', ]) ] diff --git a/tests/log/MongoDbTargetTest.php b/tests/log/MongoDbTargetTest.php index 2c9847f19..7335bcffa 100644 --- a/tests/log/MongoDbTargetTest.php +++ b/tests/log/MongoDbTargetTest.php @@ -14,21 +14,11 @@ protected function tearDown() parent::tearDown(); } - /** - * @return MongoDbTarget test log target - */ - protected function createLogTarget() - { - return new MongoDbTarget([ - 'db' => $this->getConnection(), - ]); - } - // Tests : public function testExport() { - $target = $this->createLogTarget(); + $target = new MongoDbTarget(); $target->messages = [ [ diff --git a/tests/rbac/MongoDbManagerTest.php b/tests/rbac/MongoDbManagerTest.php index 5800aef55..16510ab70 100644 --- a/tests/rbac/MongoDbManagerTest.php +++ b/tests/rbac/MongoDbManagerTest.php @@ -23,7 +23,7 @@ class MongoDbManagerTest extends TestCase protected function setUp() { parent::setUp(); - $this->auth = $this->createManager(); + $this->auth = new MongoDbManager(); } protected function tearDown() @@ -34,14 +34,6 @@ protected function tearDown() parent::tearDown(); } - /** - * @return MongoDbManager - */ - protected function createManager() - { - return new MongoDbManager(['db' => $this->getConnection()]); - } - // Tests : public function testCreateRole() @@ -354,7 +346,7 @@ public function testAssignMultipleRoles() $this->auth->assign($reader, 'readingAuthor'); $this->auth->assign($author, 'readingAuthor'); - $this->auth = $this->createManager(); + $this->auth = new MongoDbManager(); $roles = $this->auth->getRolesByUser('readingAuthor'); $roleNames = []; @@ -376,7 +368,7 @@ public function testAssignmentsToIntegerId() $this->auth->assign($author, 1337); $this->auth->assign($reader, 1337); - $this->auth = $this->createManager(); + $this->auth = new MongoDbManager(); $this->assertEquals(0, count($this->auth->getAssignments(0))); $this->assertEquals(1, count($this->auth->getAssignments(42))); @@ -389,7 +381,7 @@ public function testGetAssignmentsByRole() $reader = $this->auth->getRole('reader'); $this->auth->assign($reader, 123); - $this->auth = $this->createManager(); + $this->auth = new MongoDbManager(); $this->assertEquals([], $this->auth->getUserIdsByRole('nonexisting')); $this->assertEquals(['reader A', '123'], $this->auth->getUserIdsByRole('reader'), '', 0.0, 10, true); diff --git a/tests/validators/MongoDateValidatorTest.php b/tests/validators/MongoDateValidatorTest.php index 32faf0efe..bb89195a8 100644 --- a/tests/validators/MongoDateValidatorTest.php +++ b/tests/validators/MongoDateValidatorTest.php @@ -12,7 +12,6 @@ class MongoDateValidatorTest extends TestCase protected function setUp() { parent::setUp(); - $this->mockApplication(); date_default_timezone_set('UTC'); } diff --git a/tests/validators/MongoIdValidatorTest.php b/tests/validators/MongoIdValidatorTest.php index 6c8208163..e066fb2f6 100644 --- a/tests/validators/MongoIdValidatorTest.php +++ b/tests/validators/MongoIdValidatorTest.php @@ -9,12 +9,6 @@ class MongoIdValidatorTest extends TestCase { - protected function setUp() - { - parent::setUp(); - $this->mockApplication(); - } - public function testValidateValue() { $validator = new MongoIdValidator();