DatabaseTest.php 2.16 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 16 17 18
    protected function tearDown()
    {
        $this->dropCollection('customer');
        $this->dropFileCollection('testfs');
        parent::tearDown();
    }
19

20
    // Tests :
21

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

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

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

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

37 38 39
    public function testGetFileCollection()
    {
        $database = $connection = $this->getConnection()->getDatabase();
40

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

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

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

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

56 57 58 59 60 61 62
        $result = $database->executeCommand([
            '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);
    }
AlexGx committed
70
}