TestCase   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
dl 0
loc 46
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A destroyApplication() 0 3 1
A tearDown() 0 5 1
A mockApplication() 0 8 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