TestCase.php 2.46 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php

Qiang Xue committed
3
namespace yiiunit;
AlexGx committed
4

Qiang Xue committed
5
use yii\helpers\ArrayHelper;
Qiang Xue committed
6

Carsten Brandt committed
7 8 9
/**
 * This is the base class for all yii framework unit tests.
 */
10
abstract class TestCase extends \PHPUnit_Framework_TestCase
w  
Qiang Xue committed
11
{
12 13 14 15 16 17 18 19 20 21 22 23 24 25
    public static $params;

    /**
     * Clean up after test.
     * By default the application created with [[mockApplication]] will be destroyed.
     */
    protected function tearDown()
    {
        parent::tearDown();
        $this->destroyApplication();
    }

    /**
     * Returns a test configuration param from /data/config.php
Qiang Xue committed
26 27
     * @param  string $name params name
     * @param  mixed $default default value to use when param is not set.
28 29
     * @return mixed  the value of the configuration param
     */
30
    public static function getParam($name, $default = null)
31 32 33 34 35 36 37 38 39 40 41
    {
        if (static::$params === null) {
            static::$params = require(__DIR__ . '/data/config.php');
        }

        return isset(static::$params[$name]) ? static::$params[$name] : $default;
    }

    /**
     * Populates Yii::$app with a new application
     * The application will be destroyed on tearDown() automatically.
Qiang Xue committed
42
     * @param array $config The application configuration, if needed
43 44 45 46
     * @param string $appClass name of the application class to create
     */
    protected function mockApplication($config = [], $appClass = '\yii\console\Application')
    {
Qiang Xue committed
47
        new $appClass(ArrayHelper::merge([
48 49
            'id' => 'testapp',
            'basePath' => __DIR__,
Qiang Xue committed
50 51
            'vendorPath' => $this->getVendorPath(),
        ], $config));
52 53
    }

Qiang Xue committed
54 55
    protected function mockWebApplication($config = [], $appClass = '\yii\web\Application')
    {
Qiang Xue committed
56
        new $appClass(ArrayHelper::merge([
Qiang Xue committed
57 58
            'id' => 'testapp',
            'basePath' => __DIR__,
Qiang Xue committed
59
            'vendorPath' => $this->getVendorPath(),
Qiang Xue committed
60 61 62
            'components' => [
                'request' => [
                    'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
Qiang Xue committed
63 64
                    'scriptFile' => __DIR__ .'/index.php',
                    'scriptUrl' => '/index.php',
Qiang Xue committed
65 66
                ],
            ]
Qiang Xue committed
67 68
        ], $config));
    }
Qiang Xue committed
69

Qiang Xue committed
70 71 72 73 74 75 76
    protected function getVendorPath()
    {
        $vendor = dirname(dirname(__DIR__)) . '/vendor';
        if (!is_dir($vendor)) {
            $vendor = dirname(dirname(dirname(dirname(__DIR__))));
        }
        return $vendor;
Qiang Xue committed
77 78
    }

79 80 81 82 83 84 85
    /**
     * Destroys application in Yii::$app by setting it to null.
     */
    protected function destroyApplication()
    {
        \Yii::$app = null;
    }
Zander Baldwin committed
86
}