MongoDbTestCase.php 3.88 KB
Newer Older
1 2
<?php

3
namespace yiiunit\extensions\mongodb;
4 5

use yii\helpers\FileHelper;
6
use yii\mongodb\Connection;
7
use Yii;
8
use yii\mongodb\Exception;
9 10
use yiiunit\TestCase;

11
class MongoDbTestCase extends TestCase
12
{
13 14 15 16 17 18 19 20 21 22 23 24
    /**
     * @var array Mongo connection configuration.
     */
    protected $mongoDbConfig = [
        'dsn' => 'mongodb://localhost:27017',
        'defaultDatabaseName' => 'yii2test',
        'options' => [],
    ];
    /**
     * @var Connection Mongo connection instance.
     */
    protected $mongodb;
25

26 27 28 29
    public static function setUpBeforeClass()
    {
        static::loadClassMap();
    }
30

31 32 33 34 35 36
    protected function setUp()
    {
        parent::setUp();
        if (!extension_loaded('mongo')) {
            $this->markTestSkipped('mongo extension required.');
        }
37
        $config = self::getParam('mongodb');
38 39 40 41 42 43
        if (!empty($config)) {
            $this->mongoDbConfig = $config;
        }
        $this->mockApplication();
        static::loadClassMap();
    }
44

45 46 47 48 49 50 51
    protected function tearDown()
    {
        if ($this->mongodb) {
            $this->mongodb->close();
        }
        $this->destroyApplication();
    }
52

53 54 55 56 57 58 59 60
    /**
     * Adds sphinx extension files to [[Yii::$classPath]],
     * avoiding the necessity of usage Composer autoloader.
     */
    protected static function loadClassMap()
    {
        $baseNameSpace = 'yii/mongodb';
        $basePath = realpath(__DIR__. '/../../../../extensions/mongodb');
61 62 63 64

        $alias = '@' . $baseNameSpace;
        if (!in_array($alias, Yii::$aliases)) {
            Yii::setAlias($alias, $basePath);
65 66
        }
    }
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    /**
     * @param  boolean                 $reset whether to clean up the test database
     * @param  boolean                 $open  whether to open test database
     * @return \yii\mongodb\Connection
     */
    public function getConnection($reset = false, $open = true)
    {
        if (!$reset && $this->mongodb) {
            return $this->mongodb;
        }
        $db = new Connection;
        $db->dsn = $this->mongoDbConfig['dsn'];
        $db->defaultDatabaseName = $this->mongoDbConfig['defaultDatabaseName'];
        if (isset($this->mongoDbConfig['options'])) {
            $db->options = $this->mongoDbConfig['options'];
        }
        if ($open) {
            $db->open();
        }
        $this->mongodb = $db;
Paul Klimov committed
88

89 90
        return $db;
    }
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105
    /**
     * Drops the specified collection.
     * @param string $name collection name.
     */
    protected function dropCollection($name)
    {
        if ($this->mongodb) {
            try {
                $this->mongodb->getCollection($name)->drop();
            } catch (Exception $e) {
                // shut down exception
            }
        }
    }
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120
    /**
     * Drops the specified file collection.
     * @param string $name file collection name.
     */
    protected function dropFileCollection($name = 'fs')
    {
        if ($this->mongodb) {
            try {
                $this->mongodb->getFileCollection($name)->drop();
            } catch (Exception $e) {
                // shut down exception
            }
        }
    }
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    /**
     * Finds all records in collection.
     * @param  \yii\mongodb\Collection $collection
     * @param  array                   $condition
     * @param  array                   $fields
     * @return array                   rows
     */
    protected function findAll($collection, $condition = [], $fields = [])
    {
        $cursor = $collection->find($condition, $fields);
        $result = [];
        foreach ($cursor as $data) {
            $result[] = $data;
        }

        return $result;
    }

    /**
     * Returns the Mongo server version.
     * @return string Mongo server version.
     */
    protected function getServerVersion()
    {
        $connection = $this->getConnection();
        $buildInfo = $connection->getDatabase()->executeCommand(['buildinfo' => true]);

        return $buildInfo['version'];
    }
Qiang Xue committed
151
}