TestCase::mockApplication()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 2
1
<?php
2
3
/**
4
 * @author Anton Tuyakhov <[email protected]>
5
 */
6
7
namespace tuyakhov\braintree;
8
9
use Yii;
10
use yii\console\Application;
11
use yii\helpers\ArrayHelper;
12
13
/**
14
 * This is the base class for all unit tests.
15
 */
16
abstract class TestCase extends \PHPUnit\Framework\TestCase
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected function setUp(): void
22
    {
23
        parent::setUp();
24
25
        static::mockApplication();
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function tearDown(): void
32
    {
33
        parent::tearDown();
34
35
        static::destroyApplication();
36
    }
37
38
    /**
39
     * Populates Yii::$app with a new application.
40
     * The application will be destroyed on tearDown() automatically.
41
     * @param array $config the application configuration, if needed
42
     * @param string $appClass name of the application class to create
43
     */
44
    protected static function mockApplication(array $config = [], string $appClass = Application::class)
45
    {
46
        $localConfigFile = __DIR__ . '/config/main-local.php';
47
        new $appClass(
48
            ArrayHelper::merge(
49
                require __DIR__ . '/config/main.php',
50
                is_file($localConfigFile) ? require $localConfigFile : [],
51
                $config
52
            )
53
        );
54
    }
55
56
    /**
57
     * Destroys application in Yii::$app by setting it to null.
58
     */
59
    protected static function destroyApplication()
60
    {
61
        Yii::$app = null;
62
    }
63
}
64