DatabaseTest.php 1.94 KB
Newer Older
1 2
<?php

3
namespace yiiunit\extensions\mongodb;
4

5 6
use yii\mongodb\Collection;
use yii\mongodb\file\Collection as FileCollection;
7 8

/**
9
 * @group mongodb
10
 */
11
class DatabaseTest extends MongoDbTestCase
12 13 14 15
{
	protected function tearDown()
	{
		$this->dropCollection('customer');
16
		$this->dropFileCollection('testfs');
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
		parent::tearDown();
	}

	// Tests :

	public function testGetCollection()
	{
		$database = $connection = $this->getConnection()->getDatabase();

		$collection = $database->getCollection('customer');
		$this->assertTrue($collection instanceof Collection);
		$this->assertTrue($collection->mongoCollection instanceof \MongoCollection);

		$collection2 = $database->getCollection('customer');
		$this->assertTrue($collection === $collection2);

		$collectionRefreshed = $database->getCollection('customer', true);
		$this->assertFalse($collection === $collectionRefreshed);
	}
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	public function testGetFileCollection()
	{
		$database = $connection = $this->getConnection()->getDatabase();

		$collection = $database->getFileCollection('testfs');
		$this->assertTrue($collection instanceof FileCollection);
		$this->assertTrue($collection->mongoCollection instanceof \MongoGridFS);

		$collection2 = $database->getFileCollection('testfs');
		$this->assertTrue($collection === $collection2);

		$collectionRefreshed = $database->getFileCollection('testfs', true);
		$this->assertFalse($collection === $collectionRefreshed);
	}

52
	public function testExecuteCommand()
53 54 55
	{
		$database = $connection = $this->getConnection()->getDatabase();

56
		$result = $database->executeCommand([
57 58 59 60 61 62
			'distinct' => 'customer',
			'key' => 'name'
		]);
		$this->assertTrue(array_key_exists('ok', $result));
		$this->assertTrue(array_key_exists('values', $result));
	}
63 64 65 66 67 68 69

	public function testCreateCollection()
	{
		$database = $connection = $this->getConnection()->getDatabase();
		$collection = $database->createCollection('customer');
		$this->assertTrue($collection instanceof \MongoCollection);
	}
70
}