TestCase::getVendorPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace kdn\yii2\validators;
4
5
use PHPUnit_Framework_TestCase;
6
use Yii;
7
use yii\helpers\ArrayHelper;
8
9
/**
10
 * Class TestCase.
11
 * @package kdn\yii2\validators
12
 */
13
abstract class TestCase extends PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * Mock Yii application.
17
     * @before
18
     */
19
    protected function prepare()
20
    {
21
        static::mockApplication();
22
    }
23
24
    /**
25
     * Clean up after test.
26
     * By default, the application created with `mockApplication` will be destroyed.
27
     * @after
28
     */
29
    protected function clear()
30
    {
31
        static::destroyApplication();
32
    }
33
34
    /**
35
     * Populates Yii::$app with a new application.
36
     * The application will be destroyed on clear() automatically.
37
     * @param array $config the application configuration, if needed
38
     * @param string $appClass name of the application class to create
39
     */
40
    protected static function mockApplication($config = [], $appClass = 'yii\console\Application')
41
    {
42
        new $appClass(
43
            ArrayHelper::merge(
44
                [
45
                    'id' => 'test-app',
46
                    'basePath' => __DIR__,
47
                    'vendorPath' => static::getVendorPath(),
48
                ],
49
                $config
50
            )
51
        );
52
    }
53
54
    /**
55
     * Get path to "vendor" directory.
56
     * @return string path to "vendor" directory.
57
     */
58
    protected static function getVendorPath()
59
    {
60
        return dirname(__DIR__) . '/vendor';
61
    }
62
63
    /**
64
     * Destroys application in Yii::$app by setting it to null.
65
     */
66
    protected static function destroyApplication()
67
    {
68
        Yii::$app = null;
69
    }
70
71
    /**
72
     * Add column to array.
73
     * @param array $array
74
     * @param mixed $value
75
     * @return array
76
     */
77
    protected static function arrayAddColumn($array, $value)
78
    {
79
        return array_map(
80
            function ($data) use ($value) {
81
                $data[] = $value;
82
83
                return $data;
84
            },
85
            $array
86
        );
87
    }
88
89
    /**
90
     * \u escape sequence for PHP.
91
     * @param string $text
92
     * @return string
93
     */
94
    protected static function u($text)
95
    {
96
        return json_decode("\"$text\"");
97
    }
98
}
99