DatabaseTest.php 1.69 KB
Newer Older
1 2 3 4 5
<?php

namespace yiiunit\extensions\mongo;

use yii\mongo\Collection;
6
use yii\mongo\file\Collection as FileCollection;
7 8 9 10 11 12 13 14 15

/**
 * @group mongo
 */
class DatabaseTest extends MongoTestCase
{
	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 53 54 55 56 57 58 59 60 61 62
	public function testCommand()
	{
		$database = $connection = $this->getConnection()->getDatabase();

		$result = $database->execute([
			'distinct' => 'customer',
			'key' => 'name'
		]);
		$this->assertTrue(array_key_exists('ok', $result));
		$this->assertTrue(array_key_exists('values', $result));
	}
63
}